Given an array of integers, task is to print the array in the order – smallest number, Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number and so on…..
Examples:
Input : arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6]
Output :arr[] = {1, 9, 2, 8, 3, 7, 4, 6, 5}
Input : arr[] = [1, 2, 3, 4]
Output :arr[] = {1, 4, 2, 3}
A simple solution is to first find the smallest element, swap it with first element. Then find largest element, swap it with second element and so on. Time complexity of this solution is O(n2).
An efficient solution is to use sorting.
1. Sort the elements of array.
2. Take two variables say i and j and point them to the first and last index of the array respectively.
3. Now run a loop and store the elements in the array one by one by incrementing i and decrementing j.
Let’s take an array with input 5, 8, 1, 4, 2, 9, 3, 7, 6 and sort them so the array become 1, 2, 3, 4, 5, 6, 7, 8, 9. Now take two variables say i and j and point them to the first and last index of the array respectively, run a loop and store value into new array by incrementing i and decrementing j. We get final result as 1 9 2 8 3 7 4 6 5.
C++
// C++ program to print the array in given order #include <bits/stdc++.h> using namespace std; // Function which arrange the array. void rearrangeArray(int arr[], int n) { // Sorting the array elements sort(arr, arr + n); int tempArr[n]; // To store modified array // Adding numbers from sorted array to // new array accordingly int ArrIndex = 0; // Traverse from begin and end simultaneously for (int i = 0, j = n-1; i <= n / 2 || j > n / 2; i++, j--) { tempArr[ArrIndex] = arr[i]; ArrIndex++; tempArr[ArrIndex] = arr[j]; ArrIndex++; } // Modifying original array for (int i = 0; i < n; i++) arr[i] = tempArr[i]; } // Driver Code int main() { int arr[] = { 5, 8, 1, 4, 2, 9, 3, 7, 6 }; int n = sizeof(arr) / sizeof(arr[0]); rearrangeArray(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } |
Java
// Java program to print the array in given order import java.util.Arrays; public class GFG { // Function which arrange the array. static void rearrangeArray(int arr[], int n) { // Sorting the array elements Arrays.sort(arr); int[] tempArr = new int[n]; // To store modified array // Adding numbers from sorted array to // new array accordingly int ArrIndex = 0; // Traverse from begin and end simultaneously for (int i = 0, j = n-1; i <= n / 2 || j > n / 2; i++, j--) { if(ArrIndex < n) { tempArr[ArrIndex] = arr[i]; ArrIndex++; } if(ArrIndex < n) { tempArr[ArrIndex] = arr[j]; ArrIndex++; } } // Modifying original array for (int i = 0; i < n; i++) arr[i] = tempArr[i]; } // Driver Code public static void main(String args[]) { int arr[] = { 5, 8, 1, 4, 2, 9, 3, 7, 6 }; int n = arr.length; rearrangeArray(arr, n); for (int i = 0; i < n; i++) System.out.print(arr[i]+" "); } } // This code is contributed by Sumit Ghosh |
Python3
# Python 3 program to print # the array in given order # Function which arrange the # array. def rearrangeArray(arr, n) : # Sorting the array elements arr.sort() # To store modified array tempArr = [0] * (n + 1) # Adding numbers from sorted # array to new array accordingly ArrIndex = 0 # Traverse from begin and end # simultaneously i = 0 j = n-1 while(i <= n // 2 or j > n // 2 ) : tempArr[ArrIndex] = arr[i] ArrIndex = ArrIndex + 1 tempArr[ArrIndex] = arr[j] ArrIndex = ArrIndex + 1 i = i + 1 j = j - 1 # Modifying original array for i in range(0, n) : arr[i] = tempArr[i] # Driver Code arr = [ 5, 8, 1, 4, 2, 9, 3, 7, 6 ] n = len(arr) rearrangeArray(arr, n) for i in range(0, n) : print( arr[i], end = " ") # This code is contributed by Nikita Tiwari. |
C#
// C# program to print the // array in given order using System; public class GFG { // Function which arrange the array. static void rearrangeArray(int []arr, int n) { // Sorting the array elements Array.Sort(arr); // To store modified array int []tempArr = new int[n]; // Adding numbers from sorted array // to new array accordingly int ArrIndex = 0; // Traverse from begin and end simultaneously for (int i = 0, j = n-1; i <= n / 2 || j > n / 2; i++, j--) { if(ArrIndex < n) { tempArr[ArrIndex] = arr[i]; ArrIndex++; } if(ArrIndex < n) { tempArr[ArrIndex] = arr[j]; ArrIndex++; } } // Modifying original array for (int i = 0; i < n; i++) arr[i] = tempArr[i]; } // Driver Code public static void Main(String []args) { int []arr = {5, 8, 1, 4, 2, 9, 3, 7, 6}; int n = arr.Length; rearrangeArray(arr, n); for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to print // the array in given order // Function which // arrange the array. function rearrangeArray($arr, $n) { // Sorting the // array elements sort($arr); // To store modified array $tempArr = array($n); // Adding numbers from // sorted array to new // array accordingly $ArrIndex = 0; // Traverse from begin // and end simultaneously for ($i = 0, $j = $n - 1; $i <= $n / 2 || $j > $n / 2; $i++, $j--) { $tempArr[$ArrIndex] = $arr[$i]; $ArrIndex++; $tempArr[$ArrIndex] = $arr[$j]; $ArrIndex++; } // Modifying original array for ($i = 0; $i < $n; $i++) $arr[$i] = $tempArr[$i]; for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; } // Driver Code $arr = array(5, 8, 1, 4, 2, 9, 3, 7, 6 ); $n = count($arr) ; rearrangeArray($arr, $n); // This code is contributed // by Sam007 ?> |
Output:
1 9 2 8 3 7 4 6 5
Time Complexity : O(n Log n)
Auxiliary Space : O(n)
This article is contributed by Ayush Saxena. 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.
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 all elements of array which are multiples of x in increasing order
- Rearrange array such that difference of adjacent elements is in descending order
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Rearrange array such that all even-indexed elements in the Array is even
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Rearrange an array to make similar indexed elements different from that of another array
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Rearrange an array in maximum minimum form | Set 1
- Rearrange an array to minimize sum of product of consecutive pair elements
- Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Rearrange array such that even positioned are greater than odd
- Rearrange an array such that arr[i] = i
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Rearrange an array to maximize i*arr[i]
- Minimize swaps to rearrange array such that parity of index and corresponding element is same
- Rearrange an array such that product of every two consecutive elements is a multiple of 4

