Last active
February 28, 2019 09:08
-
-
Save kkabdol/d62512ac2aa65bd82908b6a05c83b15e to your computer and use it in GitHub Desktop.
Count Triplets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// problem : https://www.hackerrank.com/challenges/count-triplets-1/problem | |
// solution : https://www.hackerrank.com/challenges/count-triplets-1/forum/comments/468507 | |
long countTriplets(vector<long> arr, long r) { | |
map<int,long> mp2, mp3; | |
//mp2 to hold count of needed values after this one to complete 2nd part of triplet | |
//mp3 to hold count of needed values to complete triplet | |
long count = 0; | |
for( long val : arr ) | |
{ | |
if ( mp3[val] > 0 ) //This value completes mp3[val] triplets | |
{ | |
count += mp3[val]; | |
} | |
if ( mp2[val] > 0 ) //This value is valid as 2° part of mp2[val] triplets | |
{ | |
mp3[val*r] += mp2[val]; | |
} | |
mp2[val*r]++; //"Push-up" this value as possible triplet start | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment