Data Structures & AlgorithmsHardGATE CS · TCS NQT
Moving one unit per step, what is the minimum total number of unit moves to make every element of [1, 2, 9, 10, 12] equal to a common value? (Use the median as target.)
- A. 20
- B. 19
- C. 17
- D. -4
Correct answer: B. 19
The sum of absolute deviations is minimised at the median; with median 9 the total is 8 + 7 + 0 + 1 + 3 = 19. The mean is a worse target for absolute deviation.
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 index triples i < j < k in [1, 2, 3, 4, 5, 6] satisfy a[i] + a[k] = 2 a[j]?
- What is the area of the largest rectangle under the histogram [3, 1, 3, 2, 2]?
- How many distinct XOR values arise from index pairs i < j of [1, 2, 3, 4]?
- How many non-empty prefixes of "abacaba" are palindromes?
- What is the minimum number of single-character flips needed to turn the binary string "10100101" into a string of the form 0...01...1 (all…
- For [1, -2, 3, 4, -1, 5, -6, 2], you may delete at most one element before taking a maximum sum over non-empty contiguous subarrays. What…