# Variations - ZCO15002

I’ve successfully solved this problem (CodeChef: Practical coding for everyone). But I wanted to solve this problem using yet another method. I thought that a merge sort type approach can work for this problem as we did in inversion count. So I partition the array in two halves. Find variation count for each half and the final sorted array when merging. Then return the result. But it provides me a WA judgment. Can somebody point out where am I being mistaken? Check method solve3(). Link: https://pastebin.com/7FW876kA.

@arita_roy, I have also thought the same as you. But I am also getting WA. Don’t know where am I wrong?

private static int merge(int[] arr, long k, int start, int mid, int end) {
int count = 0;
ArrayList left = new ArrayList<>();
ArrayList right = new ArrayList<>();
ArrayList result = new ArrayList<>();

``````		for (int i = start; i <= mid; i++) {
}

for (int i = mid + 1; i <= end; i++) {
}

int i = 0, j = 0;
// 2,3 1
while (i < left.size() && j < right.size()) {
if (left.get(i) > right.get(j)) {
if (Math.abs(left.get(i) - right.get(j)) >= k) {
count += Math.abs(left.size() - i);
}
j++;
} else {
if (Math.abs(left.get(i) - right.get(j)) >= k) {
count += Math.abs(right.size() - j);
}
i++;
}
}

if (i != left.size()) {
while (i != left.size()) {
i++;
}
}
if (j != right.size()) {
while (j != right.size()) {