Given an array of **N** integers. The task is to find the lexicographically smallest array possible by applying the given operation any number of times. The operation is to pick two elements **a _{i}** and

**a**(1<=i, j<=N) such that

_{j}**a**is odd, and then swap

_{i}+ a_{j}**a**and

_{i}**a**.

_{j}**Examples:**

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

Output :1 2 3 4 5

Explanation :First swap (5, 2) and then (4, 3). This is the

lexicographically smallest possible array that can be obtained by

swapping array elements satifies given condition

Input :a[] = {4, 2}

Output :4 2

Explanation :Not possible to swap any elements.

**Approach**: Observe that swapping of 2 elements is possible if they have different parity. If all elements in the array have the same parity **(odd + odd and even + even is not odd)**, no swaps are possible. Hence the answer will be the input array only. Otherwise, you can actually swap any pair of elements. Assume you want to swap 2 elements, a and b, and they have the same parity. There must be a third element c that has a different parity. Without loss of generality, assume the array is **[a, b, c]**. Let’s do the following swaps:

- Swap a and c:
- Swap b and c: [b, c, a]
- Swap a and c: [b, a, c]

In other words, use c as an intermediate element to swap a and b, and it’ll always return to its original position afterward. Since swapping is possible between any pair of elements, we can always sort the array, which will be the lexicographically smallest array.

Below is the implementation of the above approach:

## C++

`// CPP program to find possible ` `// lexicographically smaller array ` `// by swapping only elements whose sum is odd ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find possible lexicographically smaller array ` `void` `lexicographically_smaller(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store number of even and odd integers ` ` ` `int` `odd = 0, even = 0; ` ` ` ` ` `// Find number of even and odd integers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % 2) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If it possible to make ` ` ` `// lexicographically smaller ` ` ` `if` `(odd && even) ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 4, 3, 2 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function call ` ` ` `lexicographically_smaller(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find possible ` `// lexicographically smaller array ` `// by swapping only elements whose sum is odd ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find possible lexicographically smaller array ` `static` `void` `lexicographically_smaller(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store number of even and odd integers ` ` ` `int` `odd = ` `0` `, even = ` `0` `; ` ` ` ` ` `// Find number of even and odd integers ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % ` `2` `== ` `1` `) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If it possible to make ` ` ` `// lexicographically smaller ` ` ` `if` `(odd > ` `0` `&& even > ` `0` `) ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `System.out.print(arr[i] + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `5` `, ` `4` `, ` `3` `, ` `2` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Function call ` ` ` `lexicographically_smaller(arr, n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find possible ` `# lexicographically smaller array ` `# by swapping only elements whose sum is odd ` ` ` `# Function to find possible ` `# lexicographically smaller array ` `def` `lexicographically_smaller(arr, n): ` ` ` ` ` `# To store number of even and odd integers ` ` ` `odd, even ` `=` `0` `, ` `0` `; ` ` ` ` ` `# Find number of even and odd integers ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `1` `): ` ` ` `odd ` `+` `=` `1` `; ` ` ` `else` `: ` ` ` `even ` `+` `=` `1` `; ` ` ` ` ` `# If it possible to make ` ` ` `# lexicographically smaller ` ` ` `if` `(odd > ` `0` `and` `even > ` `0` `): ` ` ` `arr.sort(); ` ` ` ` ` `# Print the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `print` `(arr[i], end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `5` `, ` `4` `, ` `3` `, ` `2` `]; ` ` ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `# Function call ` ` ` `lexicographically_smaller(arr, n); ` ` ` `# This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find possible ` `// lexicographically smaller array by ` `// swapping only elements whose sum is odd ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find possible ` `// lexicographically smaller array ` `static` `void` `lexicographically_smaller(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `// To store number of even and odd integers ` ` ` `int` `odd = 0, even = 0; ` ` ` ` ` `// Find number of even and odd integers ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % 2 == 1) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If it possible to make ` ` ` `// lexicographically smaller ` ` ` `if` `(odd > 0 && even > 0) ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Print the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `Console.Write(arr[i] + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 5, 4, 3, 2 }; ` ` ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Function call ` ` ` `lexicographically_smaller(arr, n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5

**Time Complexity**: O(N log 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.

## Recommended Posts:

- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smaller string by swapping at most one character pair
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest array after at-most K consecutive swaps
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Lexicographically smallest rotated sequence | Set 2
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Construct lexicographically smallest palindrome
- Make a lexicographically smallest palindrome with minimal changes
- K-th lexicographically smallest unique substring of a given string
- Lexicographically Kth smallest way to reach given coordinate from origin
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Queries to answer the X-th smallest sub-string lexicographically

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.