We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Write your code here
pair, triple := map[int32]int32{}, map[int32]int32{}
var res int32
for _, a := range arr {
if c, ok := triple[a]; ok {
res += c
}
if c, ok := pair[a]; ok {
triple[a+d] += c
}
pair[a+d]++
}
return res
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Triplets
You are viewing a single comment's thread. Return to all comments →
i learned this elegant solution from a similar problem "count triplets", where it is a geometric series ak = aj *d, aj = ai *d:
func beautifulTriplets(d int32, arr []int32) int32 {
}