xxxxxxxxxx
// Java program to find maximum
// integer possible by doing
// at-most K swap operations on
// its digits.
import java.io.*;
class Res {
static String max = "";
}
class Solution {
// Function to set highest possible digits at given
// index.
public static void findMaximumNum(char ar[], int k,
Res r)
{
if (k == 0)
return;
int n = ar.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// if digit at position i is less than digit
// at position j, we swap them and check for
// maximum number so far.
if (ar[j] > ar[i]) {
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
String st = new String(ar);
// if current number is more than
// maximum so far
if (r.max.compareTo(st) < 0) {
r.max = st;
}
// calling recursive function to set the
// next digit.
findMaximumNum(ar, k - 1, r);
// backtracking
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}
}
// Function to find the largest number after k swaps.
public static void main(String[] args)
{
String str = "129814999";
int k = 4;
Res r = new Res();
r.max = str;
findMaximumNum(str.toCharArray(), k, r);
//Print the answer stored in res class
System.out.println(r.max);
}
}
xxxxxxxxxx
class Solution
{
//Function to find the largest number after k swaps.
public static String findMaximumNum(String s, int k)
{
//code here.
char[] ch = s.toCharArray();
String str[]={s};
find(ch, k, str);
return str[0];
}
private static void find(char[] ch, int k, String[] str ) {
if(k==0) {
return;
}
for(int i=0;i<ch.length-1;i++) {
for(int j=i+1;j<ch.length;j++) {
if(ch[i]<ch[j]) {
char temp = ch[i];
ch[i]=ch[j];
ch[j]=temp;
if(String.valueOf(ch).compareTo(str[0])>0) {
str[0] = String.valueOf(ch);
}
find(ch,k-1,str);
// backtrack;
temp = ch[i];
ch[i]=ch[j];
ch[j]=temp;
}
}
}
}