Data Structures & AlgorithmsHard
How many index triples i < k < j in 2 4 6 3 9 5 satisfy A[i] + A[j] = 2·A[k] (the middle value is the arithmetic mean of the outer two)?
- A. 6
- B. 1
- C. 2
- D. 0
Correct answer: B. 1
Enumerate index triples in order with the middle index as k, and test the arithmetic-mean relation. Counting even-sum pairs, putting the middle value at the wrong end, or using a plain sum relation changes the count.
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
- What is the length of the longest contiguous subarray of 1 2 3 5 1 2 3 such that the sum of EVERY prefix of that subarray is divisible by 3?
- Why is a stack-based check that tracks TYPE as well as nesting needed for two bracket kinds?
- To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?
- For the 'largest number from concatenation' problem, why does the comparator compare x+y with y+x as strings?
- When choosing non-adjacent positions to maximise a sum, why does the DP keep two states per position?
- Why does the lexicographically largest subsequence after deleting k digits come from a greedy stack?