Lexicographical smallest alternate Array

Given an array arr[] of distinct elements, the task is to rearrange the array such that it is lexicographically smallest and of the form arr[0] > arr[1] < arr[2] > arr[3] < …

Examples:

Input: arr[] = {3, 2, 1, 4, 5}
Output: 2 1 4 3 5

Input: arr[] = {10, 22}
Output: 22 10

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: In order to get the lexicographically smallest array, we can choose the minimum element as the first element but that will not satisfy the condition where the first element has to be strictly greater than the second element.
Now, the second-best choice is to choose the second minimum from the array and the only element which is smaller than it is the smallest element which will be the second element of the array.
Apply the same process for the rest of the array elements, choose the second minimum of the remaining elements and then choose the minimum for every two consecutive positions which can be obtained by first sorting the given array then swapping every two consecutive elements.

Below is the implementation of the above approach:

C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the ` `// contents of an array ` `void` `printArr(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``cout << arr[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Function to find the lexicographically ` `// smallest alternating array ` `void` `smallestArr(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Sort the array ` `    ``sort(arr, arr + n); ` ` `  `    ``// Swap every two consecutive elements ` `    ``for` `(``int` `i = 0; i + 1 < n; i = i + 2) { ` `        ``swap(arr[i], arr[i + 1]); ` `    ``} ` ` `  `    ``// Print the re-arranged array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 2, 1, 4, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``smallestArr(arr, n); ` ` `  `    ``return` `0; ` `} `

Java

 `// Java implementation of the approach  ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to print the ` `    ``// contents of an array ` `    ``static` `void` `printArr(``int``[] arr, ``int` `n)  ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Function to find the lexicographically ` `    ``// smallest alternating array ` `    ``static` `void` `smallestArr(``int``[] arr, ``int` `n)  ` `    ``{ ` ` `  `        ``// Sort the array ` `        ``Arrays.sort(arr); ` ` `  `        ``// Swap every two consecutive elements ` `        ``for` `(``int` `i = ``0``; i + ``1` `< n; i = i + ``2``)  ` `        ``{ ` `            ``int` `temp = arr[i]; ` `            ``arr[i] = arr[i + ``1``]; ` `            ``arr[i + ``1``] = temp; ` `        ``} ` ` `  `        ``// Print the re-arranged array ` `        ``printArr(arr, n); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``3``, ``2``, ``1``, ``4``, ``5` `}; ` `        ``int` `n = arr.length; ` `        ``smallestArr(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to print the ` `# contents of an array ` `def` `printArr(arr, n): ` `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `" "``); ` ` `  `# Function to find the lexicographically ` `# smallest alternating array ` `def` `smallestArr(arr, n): ` ` `  `    ``# Sort the array ` `    ``arr.sort(); ` ` `  `    ``# Swap every two consecutive elements ` `    ``for` `i ``in` `range``(``0``, n ``-` `1``, ``2``): ` ` `  `        ``temp ``=` `arr[i]; ` `        ``arr[i] ``=` `arr[i ``+` `1``]; ` `        ``arr[i ``+` `1``] ``=` `temp; ` ` `  `    ``# Print the re-arranged array ` `    ``printArr(arr, n); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``arr ``=` `[ ``3``, ``2``, ``1``, ``4``, ``5` `]; ` `    ``n ``=` `len``(arr); ` `    ``smallestArr(arr, n); ` `     `  `# This code contributed by Rajput-Ji `

C#

 `// C# implementation of the approach  ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `    ``// Function to print the ` `    ``// contents of an array ` `    ``static` `void` `printArr(``int``[] arr, ``int` `n)  ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Function to find the lexicographically ` `    ``// smallest alternating array ` `    ``static` `void` `smallestArr(``int``[] arr, ``int` `n)  ` `    ``{ ` ` `  `        ``// Sort the array ` `        ``Array.Sort(arr); ` ` `  `        ``// Swap every two consecutive elements ` `        ``for` `(``int` `i = 0; i + 1 < n; i = i + 2)  ` `        ``{ ` `            ``int` `temp = arr[i]; ` `            ``arr[i] = arr[i + 1]; ` `            ``arr[i + 1] = temp; ` `        ``} ` ` `  `        ``// Print the re-arranged array ` `        ``printArr(arr, n); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] arr = { 3, 2, 1, 4, 5 }; ` `        ``int` `n = arr.Length; ` `        ``smallestArr(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

PHP

 ` `

Output:

```2 1 4 3 5
```

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.