-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathM0022.cpp
More file actions
45 lines (38 loc) · 797 Bytes
/
Copy pathM0022.cpp
File metadata and controls
45 lines (38 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Problem Statement: https://www.hackerrank.com/challenges/richie-rich/problem
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string highestValuePalindrome(string &s, int n, int k) {
int i, j;
vector<bool> changed(n);
for (i = 0, j = n - 1; i < n / 2; i++, j--)
if (s[i] != s[j]) {
if (k == 0)
return "-1";
s[i] = s[j] = max(s[i], s[j]);
changed[i] = changed[j] = true;
k--;
}
for (i = 0, j = n - 1; i <= n / 2 && k != 0; i++, j--) {
if (s[i] == '9' && s[j] == '9')
continue;
else if (changed[i] || i == j) {
s[i] = s[j] = '9';
k--;
} else if (k > 1) {
s[i] = s[j] = '9';
k -= 2;
}
}
return s;
}
int main() {
int n, k;
string s;
cin >> n >> k >> s;
cout << highestValuePalindrome(s, n, k);
return 0;
}