-
Notifications
You must be signed in to change notification settings - Fork 617
/
palindrome_number.cpp
63 lines (56 loc) · 1.61 KB
/
palindrome_number.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
/* Palindrome Number :
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
This code is implemented without using extra space.
In this we will be taking two point , leading and trailing which will be pointing
in start and end of the number and then we will be comparing start and end , if
found equal then we will move the pointers and we will compare till every digit is
checked . So if condition satisfied then we will return true else return false.
*/
#include <bits/stdc++.h>
using namespace std;
bool checkPalindrome(int num)
{
// divi = divisor
int divi = 1;
//finding the apt. divisor for the number
while (num / divi >= 10)
{
divi *= 10;
}
while (num != 0)
{
int leading = num / divi;
int trailing = num % 10;
if (leading != trailing)
{
return false;
}
//reducing number to compare
num = (num % divi) / 10;
divi = divi / 100;
}
return true;
}
int main()
{
int num;
cout << "Enter the number to check : " << endl;
cin >> num;
checkPalindrome(n) ? cout << "True" : cout << "False";
return 0;
}
/*
Time complexity : O(log n)
(here n is number of elements in array)
Space complexity : O(1)
*/
/*
Test Cases :
Test case 1 :
Input : 1001
Output : True
Test case 2 :
Input : 1234
Output : False
*/