Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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