Data Structures & AlgorithmsHard
How many contiguous subarrays of 1 3 5 2 4 6 1 have a sum that is a perfect square (0 is not present here)?
- A. 3
- B. 5
- C. 2
- D. 6
Correct answer: B. 5
Test each subarray's sum with an exact integer square-root check; the product and the shifted sum are different quantities that happen to be squares sometimes. Excluding singletons removes real squares such as 1 and 4.
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 split points i (0 ≤ i ≤ n−2) of 2 5 1 4 3 6 2 7 satisfy: (sum of A[0..i]) mod 3 equals (sum of A[i+1..n−1]) mod 3?
- How many contiguous subarrays of 3 1 2 6 4 5 9 2 1 have a length divisible by 3 AND a sum divisible by 3?
- How many index triples i < j < k in 1 2 3 4 5 6 7 have A[i] + A[j] + A[k] divisible by 3?
- How many index pairs i < j in 1 2 3 4 5 6 8 15 satisfy A[i]·A[j] ≡ 1 (mod 7)?
- How many contiguous subarrays of 4 7 1 10 5 8 have (maximum − minimum) divisible by 3? (A single element gives 0, which counts as…
- What is the length of the shortest contiguous subarray of 3 8 2 5 1 4 6 whose sum is divisible by 5?