Data Structures & AlgorithmsHardCodeforces Div2 B · GATE CS
Each move changes an element of [1, 2, 9, 10, 4, 4, 3] by exactly 1 (up or down). What is the minimum number of moves needed to make all elements equal to the same value?
- A. 20
- B. 17
- C. -5
- D. 18
Correct answer: B. 17
The sum of |aᵢ − x| is minimised at a median of the values, not the mean; for an odd count the median is the middle element after sorting. The mean minimises the sum of squared deviations instead, and gives a larger cost here.
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
- Ropes of lengths [4, 3, 2, 6] are joined two at a time, and joining costs the sum of their lengths. You may choose the order of joins…
- Vertical lines have heights [1, 8, 6, 2, 5, 4, 8, 3, 7, 9, 1, 6] at positions 0 to 11. Choose two lines; the water they hold is…
- What is the maximum possible sum of entries of a non-empty contiguous rectangular submatrix of [[1, -2, 3, 0], [-1, 4, -2, 2], [2, -3, 5…
- A fence has 5 posts and each post is painted one of k = 3 colours. No three consecutive posts may share the same colour. How many…
- Target string "abcbc" must be built by concatenating words from the list ['ab', 'bc', 'abc', 'a', 'b', 'c', 'cb']; each word can be…
- A truck carries at most 5 boxes. Box types are given as (number of boxes of this type, units per box): [(1, 3), (2, 2), (3, 1), (2, 4)]…