-
Notifications
You must be signed in to change notification settings - Fork 1
/
HighValPalin.cpp
80 lines (67 loc) · 1.43 KB
/
HighValPalin.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// problem: "https://www.hackerrank.com/challenges/richie-rich/problem"
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
long long int i,c,k,n,s,x;
int a[100000];
char num[100000];
cin>>n>>k;
cin>>num;
s=strlen(num);
for(i=0;i<s;i++)
a[i]=0;
c=0;
for(i=0;i<=s/2;i++)
{
if(num[i]>num[s-1-i])
{
num[s-1-i]=num[i];
c++;
a[i]=-1;
}
else if(num[i]<num[s-1-i])
{
num[i]=num[s-1-i];
c++;
a[i]=-1;
}
}
if(c>k)
cout<<"-1"<<endl;
else if(c==k)
puts(num);
else
{
i=0; x=s;
while((c<k)&&(i<=(s+1)/2))
{
if(num[i]!='9')
{
if((a[i]==-1)||(a[s-1-i]==-1))
{
c--;
num[i]=num[s-i-1]='9';
c=c+2;
}
else if((a[i]==0)&&(a[s-1-i]==0))
{
if(c+2<=k)
{
num[i]=num[s-i-1]='9';
c=c+2;
}
else if((i==(s+1)/2-1)&&((s+1)%2==0))
{
num[i]='9';
c++;
}
}
}
i++;
}
puts(num);
}
return 0;
}