Data Structures & AlgorithmsHard
How many adjacent pairs (A[i], A[i+1]) in 4 7 2 9 1 8 5 6 have a sum of at least 10?
- A. 4
- B. 17
- C. 6
- D. 5
Correct answer: D. 5
Only neighbouring positions form adjacent pairs, so each of the n−1 pairs is checked once. Using a strict bound, counting all index pairs, or using the product instead of the sum gives different totals.
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
- In the 'maximum number of disjoint pairs with sum K' problem, why is a count-based greedy optimal?
- To find the longest subarray in which every value appears an even number of times, why can a parity bitmask over values be used with…
- Why is the minimum number of adjacent swaps to make a string palindromic NOT simply half the number of mismatched mirror pairs?
- When repeatedly deleting adjacent pairs of different letters, why does the final length have fixed parity?
- In the 'shortest block to sort so the whole array becomes sorted' problem, why does the block start at the first mismatch with the sorted…
- For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?