Ctrl K

Matching Pairs at Divisible Positions

Easy
Beads on a string are numbered from zero, and colors[i] is the color of bead i. You are also given a number divisor. Count the pairs of positions (i, j) with i < j where both beads have the same color (colors[i] == colors[j]) and the product i * j is divisible by divisor.
Example 1
colors=[4, 2, 4, 2, 4], divisor=3 → 3

Equal pairs are (0, 2), (0, 4), (2, 4) for color 4 and (1, 3) for color 2. Their products are 0, 0, 8 and 3, and 0, 0 and 3 are divisible by 3. The answer is 3.

Example 2
colors=[1, 2, 3], divisor=1 → 0

No two colors match, so there are no pairs.

Example 3
colors=[7, 7], divisor=5 → 1

The only pair is (0, 1): 0 · 1 = 0, and zero is divisible by anything.

Constraints
  • 1 ≤ len(colors) ≤ 100
  • 1 ≤ colors[i] ≤ 100
  • 1 ≤ divisor ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
colors=[4, 2, 4, 2, 4], divisor=3
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 90 tests on the server, hidden ones included.