Help in solving MAKEALLEQUAL

public static void main(String[] args) {
        FastReader fs = new FastReader();
        StringBuilder ans = new StringBuilder();
        int t = fs.nextInt();
        while(t-- > 0){
            int n =fs.nextInt(), m = fs.nextInt();
            int[] arr = new int[n];
            for(int i = 0;i<n;i++)
                arr[i] = fs.nextInt();
            int[] changeArr = new int[n];
            Arrays.sort(arr);
            int j = n-1;
            int change = 0, count = 0;
            while(j >= 0){
                arr[j] += change;
                if(arr[j] == arr[n-1])
                    j--;
                else{
                    int iterationsForThisValue = arr[n-1] - arr[j];
                    change += iterationsForThisValue;
                    count += iterationsForThisValue;
                    changeArr[Math.max(0,j-m+1)] += iterationsForThisValue;
                    change -= changeArr[j];
                    j--;
                }
            }
            ans.append(count + "\n");
        }
        System.out.println(ans);
    }

iterationsForThisValue means how much do i have to change the jth element to get it to max element
changeArr[j] marks the starts of the window which ends at j + m th element.