Next Smallest Palindrome | Practice | GeeksforGeeks

Given a number, in the form of an array Num[] of size N containing digits from 1 to 9(inclusive). The task is to find the next smallest palindrome strictly larger than the given number.
Example 1:
Input:
N = 11
Num[] = {9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2}
Output: 9 4 1 8 8 0 8 8 1 4 9
Explanation: Next smallest palindrome is
94188088149.

Example 2:
Input:
N = 5
Num[] = {2, 3, 5, 4, 5}
Output: 2 3 6 3 2
Explanation: Next smallest palindrome is
23632.

Your Task:
Complete the function generateNextPalindrome() which takes an array num, and an single integer n, as input parameters and returns an array of integers denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
1 <= Num[i] <= 9
 
0
gauravkumar2015 days ago
class Solution {
public:
    string makePalindrome(string in) {
        int a = 0;
        int b = in.size() - 1;
        while (a < b) {
            in[b] = in[a];
            a++;
            b--;
        }
        return in;
    }

    string solve(string A) {
        int n = A.size();
        string temp = makePalindrome(A);
        if (temp > A) return temp;
        int carry = 1;
        for (int a = (n - 1) / 2; a >= 0; a--) {
            int t = A[a] - '0' + carry;
            if (t == 10) {
                t = 0;
                carry = 1;
            } else carry = 0;
            A[a] = '0' + t;
        }
        string res;
        if (carry == 1) res = "1";
        res += A;
        return makePalindrome(res);
    }

    vector<int> generateNextPalindrome(int num[], int n) {
        // code here
        string A;
        for (int i = 0; i < n; i++)A += num[i] + '0';
        string res = solve(A);

        vector<int> ans;
        for (auto i: res)ans.push_back(i - '0');
        return ans;
    }

};
+1
deskhell4 weeks ago
class Solution {
public:
    string makePalindrome(string in) {
        int a = 0;
        int b = in.size() - 1;
        while (a < b) {
            in[b] = in[a];
            a++;
            b--;
        }
        return in;
    }

    string solve(string A) {
        int n = A.size();
        string temp = makePalindrome(A);
        if (temp > A) return temp;
        int carry = 1;
        for (int a = (n - 1) / 2; a >= 0; a--) {
            int t = A[a] - '0' + carry;
            if (t == 10) {
                t = 0;
                carry = 1;
            } else carry = 0;
            A[a] = '0' + t;
        }
        string res;
        if (carry == 1) res = "1";
        res += A;
        return makePalindrome(res);
    }

    vector<int> generateNextPalindrome(int num[], int n) {
        // code here
        string A;
        for (int i = 0; i < n; i++)A += num[i] + '0';
        string res = solve(A);

        vector<int> ans;
        for (auto i: res)ans.push_back(i - '0');
        return ans;
    }

};
0
mayanksahu2210
This comment was deleted.
+1
rohansarna0712014 months ago
vector<int> generateNextPalindrome(int arr[], int n) {
	    if(n ==1){
	        if(arr[0] >= 9 )
	            return {1 , 1 }; 
	       
	       return{arr[0] +=1};       
	    }

    vector<int>num; 
    for(int i = 0 ; i<n ; i++){
        num.push_back(arr[i]); 
    }
    
    // Find the index of mid digit
    int mid = n / 2;

    // A bool variable to check if copy of left
    // side to right is sufficient or not
    bool leftsmaller = false;

    // End of left side is always 'mid -1'
    int i = mid - 1;

    // Beginning of right side depends
    // if n is odd or even
    int j = (n % 2) ? mid + 1 : mid;

   // Initially, ignore the middle same digits 
    while (i >= 0 && num[i] == num[j])
        i--, j++;

    // Find if the middle digit(s) need to be
    // incremented or not (or copying left 
    // side is not sufficient)
    if (i < 0 || num[i] < num[j])
        leftsmaller = true;

    // Copy the mirror of left to tight
    while (i >= 0)
    {
        num[j] = num[i];
        j++;
        i--;
    }

    // Handle the case where middle digit(s) must
    // be incremented. This part of code is for 
    // CASE 1 and CASE 2.2
    if (leftsmaller == true)
    {
        int carry = 1;
        i = mid - 1;

        // If there are odd digits, then increment
        // the middle digit and store the carry
        if (n % 2 == 1)
        {
            num[mid] += carry;
            carry = num[mid] / 10;
            num[mid] %= 10;
            j = mid + 1;
        }
        else
            j = mid;


        while (i >= 0)
        {
            num[i] += carry;
            carry = num[i] / 10;
            num[i] %= 10;
            
            // Copy mirror to right
            num[j++] = num[i--]; 
        }

    }
        return num  ;
	    
	}

-1
aayush119130884 months ago
class Solution{public:   // vector<int> part(vector<int> v){   //     int n=v.size();          // }    // 12340=>12421   // 12307=>12321   // 1240 1331   // 1207=>1221   // 12940=>13031   string greater(int a[],int n){       int k=n/2;       int m=k,j;       if(n%2!=0){           int j;           for(int i=k-1,j=m+1;i>=0&&j<n;i--,j++){               if(a[i]==a[j]){                   continue;               }else if(a[i]>a[j]){                   return "left";               }else{                   return "right";               }           }       }else{           int j;           for(int i=k-1,j=m;i>=0&&j<n;i--,j++){               if(a[i]==a[j]){                   continue;               }else if(a[i]>a[j]){                   return "left";               }else{                   return "right";               }           }                  }       return "equal";   }   void sum(int a[],int n,int i,int ic){       int j=i;       int c=0;       a[j]+=ic;       while(j>=0){           int t=a[j];           a[j]=t%10+c;           c=t/10;           j--;       }   }    void mirror(int a[],int n){       int i=0,j=n-1;       while(i<j){           a[j]=a[i];           i++;           j--;       }   }vector<int> generateNextPalindrome(int ans[], int n) {   // string s"";   if(n==1){       if(ans[0]>9){           return {ans[0]+1};       }else if(ans[0]==9){           return {1,1};       }   }   bool f=true;   for(int i=0;i<n;i++){   if(ans[i]!=9){       f=false;       break;   }   }   if(f){       sum(ans,n,n-1,2);        vector<int> v;     for(int i=0;i<n;i++){        v.push_back(ans[i]);    }    return v;   }       // v.push_back(ans[i]);            string flag=greater(ans,n);    if(flag=="left"){       // if(a%2===0){            mirror(ans,n);           // return ans;    }else {        if(n%2==0){            sum(ans,n,(n/2)-1,1);              mirror(ans,n);        }else{              sum(ans,n,(n/2),1);              mirror(ans,n);        }            }    vector<int> v;     for(int i=0;i<n;i++){        v.push_back(ans[i]);    }    return v;    }
};
by aayush'
0
abhinavbansal05
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.