Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA
What is the length of the longest contiguous subarray of [2, 2, 1, 3, 1, 1, 2] whose sum is divisible by 4?
- A. 5
- B. 12
- C. 7
- D. 3
Correct answer: C. 7
Store the first index of each prefix sum mod 4 with prefix 0 at index −1. The total 12 is divisible by 4, so the whole array (7) works. Without that seed the best gap is 5; 12 is the sum and 3 a shorter valid window such as [1, 1, 2].
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 arithmetic triples (x, x+d, x+2d) with d >= 1 consist entirely of values from [1, 2, 3, 5, 7, 9]?
- Five jobs given as (processing time, deadline) are (4,4), (2,2), (3,3), (1,1), (6,7). You run them in some order, and a job is late if it…
- How many index pairs i < j in [4, 1, 5, 3, 2, 6] satisfy 0 < a[i] - a[j] <= 2?
- From the windows of length 3 of [3, 9, 2, 8, 1, 7], remove the smallest element of the chosen window and maximise the sum that remains…
- What is the sum of (max minus min) over all windows of size 3 in [2, 7, 1, 8, 2]?
- What is the smallest positive integer x such that the sum of floor(x / ai) over a = [2, 3, 5] is at least 20?