Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many contiguous subarrays of [6, 12, 18, 8, 4] have gcd exactly equal to 2?

  1. A. 6
  2. B. 15
  3. C. 10
  4. D. 1

Correct answer: A. 6

No single element has gcd 2, and every subarray containing 18, 8 (gcd 2) or 6, 12, 18, 8 qualifies: [18,8], [12,18,8], [6,12,18,8], [18,8,4], [12,18,8,4], [6,12,18,8,4] = 6. 15 tests 'gcd is even', 10 counts every multi-element subarray and 1 only adjacent pairs.

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

Exams that ask this topic