Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many non-empty contiguous subarrays of [6, 10, 15, 7] have gcd equal to 1?

  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: B. 4

The gcd of a subarray is non-increasing as it grows, so for each right end the valid left ends form a contiguous block. Extending a subarray can only lower the gcd, so once it reaches 1 every longer one also does.

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

Exams that ask this topic