Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
How many contiguous subarrays of [6, 12, 18, 8, 4] have gcd exactly equal to 2?
- A. 6
- B. 15
- C. 10
- 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.
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 least common multiple of the integers 2 through 20, reduced modulo 1000000007?
- How many contiguous subarrays of [2, 5, 3, 4, 10] have a product divisible by 10?
- How many integers from 1 to 1000 have digit sum exactly 10 and are odd?
- How many ordered pairs (a, b) with 1 <= a, b <= 6 satisfy a b ≡ 1 (mod 7)?
- How many contiguous subarrays of [1, 1, 2, 1, 1] have all elements of the same parity?
- How many index pairs i < j in [1, 3, 4, 7, 8, 10] satisfy |a[j] - a[i]| <= 3?