-
Notifications
You must be signed in to change notification settings - Fork 1
/
54-next-higher-palindromic-number-using-the-same-set-of-digits.cpp
83 lines (72 loc) · 1.71 KB
/
54-next-higher-palindromic-number-using-the-same-set-of-digits.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
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
string nextPalin(string str)
{
string ans = "";
vector<int> vec;
int l = 0, r = str.size() - 1;
while(l < r)
{
vec.push_back(str[l] - '0');
l++;
r--;
}
int n = vec.size();
if(n > 1)
{
int i = 1;
int lastInc = -1;
while(i < n)
{
if(vec[i] > vec[i - 1])
{
lastInc = i;
}
i++;
}
if(lastInc == -1)
return "-1";
int val = vec[lastInc];//5
int index = lastInc;//1
for(i = lastInc; i < n; ++i)
{
if(vec[i] > vec[lastInc - 1] && vec[i] < vec[index])
index = i;
}
int temp = vec[index];
vec[index] = vec[lastInc - 1];
vec[lastInc - 1] = temp;
sort(vec.begin() + lastInc, vec.end());
}
else
return "-1";
for(int i = 0; i < n; ++i)
{
ans += to_string(vec[i]);
}
if(l == r)
ans += str[l];
for(int i = (n - 1); i >= 0; --i)
{
ans += to_string(vec[i]);
}
return ans;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
string s;
cin >> s;
Solution obj;
cout << obj.nextPalin(s) << endl;
}
return 0;
} // } Driver Code Ends