Problem Statement:
Given an integer “n”, you need to check if the number is palindrome or not.
A number when reversed, will remains the same.
Example:
Input:
n = 12321
Output: Yes
Solution Explanation:
Solution is very simple.
Call a recursive function and then reverse the number using recursion.
Then in the main function, check if the original number is same as reversed number and return the result.
Time Complexity: O(log n) number of digits that needs to reverse
Space Complexity: O(log n) for stack space
Code Solution
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
int revrse_num(int n, int temp)
{
if (n == 0)
return temp;
temp = (temp * 10) + (n % 10);
return revrse_num(n / 10, temp);
}
int main()
{
int n = 12321;
int temp = revrse_num(n, 0);
if (temp == n)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
Output
Yes