Search for a command to run...
Given an array of integers arr[] and a target integer $k$, the task is to find a continuous subarray such that the absolute difference between its sum and $k$ is minimized. The function must return the sum of that closest subarray.
Input: $[2,-3,5,1,7], 8$
Output: $6$
> The subarray $[5, 1]$ has a sum of $6$. The absolute difference is $|6 - 8| = 2$. This is the minimum difference found among all continuous subarrays.
Iterate through all possible subarrays (using nested loops) and calculate the sum for each. Keep track of the sum that yields the smallest absolute difference from $k$.
Array, Subarray, Brute Force, Prefix Sum (Optimization), Minimum Difference
Example 1
[2,-3,5,1,7],88[2,-3,5,1,7],8
8