-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPALIN.cpp
170 lines (155 loc) · 2.49 KB
/
PALIN.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<sstream>
using namespace std;
int main()
{
long int t;
scanf("%ld",&t);
while(t--)
{
long int k,n,i=0,j,mid,nd,reverseStart,number;
char array[1000001];
char copy[1000001];
scanf("%s",&array);
long int arraySize=0;
arraySize=sizeof(array)/sizeof(*array);
strncpy(copy,array,arraySize);
bool isEven = false;
bool isLarge=false;
while(array[i]!='\0')
i++;
//added the below code now!
//if(array[0]=='9' && array[1]=='\0')
//{
// printf("11\n");
// continue;
//}
//The code ends
std::istringstream(array)>>number;
j=0;
k=0;
while(array[k]!='\0')
{
if(array[k]=='9')
j++;
k++;
}
if(i==j)
{
printf("1");
for(j=0;j<(i-1);j++)
printf("0");
printf("1");
printf("\n");
continue;
}
if(i%2==0)
isEven=true;
if(isEven)
mid=i/2;
else
mid=ceil((i/2));
//printf("Mid i isEven: %ld,%ld,%d\n",mid,i,isEven);
i=mid-1;
if(!isEven)
reverseStart=mid+1;
else
reverseStart=mid;
while(array[reverseStart]!='\0')
array[reverseStart++]=array[i--];
//i=0;
//while(array[i]!='\0')
//printf("%c",array[i++]);
//printf("\n");
//copy has original number, array has the new palindrome number
i=0;
while(array[i]!='\0')
{
if(array[i]>copy[i])
{
isLarge=true;
break;
}
else
if(array[i]==copy[i])
i++;
else
break;
}
//printf("isLarge is: %d\n",isLarge);
//delete[] copy;
//printf("Mid is: %ld\n",mid);
long int pointer=0,middle;
i=0;
if(!isLarge)
{
if(isEven)
{
if(array[mid]=='9')
{
middle=mid;
while(array[mid]=='9')
{
array[mid]='0';
mid++;
i++;
}
array[mid--]++;
while(pointer<=i)
{
array[middle--]='0';
pointer++;
}
array[middle]++;
}
else
{
array[mid]++;
array[mid-1]++;
}
i=0;
while(array[i]!='\0')
printf("%c",array[i++]);
printf("\n");
}
else
{
if(array[mid]=='9')
{
middle=mid;
while(array[mid]=='9')
{
array[mid]='0';
mid++;
i++;
}
array[mid--]++;
while(pointer<i)
{
array[middle--]='0';
pointer++;
}
array[middle]++;
}
else
{
array[mid]++;
}
i=0;
while(array[i]!='\0')
printf("%c",array[i++]);
printf("\n");
}
}
else
{
i=0;
while(array[i]!='\0')
printf("%c",array[i++]);
printf("\n");
}
}
return 0;
}