Input: arr[] = {1, 3, 3, 7}, K = 4
Output: 0
Explanation:
The given array can be split into 4 subarrays as {1}, {3}, {3}, and {7}.
The difference between minimum and maximum of each subarray is:
1. {1}, difference = 1 - 1 = 0
2. {3}, difference = 3 - 3 = 0
3. {3}, difference = 3 - 3 = 0
4. {7}, difference = 7 - 7 = 0
Therefore, the sum all the difference is 0 which is minimized.
Input: arr[] = {4, 8, 15, 16, 23, 42}, K = 3
Output: 12
Explanation:
The given array can be split into 3 subarrays as {4, 8, 15, 16}, {23}, and {42}.
The difference between minimum and maximum of each subarray is:
1. {4, 8, 15, 16}, difference = 16 - 4 = 12
2. {23}, difference = 23 - 23 = 0
3. {42}, difference = 42 - 42 = 0
Therefore, the sum all the difference is 12 which is minimized.