Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many index pairs i < j in [5, 2, 8, 3, 1, 9] satisfy a[i] * a[j] <= 15?

  1. A. 15
  2. B. 8
  3. C. 7
  4. D. 3

Correct answer: B. 8

Sorted values 1, 2, 3, 5, 8, 9: two pointers find the pairs with product ≤ 15: (1,2), (1,3), (1,5), (1,8), (1,9), (2,3), (2,5), (3,5) = 8. Using < 15 drops (3,5) and gives 7, 15 counts all pairs, and 3 pairs only values ≤ √15.

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

Exams that ask this topic