Minimum adjacent swaps required to get Kth smallest number greater than given number
Last Updated :
19 May, 2022
Given numeric string S of size N and a positive integer K, the task is to find the minimum number of adjacent swaps required in S to obtain the Kth smallest numeric string greater than the given string.
Examples:
Input: S = “11112”, K = 4
Output: 4
Explanation:
The Kth(= 4th) smallest numeric string which is greater than the given string is “21111”. The sequence of adjacent swap to reach the string “21111” is “11112” -> “11121” -> “11211″ -> “12111″ -> “21111″.
Therefore, the minimum number of adjacent swaps required is 4.
Input: S = “12345”, K = 1
Output: 1
Approach: The given problem can be solved by using the Greedy Approach. Follow the steps below to solve the problem:
- Store the copy of the current numeric string in a variable, say res.
- Create a variable, say totalSwaps that stores the minimum swaps required.
- Since K-th largest number is required, this statement is equal to finding K-th permutation starting from the current string.
- Find the K-th permutation using the function next_permutation().
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- If res[i] is not equal to str[i] then initialize the variable start as i+1 and traverse over a while loop till res[i] is not equal to str[start] and increase the value of i by 1.
- Iterate a loop till i is not equal to start and swap the values str[start] and str[start – 1] and decrease the value of start by 1 and increase the value of totalSwaps by 1.
- After performing the above steps, print the value of totalSwaps as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minSwapsKthLargest(string str, int k)
{
string res = str;
for ( int i = 0; i < k; i++) {
next_permutation(
str.begin(), str.end());
cout<< "str = " <<str<<endl;
}
int swap_count = 0;
for ( int i = 0; i < res.length(); i++) {
if (res[i] != str[i]) {
int start = i + 1;
while (res[i] != str[start]) {
start++;
}
while (i != start) {
swap(str[start], str[start - 1]);
start--;
swap_count++;
}
}
}
cout << swap_count;
}
int main()
{
string S = "11112" ;
int K = 4;
minSwapsKthLargest(S, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static char [] next_permutation( char [] array) {
int i = array.length - 1 ;
while (i > 0 && array[i - 1 ] >= array[i]) {
i--;
}
int j = array.length - 1 ;
while (array[j] <= array[i - 1 ]) {
j--;
}
char temp = array[i - 1 ];
array[i - 1 ] = array[j];
array[j] = temp;
j = array.length - 1 ;
while (i < j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
return array;
}
static void minSwapsKthLargest(String str, int k)
{
char [] res = str.toCharArray();
char [] s = str.toCharArray();
for ( int i = 0 ; i < k; i++) {
s = next_permutation(s);
}
int swap_count = 0 ;
for ( int i = 0 ; i < res.length; i++) {
if (res[i] != s[i]) {
int start = i + 1 ;
while (res[i] != s[start]) {
start++;
}
while (i != start) {
char t = s[start];
s[start] = s[start - 1 ];
s[start - 1 ] = t;
start--;
swap_count++;
}
}
}
System.out.print(swap_count);
}
public static void main(String[] args) {
String S = "11112" ;
int K = 4 ;
minSwapsKthLargest(S, K);
}
}
|
Python3
def next_permutation(array):
i = len (array) - 1
while (i > 0 and ord (array[i - 1 ]) > = ord (array[i])):
i - = 1
if (i < = 0 ):
return False
j = len (array) - 1
while ( ord (array[j]) < = ord (array[i - 1 ])):
j - = 1
array[j],array[i - 1 ] = array[i - 1 ],array[j]
j = len (array) - 1
while (i < j):
array[i],array[j] = array[j],array[i]
i + = 1
j - = 1
return array
def minSwapsKthLargest( Str , k):
res = list ( Str )
Str = list ( Str )
for i in range (k):
next_permutation( Str )
swap_count = 0
for i in range ( len (res)):
if (res[i] ! = Str [i]):
start = i + 1
while (res[i] ! = Str [start]):
start + = 1
while (i ! = start):
Str [start], Str [start - 1 ] = Str [start - 1 ], Str [start]
start - = 1
swap_count + = 1
print (swap_count)
S = "11112"
K = 4
minSwapsKthLargest(S, K)
|
C#
using System;
class GFG {
static char [] next_permutation( char [] array)
{
int i = array.Length - 1;
while (i > 0 && array[i - 1] >= array[i]) {
i--;
}
int j = array.Length - 1;
while (array[j] <= array[i - 1]) {
j--;
}
char temp = array[i - 1];
array[i - 1] = array[j];
array[j] = temp;
j = array.Length - 1;
while (i < j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
return array;
}
static void minSwapsKthLargest( string str, int k)
{
string res = str;
char [] str1 = str.ToCharArray();
for ( int i = 0; i < k; i++) {
next_permutation(str1);
}
int swap_count = 0;
for ( int i = 0; i < res.Length; i++) {
if (res[i] != str1[i]) {
int start = i + 1;
while (res[i] != str1[start]) {
start++;
}
while (i != start) {
char temp = str1[start];
str1[start] = str1[start - 1];
str1[start - 1] = temp;
start--;
swap_count++;
}
}
}
Console.WriteLine(swap_count);
}
public static void Main()
{
string S = "11112" ;
int K = 4;
minSwapsKthLargest(S, K);
}
}
|
Javascript
<script>
function next_permutation(array)
{
var i = array.length - 1;
while (i > 0 && array[i - 1].charCodeAt(0) >= array[i].charCodeAt(0)) {
i--;
}
if (i <= 0) {
return false ;
}
var j = array.length - 1;
while (array[j].charCodeAt(0) <= array[i - 1].charCodeAt(0)) {
j--;
}
var temp = array[i - 1];
array[i - 1] = array[j];
array[j] = temp;
j = array.length - 1;
while (i < j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
return array;
}
function minSwapsKthLargest(str, k)
{
let res = str.split( '' );
str = str.split( '' )
for (let i = 0; i < k; i++) {
next_permutation(
str);
}
let swap_count = 0;
for (let i = 0; i < res.length; i++) {
if (res[i] != str[i]) {
let start = i + 1;
while (res[i] != str[start]) {
start++;
}
while (i != start) {
let temp = str[start];
str[start] = str[start - 1]
str[start - 1] = temp;
start--;
swap_count++;
}
}
}
document.write(swap_count);
}
let S = "11112" ;
let K = 4;
minSwapsKthLargest(S, K);
</script>
|
Time Complexity: O(N*(N + K))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...