#C8329. Minimum Operations to Achieve Target Sum
Minimum Operations to Achieve Target Sum
Minimum Operations to Achieve Target Sum
Given an array of (N) integers and a target sum (K), the task is to determine the minimum number of operations required to make the sum of the array exactly equal to (K).
In one operation, you can increase or decrease any array element by 1. The minimum number of operations required is given by the formula:
[
|\text{sum}(\text{arr}) - K|,
] where (\text{sum}(\text{arr})) is the sum of all the elements in the array.
For example, if the array is [1, 2, 3, 4, 5] and (K = 15), then the answer is 0 since the array already sums to 15. Otherwise, you adjust the array elements accordingly.
inputFormat
The first line of the input contains two integers (N) and (K), where (N) is the number of elements in the array and (K) is the target sum. The second line contains (N) space-separated integers representing the elements of the array.
outputFormat
Output a single integer denoting the minimum number of operations required to make the sum of the array exactly equal to (K).## sample
5 15
1 2 3 4 5
0