Created
October 15, 2021 03:09
-
-
Save hieuwu/a982651614967145a4d2402a097a8bb2 to your computer and use it in GitHub Desktop.
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
// Complete the countTriplets function below. | |
fun countTriplets(arr: Array<Long>, r: Long): Long { | |
var befMap = HashMap<Long, Int>() | |
var aftMap = HashMap<Long, Int>() | |
for(num in arr) { | |
if(aftMap.containsKey(num)) aftMap[num] = aftMap[num]!! + 1 | |
else aftMap[num] = 1 | |
} | |
var count = 0L | |
for(num in arr) { | |
if (aftMap.containsKey(num)) aftMap[num] = aftMap[num]!! - 1 | |
if (num % r == 0L && befMap.containsKey(num/r) && aftMap.containsKey(num*r) ) { | |
count += befMap[num/r]!! * aftMap[num*r]!! | |
} | |
if (befMap.containsKey(num)) befMap[num] = befMap[num]!! + 1 | |
else befMap[num] = 1 | |
} | |
return count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment