# Maximum number formed from array with K number of adjacent swaps allowed

Given an array a[ ] and the number of adjacent swap operations allowed are K. The task is to find the max number that can be formed using these swap operations.

Examples:

Input : a[]={ 1, 2, 9, 8, 1, 4, 9, 9, 9 }, K = 4
Output : 9 8 1 2 1 4 9 9 9
After 1st swap a[ ] becomes 1 9 2 8 1 4 9 9 9
After 2nd swap a[ ] becomes 9 1 2 8 1 4 9 9 9
After 3rd swap a[ ] becomes 9 1 8 2 1 4 9 9 9
After 4th swap a[ ] becomes 9 8 1 2 1 4 9 9 9

Input : a[]={2, 5, 8, 7, 9}, K = 2
Output : 8 2 5 7 9

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

Approach:

• Starting from the first digit, check for the next K digits and store the index of the largest number.
• Bring that greatest digit to the top by swapping the adjacent digits .
• Reduce to value of K by the number of adjacent swaps done.
• Repeat the above steps untill the number of swaps becomes zero.

Below is the implementation of the above approach

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the ` `// elements of the array ` `void` `print(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``cout << arr[i] << ``" "``; ` `    ``} ` `    ``cout << endl; ` `} ` ` `  `// Exchange array elements one by ` `// one from  right to left side ` `// starting from the current position ` `// and ending at the target position ` `void` `swapMax(``int``* arr, ``int` `target_position, ` `                      ``int` `current_position) ` `{ ` `    ``int` `aux = 0; ` `    ``for` `(``int` `i = current_position; ` `         ``i > target_position; i--) { ` `        ``aux = arr[i - 1]; ` `        ``arr[i - 1] = arr[i]; ` `        ``arr[i] = aux; ` `    ``} ` `} ` ` `  `// Function to return the ` `// maximum number array ` `void` `maximizeArray(``int``* arr, ` `                   ``int` `length, ``int` `swaps) ` `{ ` `    ``// Base condition ` `    ``if` `(swaps == 0) ` `        ``return``; ` ` `  `    ``// Start from the first index ` `    ``for` `(``int` `i = 0; i < length; i++) { ` `        ``int` `max_index = 0, max = INT_MIN; ` ` `  `        ``// Search for the next K elements ` `        ``int` `limit = (swaps + i) > length ?  ` `                        ``length : swaps + i; ` ` `  `        ``// Find index of the maximum ` `        ``// element in next K elements ` `        ``for` `(``int` `j = i; j <= limit; j++) { ` `            ``if` `(arr[j] > max) { ` `                ``max = arr[j]; ` `                ``max_index = j; ` `            ``} ` `        ``} ` ` `  `        ``// Update the value of ` `        ``// number of swaps ` `        ``swaps -= (max_index - i); ` ` `  `        ``// Update the array elements by ` `        ``// swapping adjacent elements ` `        ``swapMax(arr, i, max_index); ` ` `  `        ``if` `(swaps == 0) ` `            ``break``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 9, 8, 1, 4, 9, 9, 9 }; ` `    ``int` `length = ``sizeof``(arr) / ``sizeof``(``int``); ` `    ``int` `swaps = 4; ` `    ``maximizeArray(arr, length, swaps); ` ` `  `    ``print(arr, length); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `class` `GFG ` `{ ` ` `  `// Function to print the ` `// elements of the array ` `static` `void` `print(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``System.out.print(arr[i] + ``" "``); ` `    ``} ` `    ``System.out.println(); ` `} ` ` `  `// Exchange array elements one by ` `// one from right to left side ` `// starting from the current position ` `// and ending at the target position ` `static` `void` `swapMax(``int``[] arr, ``int` `target_position, ` `                    ``int` `current_position) ` `{ ` `    ``int` `aux = ``0``; ` `    ``for` `(``int` `i = current_position; ` `        ``i > target_position; i--)  ` `    ``{ ` `        ``aux = arr[i - ``1``]; ` `        ``arr[i - ``1``] = arr[i]; ` `        ``arr[i] = aux; ` `    ``} ` `} ` ` `  `// Function to return the ` `// maximum number array ` `static` `void` `maximizeArray(``int``[] arr, ` `                ``int` `length, ``int` `swaps) ` `{ ` `    ``// Base condition ` `    ``if` `(swaps == ``0``) ` `        ``return``; ` ` `  `    ``// Start from the first index ` `    ``for` `(``int` `i = ``0``; i < length; i++)  ` `    ``{ ` `        ``int` `max_index = ``0``, max = Integer.MIN_VALUE; ` ` `  `        ``// Search for the next K elements ` `        ``int` `limit = (swaps + i) > length ?  ` `                        ``length : swaps + i; ` ` `  `        ``// Find index of the maximum ` `        ``// element in next K elements ` `        ``for` `(``int` `j = i; j <= limit; j++)  ` `        ``{ ` `            ``if` `(arr[j] > max)  ` `            ``{ ` `                ``max = arr[j]; ` `                ``max_index = j; ` `            ``} ` `        ``} ` ` `  `        ``// Update the value of ` `        ``// number of swaps ` `        ``swaps -= (max_index - i); ` ` `  `        ``// Update the array elements by ` `        ``// swapping adjacent elements ` `        ``swapMax(arr, i, max_index); ` ` `  `        ``if` `(swaps == ``0``) ` `            ``break``; ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `arr[] = { ``1``, ``2``, ``9``, ``8``, ``1``, ``4``, ``9``, ``9``, ``9` `}; ` `    ``int` `length = arr.length; ` `    ``int` `swaps = ``4``; ` `    ``maximizeArray(arr, length, swaps); ` ` `  `    ``print(arr, length); ` `} ` `} ` ` `  `/* This code is contributed by PrinciRaj1992 */`

