|
| 1 | +# Palindrome (L-I) |
| 2 | + |
| 3 | +## Problem |
| 4 | +- Write a function in JavaScript that takes an integer as input and returns a boolean value indicating whether the number is a palindrome or not. |
| 5 | + |
| 6 | +- A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 1221, 12321 are all palindrome numbers. The function should return true if the given number is a palindrome, and false otherwise. |
| 7 | + |
| 8 | +- The function should be able to handle negative numbers and should not convert the number to a string or array. Instead, it should perform the check by manipulating the number directly. |
| 9 | + |
| 10 | +- Example Inputs and Outputs: |
| 11 | + |
| 12 | +- Input: 121 -> Output: true |
| 13 | +- Input: -121 -> Output: false |
| 14 | +- Input: 10 -> Output: false |
| 15 | +- Input: 12321 -> Output: true |
| 16 | + |
| 17 | +## Solution |
| 18 | + |
| 19 | +```javascript |
| 20 | +let n = 12321; |
| 21 | + |
| 22 | +if(palindrome(n)){ |
| 23 | + console.log( n + " is a Palindrome number"); |
| 24 | +}else{ |
| 25 | + console.log( n + " is not a Palindrome number"); |
| 26 | +} |
| 27 | + |
| 28 | +/* function to check if a number is palindrome or not |
| 29 | +by reversing the number and comparing it with the original number */ |
| 30 | +function palindrome(n){ |
| 31 | + // create a temp variable to store the value of n |
| 32 | + let temp = n; |
| 33 | + // create a variable to store the reverse of n |
| 34 | + let rev = 0; |
| 35 | + // reverse the number |
| 36 | + while(temp > 0){ |
| 37 | + let lastDigit = temp % 10; |
| 38 | + rev = rev * 10 + lastDigit; |
| 39 | + temp = Math.floor(temp / 10); |
| 40 | + } |
| 41 | + // check if the reverse is equal to n |
| 42 | + return rev == n; |
| 43 | +} |
| 44 | + |
| 45 | +// using two pointers |
| 46 | +function palindromeTwoPointer(n){ |
| 47 | + // convert the number to string |
| 48 | + let str = n.toString(); |
| 49 | + // if the number is negative, remove the negative sign from the string |
| 50 | + if (str[0] === '-') { |
| 51 | + return false; |
| 52 | + } |
| 53 | + // create two pointers |
| 54 | + let start = 0; |
| 55 | + let end = str.length - 1; |
| 56 | + // loop through the string |
| 57 | + while(start < end){ |
| 58 | + // check if the characters at the two pointers are equal |
| 59 | + if(str[start] != str[end]){ |
| 60 | + return false; |
| 61 | + } |
| 62 | + // increment start and decrement end |
| 63 | + start++; |
| 64 | + end--; |
| 65 | + } |
| 66 | + return true; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## How it works |
| 71 | +### First Approach : |
| 72 | +- Create a variable temp to store the value of n for later use. |
| 73 | +- Create a variable rev to store the reverse of n. rev is initially set to 0. |
| 74 | +- Use a while loop to reverse the number n. The loop continues as long as temp is greater than 0. |
| 75 | +- a. In each iteration, the last digit of temp is calculated by taking the remainder when temp is divided by 10. |
| 76 | + b. The last digit is then added to rev by multiplying rev by 10 and adding the last digit. |
| 77 | + c. The value of temp is then updated by dividing it by 10 and flooring the result. This discards the last digit of temp. |
| 78 | +- After the while loop, the function checks if the reversed number rev is equal to the original number n. |
| 79 | +- If rev is equal to n, the function returns true, indicating that n is a palindrome. Otherwise, the function returns false. |
| 80 | + |
| 81 | +### Second Approach : |
| 82 | +- First, the number is converted to a string. |
| 83 | +- If the first character of the string is a negative sign (-), the function returns false as negative numbers cannot be palindromes. |
| 84 | +- Two pointers are created, start and end, and initialized to the first and last characters of the string, respectively. |
| 85 | +- The function then enters a loop, which continues as long as start is less than end. |
| 86 | +- Within the loop, the characters at the start and end pointers are compared. If they are not equal, the function returns false as the number is not a palindrome. |
| 87 | +- If the characters are equal, start is incremented and end is decremented, and the loop continues. |
| 88 | +- When the loop is finished, the function returns true, indicating that the number is a palindrome. |
| 89 | + |
| 90 | + |
| 91 | +## References |
| 92 | +- [Wikipedia](https://en.wikipedia.org/wiki/Palindromic_number#) |
| 93 | + |
| 94 | + |
| 95 | +## Problem Added By |
| 96 | +- [GitHub](https://www.github.com/hirentimbadiya) |
| 97 | +- [LinkedIn](https://www.linkedin.com/in/hirentimbadiya74) |
| 98 | +- [Twitter](https://twitter.com/heyhiru) |
| 99 | + |
| 100 | + |
| 101 | +## Contributing |
| 102 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 103 | + |
| 104 | +Please make sure to update tests as appropriate. |
0 commit comments