w3resource

Java: Partition an given array of integers into even number first and odd number second


Partition Even and Odd

Write a Java program that partitions an array of integers into even and odd numbers.

Original array: [7, 2, 4, 1, 3, 5, 6, 8, 2, 10]
After partition the said array becomes: [10, 2, 4, 2, 8, 6, 5, 3, 1, 7]

Visual Presentation:

Java Basic Exercises: Partition an given array of integers into even number first and odd number second.


Sample Solution:

Java Code:

// Importing necessary Java utilities
import java.util.*;

// Main class Solution
public class Solution {  
	
    // Main method
    public static void main(String[] args) {
		int[] nums = {7, 2, 4, 1, 3, 5, 6, 8, 2, 10};
		
		// Printing the original array
		System.out.println("Original array: " + Arrays.toString(nums));
		
     	// Calling the partitionArray2 method to partition the array
        int[] result = partitionArray2(nums);
        
        // Printing the resulting array after partitioning
        System.out.println("After partition the said array becomes: " + Arrays.toString(result));
    }

    // Method to partition the array based on odd and even numbers
    public static int[] partitionArray2(int[] nums) {
        int i = 0; // Initializing pointer i to the start of the array
        int j = nums.length - 1; // Initializing pointer j to the end of the array
        
        // Looping until pointers i and j meet or cross each other
        while (i < j) {
            // Moving pointer i until it finds an odd number
            while (nums[i] % 2 == 0) {
                i++;
            }
            
            // Moving pointer j until it finds an even number
            while (nums[j] % 2 != 0) {
                j--;
            }
            
            // Swapping the odd and even numbers if i is less than j
            if (i < j) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        
		// Returning the partitioned array
		return nums;
    }
} 

Sample Output:

Original array: [7, 2, 4, 1, 3, 5, 6, 8, 2, 10]
After partition the said array becomes: [10, 2, 4, 2, 8, 6, 5, 3, 1, 7]

Flowchart:

Flowchart: Java exercises: Partition an given array of integers into even number first and odd number second.



For more Practice: Solve these Related Problems:

  • Write a Java program to partition an array into prime and composite numbers.
  • Write a Java program to perform a stable partition of an array into even and odd numbers while preserving their original order.
  • Write a Java program to partition an array into three groups: even numbers, odd numbers, and zeros.
  • Write a Java program to rearrange an array in-place such that all even numbers appear before odd numbers without using extra space.

Go to:


PREV : Delete Middle Node in Linked List.
NEXT : Clone Binary Tree.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.