Data Structures & AlgorithmsVery hardGATE CS · DSA

How many pairs (i < j) of [2, 15, 17, 10, 24, 2, 10, 2, 4, 9] have a[i] + a[j] divisible by 4? (Count remainders mod 4 and pair r with 4 − r.)

  1. A. 33
  2. B. 13
  3. C. 15
  4. D. 12

Correct answer: B. 13

Remainder counts: 0→2, 1→2, 2→5, 3→1. Pairs come from classes r and k − r (C(c, 2) when r = 0 or r = k/2). The total is 13; multiplying c[r]·c[k − r] over all r double-counts and mishandles the self-paired classes.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic