Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

Given a number **K **and string **str **of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of **str **at most **K **times.

**Example 1:**

**Input: **K = 4, str = 1234567
**Output:**7654321**
Explanation: **Three swaps can make the
input 1234567 to 7654321, swapping 1
with 7, 2 with 6 and finally 3 with 5

**Example 2:**

**Input: **K = 3, str = 3435335
**Output: **5543333
**Explanation: **Three swaps can make the
input 3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4

**Your task:**

Since this is a functional problem you don't have to worry about input, you just have to complete the function **findMaximumNum(). **There will be three arguments which will be provided. They are as such.

**For Cpp**: arguments - findMaximumNum(str, k, max) (str: string input, k: number, max: will contain result)

**For Java**: arguments - findMaximumNum(ar[], k, max) (ar[]: character array of input, k: number, max: will contain result).

You have to compute the result and store it in the "**res**" variable. The result will be printed by the driver code.

**Expected Time Complexity:** O( n! / (n-k)! ) , where n = length of input string

**Expected Auxiliary Space:** O(n)

**Constraints**:

1 ≤ **|str| **≤ 30

1 ≤ **K **≤ 10

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Largest number in K swaps

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.