Rearrange an array such that every odd indexed element is greater than it previous
Given an unsorted array, rearrange the array such that number at the odd index is greater than the number at the previous even index. There may be multiple outputs, we need to print one of them.
Note : Indexing is based on array so it always starts from 0.
Examples:
Input : arr[] = {5, 2, 3, 4}
Output : arr[] = {2, 5, 3, 4}
Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output : arr[] = {1, 3, 2, 5, 4, 7, 6, 9, 8}
If the given array has even length, then it can be done in single run. Just for each pair of neighbours, put max in odd position and min in even. So we just start cycle from beginning and compare each two elements. Let’s say
6, 5, 4, 3, 2, 1 will give us 5, 6, 3, 4, 1, 2
If array has odd length, like 6, 5, 4, 3, 2, 1, 100 then it’s slightly trickier, we need to run second, backward cycle again, but starting from the last element. So, 6, 5, 4, 3, 2, 1, 100 after first cycle will be 5, 6, 3, 4, 1, 2, 100 and after second cycle 5, 6, 3, 4, 1, 100, 2
C++
// C++ program to rearrange array such that // odd indexed elements are greater. #include<bits/stdc++.h> using namespace std; void rearrange(int arr[], int n) { // Common code for odd and even lengths for (int i=0; i<n-1; i=i+2) { if (arr[i] > arr[i+1]) swap(arr[i], arr[i+1]); } // If length is odd if (n & 1) { for (int i=n-1; i>0; i=i-2) if (arr[i] > arr[i-1]) swap(arr[i], arr[i-1]); } } /* Utility that prints out an array on a line */void printArray(int arr[], int size) { for (int i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } /* Driver function to test above functions */int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Before rearranging\n"; printArray(arr, n); rearrange(arr, n); cout << "After rearranging\n"; printArray(arr, n); return 0; } |
Java
// Java program to rearrange array such that // odd indexed elements are greater. import java.util.Arrays; class GFG { // Utitlity function to Swap two variables public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // method to rearrange array such that // odd indexed elements are greater. public static void Rearrage(int[] arr, int n) { // Common code for odd and even lengths for (int i = 0; i < n - 1; i = i + 2) { if (arr[i] > arr[i + 1]) swap(arr, i, i + 1); } // If length is odd if ((n & 1) > 0) { for (int i = n - 1; i > 0; i = i - 2) if (arr[i] > arr[i - 1]) swap(arr, i, i - 1); } } // Driver Method public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; System.out.println("Before rearranging"); System.out.println(Arrays.toString(arr)); Rearrage(arr, arr.length); System.out.println("After rearranging"); System.out.println(Arrays.toString(arr)); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python3
# Python 3 program to rearrange array such # that odd indexed elements are greater. def rearrange(arr, n): # Common code for odd and even lengths for i in range(0, n - 1, 2): if (arr[i] > arr[i + 1]): temp = arr[i] arr[i] = arr[i + 1] arr[i + 1] = temp # If length is odd if (n & 1): i = n - 1 while(i > 0): if (arr[i] > arr[i - 1]): temp = arr[i] arr[i] = arr[i - 1] arr[i - 1] = temp i -= 2 # Utility that prints out an # array on a line def printArray(arr, size): for i in range(0, size, 1): print(arr[i], end = " ") print("\n") # Driver Code if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] n = len(arr) print("Before rearranging") printArray(arr, n) rearrange(arr, n) print("After rearranging") printArray(arr, n) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to rearrange // array such that odd // indexed elements are // greater. using System; class GFG { // Utitlity function to // Swap two variables public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // method to rearrange // array such that odd // indexed elements are // greater. public static void Rearrage(int[] arr, int n) { // Common code for odd // and even lengths for (int i = 0; i < n - 1; i = i + 2) { if (arr[i] > arr[i + 1]) swap(arr, i, i + 1); } // If length is odd if ((n & 1) > 0) { for (int i = n - 1; i > 0; i = i - 2) if (arr[i] > arr[i - 1]) swap(arr, i, i - 1); } } // Driver Code static void Main() { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Console.WriteLine("Before rearranging"); for(int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " "); Rearrage(arr, arr.Length); Console.WriteLine("\nAfter rearranging"); for(int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " "); } } // This code is contributed // by Sam007 |
PHP
<?php // PHP program to rearrange array such that // odd indexed elements are greater. function rearrange(&$arr, $n) { // Common code for odd and even lengths for ($i = 0; $i < $n - 1; $i = $i + 2) { if ($arr[$i] > $arr[$i + 1]) { $temp = $arr[$i]; $arr[$i] = $arr[$i + 1]; $arr[$i + 1] = $temp; } } // If length is odd if ($n & 1) { for ($i = $n - 1; $i > 0; $i = $i - 2) if ($arr[$i] > $arr[$i - 1]) { $temp = $arr[$i]; $arr[$i] = $arr[$i - 1]; $arr[$i - 1] = $temp; } } } // Utility that prints out an array on a line function printArray(&$arr, $size) { for ($i = 0; $i < $size; $i++) echo $arr[$i] . " "; echo "\n"; } // Driver Code $arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $n = sizeof($arr); echo "Before rearranging\n"; printArray($arr, $n); rearrange($arr, $n); echo "After rearranging\n"; printArray($arr, $n); // This code is contributed by ita_c ?> |
Output:
Before rearranging 1 2 3 4 5 6 7 8 9 After rearranging 1 3 2 5 4 7 6 9 8
This article is contributed by Roshni Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Majority Element
- Search an element in a sorted and rotated array
- Merge an array of size n into another array of size m+n
- k largest(or smallest) elements in an array | added Min Heap method
- Program for array rotation
- Write a program to reverse an array or string
- Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Leaders in an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Segregate 0s and 1s in an array

