-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
34 lines (28 loc) · 867 Bytes
/
Copy pathsolution.cpp
File metadata and controls
34 lines (28 loc) · 867 Bytes
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
class Solution
{
public:
int isPallindrome(long long int N)
{
// Find the position of the most significant bit (MSB)
int left = 63; // assuming 64-bit number
// Move left pointer to the first '1' bit
while (left >= 0 && ((N >> left) & 1) == 0)
{
left--;
}
int right = 0; // least significant bit
// Compare bits from both ends
while (left > right)
{
int leftBit = (N >> left) & 1; // extract left bit
int rightBit = (N >> right) & 1; // extract right bit
// If mismatch found, not a palindrome
if (leftBit != rightBit)
return 0;
// Move pointers inward
left--;
right++;
}
return 1; // all bits matched
}
};