Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Rearrange an array such that every odd indexed element is greater than it previous

  • Difficulty Level : Easy
  • Last Updated : 31 Aug, 2021

Given an unsorted array, rearrange the array such that the 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 an 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 an even length, then it can be done in a single run. Just for each pair of neighbors, put the max in odd position and min in even. So we just start a cycle from the beginning and compare every two elements. Let’s say 
6, 5, 4, 3, 2, 1 will give us 5, 6, 3, 4, 1, 2 
If the array has an odd length, like 6, 5, 4, 3, 2, 1, 100 then it’s slightly trickier, we need to run the 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
{
    // Utility 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 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, 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));

        Rearrange(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
{

// Utility 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 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, 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] + " ");

    Rearrange(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
?>

Javascript

<script>
// Javascript program to rearrange array such that
// odd indexed elements are greater.
    
    // Utility function to Swap two variables
    function swap(arr,i,j)
    {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    
    // method to rearrange array such that
    // odd indexed elements are greater.
    function Rearrange(arr,n)
    {
        // Common code for odd and even lengths
        for (let 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 (let i = n - 1; i > 0; i = i - 2)
                if (arr[i] > arr[i - 1])
                    swap(arr, i, i - 1);
        }
    }
    
    // Driver Method
    let arr=[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
    document.write("Before rearranging<br>");
    for(let i=0;i<arr.length;i++)
    {
        document.write(arr[i]+" ");
    }
    
    document.write("<br>")
    Rearrange(arr, arr.length);  
    document.write("After rearranging<br>");
    for(let i=0;i<arr.length;i++)
    {
        document.write(arr[i]+" ");
    }
    
    
    // This code is contributed by avanitrachhadiya2155
</script>

Output: 

Before rearranging
1 2 3 4 5 6 7 8 9 
After rearranging
1 3 2 5 4 7 6 9 8 

Time Complexity: O(n)

Auxiliary Space: O(1)
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!