Make K-Subarray 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 Sub-arrays: The array a is divided into sub-arrays based on the computed GCD, and each sub-array is sorted.
Calculating Absolute Differences: For each sub-array, 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.