0% found this document useful (0 votes)
547 views

Hackerrank Problem Solving

The document contains descriptions and solutions for several Hackerrank problem solving challenges including: 1) A sock merchant problem to count the number of matching pairs of socks from an array of sock colors. 2) A simple array sum problem to return the sum of elements in an integer array. 3) A breaking records problem to count the number of times the highest/lowest scores were broken in a games scores array. 4) A kangaroo problem to determine if two kangaroos can reach the same position at the same time based on their starting positions and jump distances. 5) A staircase printing problem to print a staircase pattern of hash symbols with spaces. 6)

Uploaded by

Ayan Saha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
547 views

Hackerrank Problem Solving

The document contains descriptions and solutions for several Hackerrank problem solving challenges including: 1) A sock merchant problem to count the number of matching pairs of socks from an array of sock colors. 2) A simple array sum problem to return the sum of elements in an integer array. 3) A breaking records problem to count the number of times the highest/lowest scores were broken in a games scores array. 4) A kangaroo problem to determine if two kangaroos can reach the same position at the same time based on their starting positions and jump distances. 5) A staircase printing problem to print a staircase pattern of hash symbols with spaces. 6)

Uploaded by

Ayan Saha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Hackerrank Problem Solving

Sales by Match - There is a large pile of socks that must be paired by color. Given
an array of integers representing the color of each sock, determine how many pairs of
socks with matching colors there are.

Function Description
Complete the sockMerchant function in the editor below.

sockMerchant has the following parameter(s):

 int n: the number of socks in the pile


 int ar[n]: the colors of each sock

int sockMerchant(int n, vector<int>& ar) {

        map<int, int> counter;
        int pairs = 0;
        
        for(int& n : ar){
            counter[n]++;
            
            if((counter[n]%2) == 0){
                pairs++;
            }
        
        }
        return pairs;

}
Simple Array Sum

Given an array of integers, find the sum of its elements.

For example, if the array ar = [1,2,3], 1+2+3=6, so return 6.

Function Description
Complete the simpleArraySum function in the editor below. It must return the
sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

 ar: an array of integers

int simpleArraySum(vector<int> ar) {
        
        int sum = 0;
        for(int i=0; i<ar.size(); i++){
            
            sum = sum + ar[i];
        }
    return sum;

}
Breaking the Records

Maria plays college basketball and wants to go pro. Each season she maintains a
record of her play. She tabulates the number of times she breaks her season
record for most points and least points in a game. Points scored in the first game
establish her record for the season, and she begins counting from there.
Example
scores = [12,24,10,24]

Scores are in the same order as the games played. She tabulates her results as
follows:

Count

Game Score Minimum Maximum Min Max

0 12 12 12 0 0

1 24 12 24 0 1

2 10 10 24 1 1

3 24 10 24 1 1

Given the scores for a season, determine the number of times Maria breaks her
records for most and least points scored during the season.

vector<int> breakingRecords(vector<int> scores) {
        int maxi = scores[0];
        int mini = scores[0];
        
        int maxcount = 0;
        int mincount = 0;
    
        for(int i=0; i<scores.size(); i++){
                if(scores[i]>maxi){
                        maxi = scores[i];
                        maxcount++;
                }
                if(scores[i]<mini){
                        mini = scores[i];
                        mincount++;
                
                }
        
        }
        return {maxcount, mincount};
}

Number Line Jumps

You are choreographing a circus show with various animals. For one act,
you are given two kangaroos on a number line ready to jump in the
positive direction (i.e, toward positive infinity).

You have to figure out a way to get both kangaroos at the same location at the
same time as part of the show. If it is possible, return YES, otherwise return NO.

Function Description
Complete the function kangaroo in the editor below.

kangaroo has the following parameter(s):

 int x1, int v1: starting position and jump distance for kangaroo 1
 int x2, int v2: starting position and jump distance for kangaroo 2
Returns
 string: either YES or NO

string kangaroo(int x1, int v1, int x2, int v2) {
        
            if(v1<=v2){
                return "NO";
            }
            
            if(((x2-x1)%(v1-v2)) == 0){
                return "YES";
            }
            else {
                return "NO";
            }
            
            
}
Staircase

This is a staircase of size n=4:

#
##
###
####
Function Description
Complete the staircase function in the editor below.

staircase has the following parameter(s):

 int n: an integer
Print

Print a staircase as described above.

void staircase(int n) {
            
            for(int i=0; i<n; i++){
                
                for(int j=0; j<n-i-1; j++){
                    cout << " ";
                }
                for(int k=0; k<=i; k++){
                    cout << "#";
                }
                cout << endl;
            }
}
Compare the Triplets

Alice and Bob each created one problem for HackerRank. A reviewer rates the
two challenges, awarding points on a scale from 1 to 100 for three
categories: problem clarity, originality, and difficulty.
The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for
Bob's challenge is the triplet b = (b[0], b[1], b[2]).
The task is to find their comparison points by
comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
 If a[i] > b[i], then Alice is awarded 1 point.
 If a[i] < b[i], then Bob is awarded 1 point.
 If a[i] = b[i], then neither person receives a point.

Comparison points is the total points a person earned.

Given a and b, determine their respective comparison points.

vector<int> compareTriplets(vector<int> a, vector<int> b) {
            
            vector<int> v; 
            
            int alice = 0;
            int bob = 0;
            
            for(int i=0; i<3; i++){
                if(a[i]>b[i])
                    alice++;
                
                if(a[i]<b[i])
                    bob++; 
            }
            
            v.push_back(alice);
            v.push_back(bob);
            
            return v;
}

You might also like