#C8870. Minimum Operations to Reach Target Sum
Minimum Operations to Reach Target Sum
Minimum Operations to Reach Target Sum
You are given an array of n integers and a target sum x. In one operation, you can increment or decrement any element of the array by 1. The goal is to make the sum of the array exactly equal to x using the minimum number of operations.
Note: Since each operation changes the total sum by 1, the minimum number of operations required is the absolute difference between the current sum of the array and x, i.e., \(|\text{sum}(array) - x|\).
Input Format: The input is given via standard input.
Output Format: Output a single integer representing the minimum number of operations required.
inputFormat
The first line contains an integer n
representing the number of elements in the array.
The second line contains n
space-separated integers which are the elements of the array.
The third line contains an integer x
, the target sum.
outputFormat
Print a single integer: the minimum number of operations required to transform the sum of the array into x
.
3
1 2 3
5
1
</p>