Along a harbor channel, n buoys are anchored in a single-file line, numbered outward from the shore starting at position 0. Each buoy periodically flashes a signal code, an integer identifying the navigation pattern it belongs to. Two buoys at positions i and j (with i < j) form a synchronized pair when they flash the identical signal code and the product i * j is an exact multiple of a given tide-cycle length k (that is, i * j leaves no remainder when divided by k). Count how many synchronized pairs of buoys exist along the channel.
Line 1: two integers n and k. Line 2: n integers, the signal codes of the buoys in position order (0-indexed).
A single integer: the number of synchronized buoy pairs.
Example 1
Input
6 2 3 1 2 2 2 4
Expected
3
Explanation
The signal codes are [3,1,2,2,2,4] at positions 0-5. Only code 2 repeats, at positions 2, 3, and 4. Pair (2,3): product 6, divisible by k=2, counts. Pair (2,4): product 8, divisible by 2, counts. Pair (3,4): product 12, divisible by 2, counts. All three candidate pairs qualify, giving 3.
Example 2
Input
4 1 1 1 1 1
Expected
6
Explanation
Every buoy flashes code 1, so all C(4,2)=6 position pairs are candidates. Since k=1, every integer product is divisible by 1, so all 6 pairs count.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →