Make KSubarray Sums Equal


Here’s the explanation for each step:
Computing the GCD: Using the gcd function, we find the greatest common divisor between the length of the array a and the given integer k.
Dividing into Subarrays: The array a is divided into subarrays based on the computed GCD, and each subarray is sorted.
Calculating Absolute Differences: For each subarray, the median is found (i.e., the middle element), and the sum of the absolute differences between each element and the median is computed.
Summing the Results: The total sum of the absolute differences is then computed and returned as the final result.