Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
How many non-empty contiguous subarrays of [6, 10, 15, 7] have gcd equal to 1?
- A. 5
- B. 4
- C. 3
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the sum of gcd(a[i..j]) over all contiguous subarrays of [4, 6, 8, 12]?
- How many index pairs i < j have gcd(a[i], a[j]) = 1 for the array [2, 3, 4, 5, 6, 9]?
- What is the sum of lcm(a[i], a[i+1]) for consecutive pairs of the array [2, 3, 4, 6, 9]?
- How many integers x in the range 1 to 1000 are divisible by 3 and have a digit sum divisible by 7?
- How many contiguous subarrays of [3, 4, 1, 2, 5, 6] have a sum divisible by 5?
- How many pairs i < j with 1 <= i, j <= 12 have i times j divisible by 12?