Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA
How many distinct values in [3, 3, 4, 2, 4, 4, 2, 4, 4] occur strictly more than n/3 times (n = 9)?
- A. 2
- B. 0
- C. 1
- D. 3
Correct answer: C. 1
Boyer-Moore with two candidates finds all values above n/3 in one pass plus a verification pass. Only 4 occurs more than n/3 = 3 times (it occurs 5 times), so the answer is one value; the Boyer-Moore pass finds it.
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 non-empty subarrays of [1, 2, 1, 2, 3, 3, 1] contain every value an even number of times?
- Flowers bloom on the days [1, 10, 3, 10, 2]. You need to make 2 bouquets, each from 2 adjacent bloomed flowers (flowers may only be used…
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1, 1], you may flip exactly one 0 to 1 (or flip none). What is the length of the longest run of…
- How many index pairs i < j in [4, 9, 1, 7, 3, 8, 2] have a sum s with 6 <= s <= 10?
- How many contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?
- Greedy removal: scanning "aabaacaab" left to right, you delete any 'a' that immediately follows another 'a' in the current output. How…