Given an array arr[] of N positive integers with an equal number of even and odd elements. The task is to use in-place swapping to interchange positions of even and odd elements in the array.
Examples:
Input: arr[] = {1, 3, 2, 4}
Output: 2 4 1 3
Explanation:
Before rearranging the given array, indices 0 and 1 had odd elements and indices 2 and 3 had even elements.
After rearrangement, array becomes {2, 4, 1, 3} where indices 0 and 1 have even elements and indices 2 and 3 have odd elements.Input: arr[] = {2, 2, 1, 3}
Output: 1 3 2 2
Explanation:
Before rearranging the given array, indices 0 and 1 had even elements and indices 2 and 3 had odd elements.
After rearrangement, array becomes {1, 3, 2, 2} where indices 0 and 1 have odd elements and indices 2 and 3 have even elements.
Naive Approach: The simplest approach is to iterate over array elements using two loops, the outer loop picks each element of the array and the inner loop is to find the opposite parity element for the picked element and swap them. After swapping elements, mark the picked element as negative so as not to pick it again. Finally, make all elements positive and print the array.
Below is the implementation of the above approach:
Java
// Java program for the above approach import java.util.*; import java.lang.*; class GFG { // Function to replace each even // element by odd and vice-versa // in a given array static void replace(int[] arr) { // Stores length of array int n = arr.length; // Traverse array for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current element is even // then swap it with odd if (arr[i] >= 0 && arr[j] >= 0 && arr[i] % 2 == 0 && arr[j] % 2 != 0) { // Perform Swap int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; // Change the sign arr[j] = -arr[j]; break; } // If current element is odd // then swap it with even else if (arr[i] >= 0 && arr[j] >= 0 && arr[i] % 2 != 0 && arr[j] % 2 == 0) { // Perform Swap int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; // Change the sign arr[j] = -arr[j]; break; } } } // Marked element positive for (int i = 0; i < n; i++) arr[i] = Math.abs(arr[i]); // Print final array for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Driver Code public static void main(String[] args) { // Given array arr[] int[] arr = { 1, 3, 2, 4 }; // Function Call replace(arr); } } |
2 4 1 3
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Two Pointer Approach. Traverse the array by picking each element that is greater than 0 and search for the opposite parity element greater than 0 from the current index up to the end of the array. If found, swap the elements and multiply them with -1. Follow the below steps to solve the problem:
- Initialize the variables e and o with -1 that will store the currently found even and odd number respectively that is not yet taken.
- Traverse the given array over the range [0, N – 1] and do the following:
- Pick the element arr[i] if it is greater than 0.
- If arr[i] is even, increment o + 1 by 1 and find the next odd number that is not yet marked is found. Mark the current and the found number by multiplying them with -1 and swap them.
- If arr[i] is odd, increment e + 1 by 1 and find the next even number that is not yet marked is found. Mark the current and the found number by multiplying them with -1 and swap them.
- After traversing the whole array, multiply its elements with -1 to again make them positive and print that array.
Below is the implementation of the above approach:
Java
// Java program for the above approach import java.io.*; class GFG { // Function to replace odd elements // with even elements and vice versa static void swapEvenOdd(int arr[]) { // Length of the array int n = arr.length; int o = -1, e = -1; // Traverse the given array for (int i = 0; i < n; i++) { // If arr[i] is visited if (arr[i] < 0) continue; int r = -1; if (arr[i] % 2 == 0) { o++; // Find the next odd element while (arr[o] % 2 == 0 || arr[o] < 0) o++; r = o; } else { e++; // Find next even element while (arr[e] % 2 == 1 || arr[e] < 0) e++; r = e; } // Mark them visited arr[i] *= -1; arr[r] *= -1; // Swap them int tmp = arr[i]; arr[i] = arr[r]; arr[r] = tmp; } // Print the final array for (int i = 0; i < n; i++) { System.out.print( (-1 * arr[i]) + " "); } } // Driver Code public static void main(String[] args) { // Given array arr[] int arr[] = { 1, 3, 2, 4 }; // Function Call swapEvenOdd(arr); } } |
2 4 1 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Count subarrays having sum of elements at even and odd positions equal
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Rearrange array such that all even-indexed elements in the Array is even
- Check if all strings of an array can be made same by interchanging characters
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Even numbers at even index and odd numbers at odd index
- Reverse the elements only at odd positions in the given Array
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange array such that even positioned are greater than odd
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Find Kth element in an array containing odd elements first and then even elements
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Modify given array to make sum of odd and even indexed elements same
- Check if the array can be sorted only if the elements on given positions can be swapped
- Missing even and odd elements from the given arrays
- Rearrange an array such that every odd indexed element is greater than it previous
- Rearrange and update array elements as specified by the given queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

