Given an array **A[] **of length **N**, the task is to find lexicographically smallest array by swapping adjacent elements for each index atmost once. Thus, for any index:

, at most one swap between A[K] and A[K+1] is allowed.**Example:**

Input:A[] = { 3, 2, 1, 4}Output:1 3 2 4Explanation:Perform the following swaps:

Swap A[1] and A[2], now A[] = { 3, 1, 2, 4 }

Swap A[0] and A[1], now A[] = { 1, 3, 2, 4 }

No further swaps are possible as A[1] and A[2] have already been swapped.Input:A[] = { 2, 1, 4, 3, 6, 5 }Output:1 2 3 4 5 6Explanation:Perform the following swaps:

Swap A[0] and A[1], now A[] = { 1, 2, 4, 3, 6, 5 }

Swap A[2] and A[3], now A[] = { 1, 2, 3, 4, 6, 5 }

Swap A[4] and A[5], now A[] = { 1, 2, 3, 4, 5, 6 }

**Approach:**

To solve the problem mentioned above we can apply **Greedy method**. We know that we can perform at most **N – 1 swaps** to make the given array as smallest as possible.

- Create a
**counter**variable and initialize with N-1 and a hash-map to store performed swaps. - Find the position of the minimum element from current index onward.
- Now perform swap backwards until we reach current element position.
- Also check if current swap is possible or not and decrement the counter also at each swap.
- Finally print the required array.

Below is the implementation of above approach:

`// C++ implementation to find the ` `// lexicographically smallest ` `// array by at most single swap ` `// for every pair of adjacent indices ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to find the ` `// lexicographically smallest array ` `void` `findSmallestArray(` `int` `A[], ` `int` `n) `
`{ ` ` ` `// maximum swaps possible `
` ` `int` `count = n - 1; `
` ` ` ` `// hash to store swaps performed `
` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> mp; `
` ` ` ` `for` `(` `int` `i = 0; i < n && count > 0; ++i) { `
` ` ` ` `// let current element be `
` ` `// the minimum possible `
` ` `int` `mn = A[i], pos = i; `
` ` ` ` `// Find actual position of `
` ` `// the minimum element `
` ` `for` `(` `int` `j = i + 1; j < n; ++j) { `
` ` ` ` `// Update minimum element and `
` ` `// its position `
` ` `if` `(A[j] < mn) { `
` ` `mn = A[j]; `
` ` `pos = j; `
` ` `} `
` ` `} `
` ` ` ` `// Perform swaps if possible `
` ` `while` `(pos > i && count > 0 `
` ` `&& !mp[{ pos - 1, pos }]) { `
` ` ` ` `// Insert current swap in hash `
` ` `mp[{ pos - 1, pos }] = 1; `
` ` ` ` `swap(A[pos], A[pos - 1]); `
` ` `--pos; `
` ` `--count; `
` ` `} `
` ` `} `
` ` ` ` `// print the required array `
` ` `for` `(` `int` `i = 0; i < n; ++i) `
` ` `cout << A[i] << ` `" "` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` ` ` `int` `A[] = { 2, 1, 4, 3, 6, 5 }; `
` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); `
` ` ` ` `findSmallestArray(A, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 implementation to find the ` `# lexicographically smallest array by ` `# at most single swap for every pair ` `# of adjacent indices ` ` ` `# Function to find the ` `# lexicographically smallest array ` `def` `findSmallestArray(A, n): `
` ` ` ` `# Maximum swaps possible `
` ` `count ` `=` `n ` `-` `1`
` ` ` ` `# Hash to store swaps performed `
` ` `mp ` `=` `{''} `
` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): `
` ` `if` `(count <` `=` `0` `): `
` ` `break` `; `
` ` ` ` `# Let current element be `
` ` `# the minimum possible `
` ` `mn ` `=` `A[i] `
` ` `pos ` `=` `i `
` ` ` ` `# Find actual position of `
` ` `# the minimum element `
` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): `
` ` ` ` `# Update minimum element `
` ` `# and its position `
` ` `if` `(A[j] < mn): `
` ` `mn ` `=` `A[j] `
` ` `pos ` `=` `j `
` ` ` ` `# Perform swaps if possible `
` ` `while` `(pos > i ` `and` `count > ` `0` `and` ` ` `((pos ` `-` `1` `, pos) ` `not` `in` `mp)): `
` ` ` ` `# Insert current swap in hash `
` ` `mp.add((pos ` `-` `1` `, pos)) `
` ` ` ` `A[pos], A[pos ` `-` `1` `] ` `=` `A[pos ` `-` `1` `], A[pos] `
` ` `pos ` `-` `=` `1`
` ` `count ` `-` `=` `1`
` ` ` ` `# Print the required array `
` ` `for` `i ` `in` `range` `(` `0` `, n): `
` ` `print` `(A[i], end ` `=` `" "` `) `
` ` `# Driver code ` `A ` `=` `[ ` `2` `, ` `1` `, ` `4` `, ` `3` `, ` `6` `, ` `5` `] `
`n ` `=` `len` `(A) `
` ` `findSmallestArray(A, n) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5 6

**Time Complexity:*** O(N ^{2})*

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 formed by removing at most one character
- Lexicographically smaller string by swapping at most one character pair
- Form the smallest number using at most one swap operation
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Lexicographically smallest string which differs from given strings at exactly K indices
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically largest string possible in one swap
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Lexicographically smallest array after at-most K consecutive swaps
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by removing duplicates
- Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
- Form the largest number using at most one swap operation
- Maximise the sum of two Numbers using at most one swap between them
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Length of array pair formed where one contains all distinct elements and other all same elements

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.