HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT - IN C++
WARM-UP CHALLENGES
1. SALES BY MATCH
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
No comments:
Post a Comment