OA | Shrinking Number Line
Anonymous User
8636

We have an array of n integers, point, and an integer k. We can perform either of the following operations once for each point[i] in point

  1. Increment point[i] by k
  2. Decrement point[i] by k.

We want to perform an operation on each point[i] such that the difference between the maximum and minimum values in the array after performing all operations is minimal.

For example, if the array point = [0,1,2,3] and k=2, we can add k to the first three elements and subtract it from the last to get pointnew = [2,3,4,1]. The maximum element is 4 and the minimum is 1. Their absolute difference is 3. The permutations of operations on the example array follow :

(attached photo below)
image

Comments (4)