Nextjump OA 2020
Suppose you have an array of elements as 13, 10, 21, 20.
Now you need to find out minimum moves to segregate even followed by odd elements in the Array so that all even elements will be in the front of the array and all odd elements will be in the back of the array. So the resulting array will 20, 10, 21, 13.
It’s simply you need to swap element 13 with element 20 in the array. So you need only one move to segregate the even and odd elements in the array.
for [8,5,11,4,6]
swap 5 and 4
swap 11 and 6
Thus,
minimum moves = 2
I was able to come up with this BF (got TLE) :
static int minMovesToEvenFollowedByOdd(int[] arr) {
int moves = 0;
int totalLength = arr.length;
for (int i = 0; i <= totalLength / 2; i++) {
if (arr[i] % 2 != 0) {
for (int j = totalLength / 2; j < totalLength; j++) {
if (arr[j] % 2 == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
moves++;
break;
} else {
continue;
}
}
} else {
continue;
}
}
return moves;Any thoughts on this?