fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Function to delete character at position i in the string
  6. string delpos(int i, string s) {
  7. s.erase(i, 1);
  8. return s;
  9. }
  10.  
  11. int main() {
  12. string s;
  13. int k, count = 0;
  14. cin >> s >> k;
  15.  
  16. // Traverse the string from the beginning to the end
  17. for (int i = 0; i < s.size() - 1; i++) {
  18. if (s[i] < s[i + 1] && count < k) {
  19. s = delpos(i, s);
  20. count++;
  21. i--; // Adjust index after deletion
  22. }
  23. }
  24.  
  25. // Traverse the string from the end to the beginning
  26. for (int i = s.size() - 1; i >= 0 && count < k; i--) {
  27. s = delpos(i, s);
  28. count++;
  29. }
  30.  
  31. cout << s << endl;
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5284KB
stdin
123456785431 2
stdout
3456785431