Largest lexicographic array with at-most K consecutive swaps
Last Updated :
09 Aug, 2022
Given an array arr[], find the lexicographically largest array that can be obtained by performing at-most k consecutive swaps.
Examples :
Input : arr[] = {3, 5, 4, 1, 2}
k = 3
Output : 5, 4, 3, 2, 1
Explanation : Array given : 3 5 4 1 2
After swap 1 : 5 3 4 1 2
After swap 2 : 5 4 3 1 2
After swap 3 : 5 4 3 2 1
Input : arr[] = {3, 5, 1, 2, 1}
k = 3
Output : 5, 3, 2, 1, 1
Brute Force Approach : Generate all permutation of the array and then pick the one which satisfies the condition of at most K swaps. The time complexity of this approach is O(n!).
Optimized Approach : In this greedy approach, first find the largest element present in the array which is greater than(if the 1st position element is not the greatest) the 1st position and which can be placed at the 1st position with at-most K swaps. After finding that element, note its index. Then, swap elements of the array and update K value. Apply this procedure for other positions till k is non-zero or array becomes lexicographically largest.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void KSwapMaximum( int arr[], int n, int k)
{
for ( int i = 0; i < n - 1 && k > 0; ++i) {
int indexPosition = i;
for ( int j = i + 1; j < n; ++j) {
if (k <= j - i)
break ;
if (arr[j] > arr[indexPosition])
indexPosition = j;
}
for ( int j = indexPosition; j > i; --j)
swap(arr[j], arr[j - 1]);
k -= indexPosition - i;
}
}
int main()
{
int arr[] = { 3, 5, 4, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 3;
KSwapMaximum(arr, n, k);
for ( int i = 0; i < n; ++i)
cout << arr[i] << " " ;
}
|
Java
import java.io.*;
class GFG
{
static void SwapInts( int array[],
int position1,
int position2)
{
int temp = array[position1];
array[position1] = array[position2];
array[position2] = temp;
}
static void KSwapMaximum( int []arr,
int n, int k)
{
for ( int i = 0 ;
i < n - 1 && k > 0 ; ++i)
{
int indexPosition = i;
for ( int j = i + 1 ; j < n; ++j)
{
if (k <= j - i)
break ;
if (arr[j] > arr[indexPosition])
indexPosition = j;
}
for ( int j = indexPosition; j > i; --j)
SwapInts(arr, j, j - 1 );
k -= indexPosition - i;
}
}
public static void main(String args[])
{
int []arr = { 3 , 5 , 4 , 1 , 2 };
int n = arr.length;
int k = 3 ;
KSwapMaximum(arr, n, k);
for ( int i = 0 ; i < n; ++i)
System.out.print(arr[i] + " " );
}
}
|
Python3
arr = [ 3 , 5 , 4 , 1 , 2 ]
def KSwapMaximum(n, k) :
global arr
for i in range ( 0 , n - 1 ) :
if (k > 0 ) :
indexPosition = i
for j in range (i + 1 , n) :
if (k < = j - i) :
break
if (arr[j] > arr[indexPosition]) :
indexPosition = j
for j in range (indexPosition, i, - 1 ) :
t = arr[j]
arr[j] = arr[j - 1 ]
arr[j - 1 ] = t
k = k - indexPosition - i
n = len (arr)
k = 3
KSwapMaximum(n, k)
for i in range ( 0 , n) :
print ( "{} " .
format (arr[i]),
end = "")
|
C#
using System;
class GFG
{
static void SwapInts( int [] array,
int position1,
int position2)
{
int temp = array[position1];
array[position1] = array[position2];
array[position2] = temp;
}
static void KSwapMaximum( int []arr,
int n, int k)
{
for ( int i = 0;
i < n - 1 && k > 0; ++i)
{
int indexPosition = i;
for ( int j = i + 1; j < n; ++j)
{
if (k <= j - i)
break ;
if (arr[j] > arr[indexPosition])
indexPosition = j;
}
for ( int j = indexPosition; j > i; --j)
SwapInts(arr, j, j - 1);
k -= indexPosition - i;
}
}
static void Main()
{
int []arr = new int []{ 3, 5, 4, 1, 2 };
int n = arr.Length;
int k = 3;
KSwapMaximum(arr, n, k);
for ( int i = 0; i < n; ++i)
Console.Write(arr[i] + " " );
}
}
|
PHP
<?php
function swap(& $x , & $y )
{
$x ^= $y ^= $x ^= $y ;
}
function KSwapMaximum(& $arr , $n , $k )
{
for ( $i = 0;
$i < $n - 1 &&
$k > 0; $i ++)
{
$indexPosition = $i ;
for ( $j = $i + 1;
$j < $n ; $j ++)
{
if ( $k <= $j - $i )
break ;
if ( $arr [ $j ] > $arr [ $indexPosition ])
$indexPosition = $j ;
}
for ( $j = $indexPosition ;
$j > $i ; $j --)
swap( $arr [ $j ], $arr [ $j - 1]);
$k -= $indexPosition - $i ;
}
}
$arr = array ( 3, 5, 4, 1, 2 );
$n = count ( $arr );
$k = 3;
KSwapMaximum( $arr , $n , $k );
for ( $i = 0; $i < $n ; $i ++)
echo ( $arr [ $i ]. " " );
?>
|
Javascript
<script>
function SwapLets(array, position1, position2)
{
let temp = array[position1];
array[position1] = array[position2];
array[position2] = temp;
}
function KSwapMaximum(arr, n, k)
{
for (let i = 0;
i < n - 1 && k > 0; ++i)
{
let indexPosition = i;
for (let j = i + 1; j < n; ++j)
{
if (k <= j - i)
break ;
if (arr[j] > arr[indexPosition])
indexPosition = j;
}
for (let j = indexPosition; j > i; --j)
SwapLets(arr, j, j - 1);
k -= indexPosition - i;
}
}
let arr = [ 3, 5, 4, 1, 2 ];
let n = arr.length;
let k = 3;
KSwapMaximum(arr, n, k);
for (let i = 0; i < n; ++i)
document.write(arr[i] + " " );
</script>
|
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...