Wednesday, April 7, 2021

HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT WARM -UP CHALLENGES - IN C++

 HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT - IN C++


WARM-UP CHALLENGES


1. SALES BY MATCH

https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup


Solution - 1:

Algo-

Sort array in increasing/ decreasing order

Compare adjacent sock colors - if equal increase count of a pair, and also increment index, so as to not a sock twice (and count 2 pairs of 3 socks)

TC = O(nlogn+n) = O(nlogn)

int sockMerchant(int n, vector<int> ar) {
    sort(ar.begin(), ar.end());
    int cnt_pair = 0;
    for(int i = 0; i < ar.size() - 1; i++){
        if(ar[i] == ar[i+1]){
            cnt_pair++;
            i++;
        }
    }
    return cnt_pair;
}



2. COUNTING VALLEYS

https://www.hackerrank.com/challenges/counting-valleys/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup&h_r=next-challenge&h_v=zen

No comments:

Post a Comment

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...