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:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to replace each even // element by odd and vice-versa // in a given array void replace( int arr[], int n) { // 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] = abs (arr[i]); // Print final array for ( int i = 0; i < n; i++) cout << arr[i] << " " ; } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 3, 2, 4 }; int n = sizeof (arr) / sizeof (arr[0]); // Function Call replace(arr,n); } // This code is contributed by SURENDRA_GANGWAR |
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); } } |
Python3
# Python3 program for the # above approach # Function to replace each even # element by odd and vice-versa # in a given array def replace(arr, n): # Traverse array for i in range (n): for j in range (i + 1 , n): # If current element is # even then swap it with odd if (arr[i] > = 0 and arr[j] > = 0 and arr[i] % 2 = = 0 and arr[j] % 2 ! = 0 ): # Perform Swap 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 elif (arr[i] > = 0 and arr[j] > = 0 and arr[i] % 2 ! = 0 and arr[j] % 2 = = 0 ): # Perform Swap tmp = arr[i] arr[i] = arr[j] arr[j] = tmp # Change the sign arr[j] = - arr[j] break # Marked element positive for i in range (n): arr[i] = abs (arr[i]) # Print final array for i in range (n): print (arr[i], end = " " ) # Driver Code if __name__ = = "__main__" : # Given array arr[] arr = [ 1 , 3 , 2 , 4 ] n = len (arr) # Function Call replace(arr, n) # This code is contributed by Chitranayal |
C#
// C# program for the above approach using System; 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 readonly array for ( int i = 0; i < n; i++) Console.Write(arr[i] + " " ); } // Driver Code public static void Main(String[] args) { // Given array []arr int [] arr = { 1, 3, 2, 4 }; // Function Call replace(arr); } } // This code is contributed by Rajput-Ji |
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:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define N 3 #define M 4 // Function to replace odd elements // with even elements and vice versa void swapEvenOdd( int arr[], int n) { 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++) { cout << (-1 * arr[i]) << " " ; } } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 3, 2, 4 }; // Length of the array int n = sizeof (arr) / sizeof (arr[0]); // Function Call swapEvenOdd(arr, n); } // This code is contributed by Rajput-Ji |
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); } } |
Python3
# Python3 program for the above approach # Function to replace odd elements # with even elements and vice versa def swapEvenOdd(arr): # Length of the array n = len (arr) o = - 1 e = - 1 # Traverse the given array for i in range (n): # If arr[i] is visited if (arr[i] < 0 ): continue r = - 1 if (arr[i] % 2 = = 0 ): o + = 1 # Find the next odd element while (arr[o] % 2 = = 0 or arr[o] < 0 ): o + = 1 r = o else : e + = 1 # Find next even element while (arr[e] % 2 = = 1 or arr[e] < 0 ): e + = 1 r = e # Mark them visited arr[i] * = - 1 arr[r] * = - 1 # Swap them tmp = arr[i] arr[i] = arr[r] arr[r] = tmp # Print the final array for i in range (n): print (( - 1 * arr[i]), end = " " ) # Driver Code if __name__ = = '__main__' : # Given array arr arr = [ 1 , 3 , 2 , 4 ] # Function Call swapEvenOdd(arr) # This code is contributed by Amit Katiyar |
C#
// C# program for the above approach using System; 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 readonly array for ( int i = 0; i < n; i++) { Console.Write((-1 * arr[i]) + " " ); } } // Driver Code public static void Main(String[] args) { // Given array []arr int []arr = { 1, 3, 2, 4 }; // Function Call swapEvenOdd(arr); } } // This code is contributed by Amit Katiyar |
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.