A sensor logs readings that should grow by a constant factor, such as a population that doubles each step. Given the log and the factor, count how many ways you can pick three readings, in log order, that grow by exactly that factor each time.
Write countGeometricTriplets(nums, r). Return the number of index triplets (i, j, k) with i < j < k such that nums[j] === nums[i] * r and nums[k] === nums[j] * r.
Rules:
Triplets are counted by position, so equal values at different indexes give different triplets.
nums must be an array of positive safe integers and r a positive safe integer; otherwise throw a TypeError. r can be 1, in which case the three values are equal.
An array with fewer than three elements returns 0.
Arrays can hold 200,000 values. Checking every triplet, or every pair, will not finish in time. The answer always fits in a safe integer for these sizes.
countGeometricTriplets([1, 2, 2, 4], 2); // 2: indexes (0,1,3) and (0,2,3)countGeometricTriplets([1, 3, 9, 9, 27, 81], 3); // 6countGeometricTriplets([2, 2, 2, 2], 1); // 4: choose any 3 of 4countGeometricTriplets([4, 2, 1], 2); // 0: the order is wrong
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
2
code listings
6
edge cases
What's inside
The full problem, with examples
Tests that check your solution
Step-by-step
Approach
Solution
Complexity
Edge cases
Follow-up questions
Card worldwide
UPI and net banking in India
One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.