Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many index pairs i < j have gcd(a[i], a[j]) = 1 for the array [2, 3, 4, 5, 6, 9]?
- A. 10
- B. 8
- C. 12
- D. 9
Correct answer: D. 9
Count all pairs first (there are 15) and then subtract pairs sharing a prime factor. Using inclusion-exclusion over prime divisors avoids checking each pair.
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 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?
- What is the value of the sum of floor(20 / i) for i = 1 to 20?
- A bubble sort performs left-to-right passes, swapping adjacent elements that are out of order, and stops after the first pass in which no…