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.
Python O(n)
Maintain two counters: one contains the frequency of each element at the right of the current element and the other the frequency of each element at the left.
Them, iterate over the array, remove one occurence of the current element from right counter, sum to the triplets the frequency of (element / r) in left counter * frequency of (element * r) in right counter and add one occurence of element in left counter.
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Python O(n) Maintain two counters: one contains the frequency of each element at the right of the current element and the other the frequency of each element at the left. Them, iterate over the array, remove one occurence of the current element from right counter, sum to the triplets the frequency of (element / r) in left counter * frequency of (element * r) in right counter and add one occurence of element in left counter.