Data Structures & AlgorithmsHard
How many index pairs i < j in [2, 8, 3, 18, 4, 12, 5] have a product A[i]·A[j] that is a perfect square?
- A. 11
- B. 16
- C. 2
- D. 4
Correct answer: D. 4
A product is a perfect square exactly when every prime appears to an even power across the two factors, so test the product itself. Including i = j adds self-pairs, a divisibility shortcut is only a necessary condition, and testing the sum tests the wrong expression.
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
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8] have a sum that is a prime number?
- For each contiguous window of length 3 in [1, 2, 1, 3, 2, 1], count the number of distinct values inside it, then add these counts over…
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and a pop may be performed whenever the stack is…
- What is the minimum number of positive integers (each value at most 10, repetitions allowed in the array) needed so that EVERY integer…
- Given a sorted array, why is counting triplets with sum < T O(n²) rather than O(n³)?
- Floyd's cycle detection finds the slow and fast pointers meeting inside the loop. How is the START of the loop then found?