Minimum moves to segregate even followed by odd elements in Array

Introduction

This tutorial will find out minimum moves to segregate even followed by odd elements in an Array using Java programming language. The function will put all even numbers first, and then odd numbers and finally count the number of moves required to arrange all elements in the desired order.

This example also works on duplicate elements but does not remove any duplicate element. The duplicate elements will be replaced by appropriate even/odd elements.

Prerequisite

Knowledge of Java

Example

Let’s move on to the example implementation…

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.

If you have duplicate elements, for example, 5, 8, 5, 11, 4, 6. The duplicate elements will be exchanged. For this example the minimum moves required is 2, because you have to replace 5 by 4 and 5 by 6 again.

Segregate Even followed by Odd

The complete source code in Java is given below:

package com.roytuts.minimum.moves.segregate.even.odd;

import java.util.Arrays;

public class EvenOddArrayElementsApp {

	public static void main(String[] args) {
		int[] arr = new int[] { 13, 10, 21, 20 };

		int minMoves = minMovesToEvenFollowedByOdd(arr);

		System.out.println("Minimum moves to segregate even followed by odd elements: " + minMoves);

		arr = new int[] { 5, 8, 5, 11, 4, 6 };

		minMoves = minMovesToEvenFollowedByOdd(arr);

		System.out.println("Minimum moves to segregate even followed by odd elements: " + minMoves);
	}

	private 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;
			}
		}

		System.out.println("Array elements after moving: " + Arrays.toString(arr));

		return moves;
	}

}

In the above example as you need to segregate the array elements into two parts – even followed by odd. Therefore it is needed to check whether the length of the array is even or not. Finally replace the odd by even in the first part of the array and odd elements to be pushed in the second part of the array.

I am also displaying the final array after rearranging the elements in the array.

Testing the Application

Now run the above code, you should see below output:

Array elements after moving: [20, 10, 21, 13]
Minimum moves to segregate even followed by odd elements: 1
Array elements after moving: [4, 8, 6, 11, 5, 5]
Minimum moves to segregate even followed by odd elements: 2
minimum moves to segregate even followed by odd elements in array

That’s all about moving elements to segregate even followed by odd elements in an array.

Source Code

Download

1 thought on “Minimum moves to segregate even followed by odd elements in Array

Leave a Reply

Your email address will not be published. Required fields are marked *