Closest Subarray Sum to K
EasyArrayStringsubarraySTRING
### Description
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.
### Constraints
* $1 \le n \le 10^4$ (Size of array)
* $-10^5 \le arr[i] \le 10^5$
* $-10^6 \le k \le 10^6$
### Example
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.
---
### Hint
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$.
### Topics
Array, Subarray, Brute Force, Prefix Sum (Optimization), Minimum Difference
Examples
Example 1
Input: [2,-3,5,1,7],8
Output: 8