## Python3

 `# Python3 implementation of the above approach  ` `import` `sys ` ` `  `# Function to print the  ` `# elements of the array  ` `def` `print_ele(arr, n) : ` `     `  `    ``for` `i ``in` `range``(n) : ` `        ``print``(arr[i],end``=``" "``);  ` `         `  `    ``print``();  ` ` `  `# Exchange array elements one by  ` `# one from right to left side  ` `# starting from the current position  ` `# and ending at the target position  ` `def` `swapMax(arr, target_position,  ` `                    ``current_position) : ` `                         `  `    ``aux ``=` `0``;  ` `    ``for` `i ``in` `range``(current_position, target_position,``-``1``) : ` `        ``aux ``=` `arr[i ``-` `1``];  ` `        ``arr[i ``-` `1``] ``=` `arr[i];  ` `        ``arr[i] ``=` `aux;  ` ` `  `# Function to return the  ` `# maximum number array  ` `def` `maximizeArray(arr, length, swaps) :  ` ` `  `    ``# Base condition  ` `    ``if` `(swaps ``=``=` `0``) : ` `        ``return``;  ` ` `  `    ``# Start from the first index  ` `    ``for` `i ``in` `range``(length) : ` `        ``max_index ``=` `0``; ``max` `=` `-``(sys.maxsize``-``1``); ` `         `  `        ``# Search for the next K elements ` `        ``if` `(swaps ``+` `i) > length : ` `            ``limit ``=` `length ` `        ``else``: ` `            ``limit ``=` `swaps ``+` `i ` `             `  `        ``# Find index of the maximum ` `        ``# element in next K elements ` `        ``for` `j ``in` `range``(i, limit ``+` `1``) : ` `            ``if` `(arr[j] > ``max``) : ` `                ``max` `=` `arr[j]; ` `                ``max_index ``=` `j; ` `                 `  `        ``# Update the value of ` `        ``# number of swaps ` `        ``swaps ``-``=` `(max_index ``-` `i);  ` ` `  `        ``# Update the array elements by  ` `        ``# swapping adjacent elements  ` `        ``swapMax(arr, i, max_index);  ` ` `  `        ``if` `(swaps ``=``=` `0``) : ` `            ``break``;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[ ``1``, ``2``, ``9``, ``8``, ``1``, ``4``, ``9``, ``9``, ``9` `];  ` `    ``length ``=` `len``(arr);  ` `    ``swaps ``=` `4``;  ` `    ``maximizeArray(arr, length, swaps);  ` ` `  `    ``print_ele(arr, length);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# program to find the sum  ` `// and product of k smallest and  ` `// k largest prime numbers in an array  ` ` `  `using` `System; ` `     `  `class` `GFG ` `{ ` ` `  `// Function to print the ` `// elements of the array ` `static` `void` `print(``int` `[]arr, ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``Console.Write(arr[i] + ``" "``); ` `    ``} ` `    ``Console.WriteLine(); ` `} ` ` `  `// Exchange array elements one by ` `// one from right to left side ` `// starting from the current position ` `// and ending at the target position ` `static` `void` `swapMax(``int``[] arr, ``int` `target_position, ` `                    ``int` `current_position) ` `{ ` `    ``int` `aux = 0; ` `    ``for` `(``int` `i = current_position; ` `        ``i > target_position; i--)  ` `    ``{ ` `        ``aux = arr[i - 1]; ` `        ``arr[i - 1] = arr[i]; ` `        ``arr[i] = aux; ` `    ``} ` `} ` ` `  `// Function to return the ` `// maximum number array ` `static` `void` `maximizeArray(``int``[] arr, ` `                ``int` `length, ``int` `swaps) ` `{ ` `    ``// Base condition ` `    ``if` `(swaps == 0) ` `        ``return``; ` ` `  `    ``// Start from the first index ` `    ``for` `(``int` `i = 0; i < length; i++)  ` `    ``{ ` `        ``int` `max_index = 0, max = ``int``.MinValue; ` ` `  `        ``// Search for the next K elements ` `        ``int` `limit = (swaps + i) > length ?  ` `                        ``length : swaps + i; ` ` `  `        ``// Find index of the maximum ` `        ``// element in next K elements ` `        ``for` `(``int` `j = i; j <= limit; j++)  ` `        ``{ ` `            ``if` `(arr[j] > max)  ` `            ``{ ` `                ``max = arr[j]; ` `                ``max_index = j; ` `            ``} ` `        ``} ` ` `  `        ``// Update the value of ` `        ``// number of swaps ` `        ``swaps -= (max_index - i); ` ` `  `        ``// Update the array elements by ` `        ``// swapping adjacent elements ` `        ``swapMax(arr, i, max_index); ` ` `  `        ``if` `(swaps == 0) ` `            ``break``; ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `[]arr = { 1, 2, 9, 8, 1, 4, 9, 9, 9 }; ` `    ``int` `length = arr.Length; ` `    ``int` `swaps = 4; ` `    ``maximizeArray(arr, length, swaps); ` ` `  `    ``print(arr, length); ` `} ` `} ` ` `  `/* This code is contributed by PrinciRaj1992 */`

Output:

```9 8 1 2 1 4 9 9 9
```

Time Complexity: O(N*N) where N is the length of given array

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 Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

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.

Improved By : princiraj1992, AnkitRai01

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.