-
Notifications
You must be signed in to change notification settings - Fork 3
/
nextSmallestPalindrome.cpp
163 lines (136 loc) · 3.43 KB
/
nextSmallestPalindrome.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
#include <iostream>
#include <math.h>
#define ll long long int
using namespace std;
/* Case 1 : Even Length Digit.
-> Middle Left Digit > Middle Right Digit.
-> Middle Left Digit < Middle Right Digit.
Case 2 : Odd Length Digit.
-> Digit[middle - 1] > Digit[middle + 1]
-> Digit[middle - 1] < Digit[middle + 1]
*/
// Return the length of the Number.
ll getLength(ll n)
{
ll count = 0;
while (n > 0)
{
count++;
n /= 10;
}
return count;
}
ll getNextSmallestPal(ll n)
{
ll temp = 0;
int t;
ll dup;
ll len = getLength(n);
ll first;
// Divide the Number into Two Parts and store it in "temp"..
for (t = 0; t < (len / 2); t++)
{
temp += (n % 10) * pow(10, t);
n /= 10;
}
// Get the first element of temp.
first = temp / pow(10, (len / 2) - 1);
// If the length of the digit is even
if (len % 2 == 0)
{
// if "Middle left Digit" is greater than the "Middle Right Digit".
if (n % 10 > first)
{
ll overrider = 0;
dup = n;
// Reverse the first half and add it to the second half.
for (t = 0; t < (len / 2); t++)
{
overrider += (n % 10) * pow(10, (len / 2) - t - 1);
n /= 10;
}
dup *= pow(10, len / 2);
dup += overrider;
}
// if "Middle Left Digit" is lesser than "Middle Right Digit".
else if (n % 10 < first)
{
// Add one to the first part.
n += 1;
ll overrider = 0;
dup = n;
// Reverse the first half and add it to the second half.
for (t = 0; t < (len / 2); t++)
{
overrider += (n % 10) * pow(10, (len / 2) - t - 1);
n /= 10;
}
// Overriding reversed first part to the second part and then merging both parts.
dup *= pow(10, len / 2);
dup += overrider;
}
}
// If the length of the digit is ODD.
else
{
// get the last digit of the first digit of the number.
ll mid = n % 10;
dup = n;
// Remove the last digit of n.
n /= 10;
// if Digit[middle - 1] > Digit[middle + 1]
if (n % 10 > first)
{
ll overrider = 0;
// Reverse the first half and then override it to the second half..
for (t = 0; t < (len / 2); t++)
{
overrider += (n % 10) * pow(10, (len / 2) - t - 1);
n /= 10;
}
// Overriding reversed first part to the second part and then merging both parts.
dup *= pow(10, len / 2);
dup += overrider;
}
// Digit[middle - 1] < Digit[middle + 1]
else
{
ll overrider = 0;
// Reverse the first half and then override it to the second half..
for (t = 0; t < (len / 2); t++)
{
overrider += (n % 10) * pow(10, (len / 2) - t - 1);
n /= 10;
}
// Overriding reversed first part to the second part and then merging both parts.
dup *= pow(10, len / 2);
dup += overrider;
// Adding 1 to the Middle Element.
dup += pow(10, len / 2);
}
}
return dup;
}
// Checks if the every digit of the Number contains 9.
bool checkAllNines(ll n)
{
while (n > 0)
{
if (n % 10 == 9)
{
n /= 10;
continue;
}
else
break;
n /= 10;
}
return (n == 0) ? true : false;
}
int main()
{
ll n;
cin >> n;
// If checkAllNines() returns "true", output the "number+2". Else, calculate the Next Smallest Palindrome.
(checkAllNines(n)) ? cout << (n + 2) << '\n' : cout << getNextSmallestPal(n) << '\n';
}