From eb4b3235aac04288eae2df94851fb427c0ef6f9f Mon Sep 17 00:00:00 2001 From: heyhiru Date: Sat, 4 Feb 2023 16:31:32 +0530 Subject: [PATCH 1/3] added a palindrome.js file --- 0001 Palindrome ( L - I)/palindrome.js | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0001 Palindrome ( L - I)/palindrome.js diff --git a/0001 Palindrome ( L - I)/palindrome.js b/0001 Palindrome ( L - I)/palindrome.js new file mode 100644 index 0000000..9ea8941 --- /dev/null +++ b/0001 Palindrome ( L - I)/palindrome.js @@ -0,0 +1,44 @@ +let n = 12321; + +if(palindrome(n)){ + console.log( n + " is a Palindrome number"); +}else{ + console.log( n + " is not a Palindrome number"); +} + +/* function to check if a number is palindrome or not +by reversing the number and comparing it with the original number */ +function palindrome(n){ + // create a temp variable to store the value of n + let temp = n; + // create a variable to store the reverse of n + let rev = 0; + // reverse the number + while(temp > 0){ + let lastDigit = temp % 10; + rev = rev * 10 + lastDigit; + temp = Math.floor(temp / 10); + } + // check if the reverse is equal to n + return rev == n; +} + +// using two pointers +function palindromeTwoPointer(n){ + // convert the number to string + let str = n.toString(); + // create two pointers + let start = 0; + let end = str.length - 1; + // loop through the string + while(start < end){ + // check if the characters at the two pointers are equal + if(str[start] != str[end]){ + return false; + } + // increment start and decrement end + start++; + end--; + } + return true; +} \ No newline at end of file From cdc2891506661552221d7034ebdd618316017aa1 Mon Sep 17 00:00:00 2001 From: heyhiru Date: Sat, 4 Feb 2023 16:46:45 +0530 Subject: [PATCH 2/3] added some logic --- 0001 Palindrome ( L - I)/palindrome.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/0001 Palindrome ( L - I)/palindrome.js b/0001 Palindrome ( L - I)/palindrome.js index 9ea8941..45019ea 100644 --- a/0001 Palindrome ( L - I)/palindrome.js +++ b/0001 Palindrome ( L - I)/palindrome.js @@ -1,4 +1,4 @@ -let n = 12321; +let n = -12321; if(palindrome(n)){ console.log( n + " is a Palindrome number"); @@ -27,6 +27,10 @@ function palindrome(n){ function palindromeTwoPointer(n){ // convert the number to string let str = n.toString(); + // if the number is negative, remove the negative sign from the string + if (str[0] === '-') { + return false; + } // create two pointers let start = 0; let end = str.length - 1; From 2c32fe69641905793863ab7b2fae12b0bafc3f06 Mon Sep 17 00:00:00 2001 From: Hiren Timbadiya Date: Sat, 4 Feb 2023 16:53:49 +0530 Subject: [PATCH 3/3] added readme file for the plindrome number problem --- 0001 Palindrome ( L - I)/README.md | 104 +++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 0001 Palindrome ( L - I)/README.md diff --git a/0001 Palindrome ( L - I)/README.md b/0001 Palindrome ( L - I)/README.md new file mode 100644 index 0000000..899dc57 --- /dev/null +++ b/0001 Palindrome ( L - I)/README.md @@ -0,0 +1,104 @@ +# Palindrome (L-I) + +## Problem +- 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. + +- 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. + +- 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. + +- Example Inputs and Outputs: + +- Input: 121 -> Output: true +- Input: -121 -> Output: false +- Input: 10 -> Output: false +- Input: 12321 -> Output: true + +## Solution + +```javascript +let n = 12321; + +if(palindrome(n)){ + console.log( n + " is a Palindrome number"); +}else{ + console.log( n + " is not a Palindrome number"); +} + +/* function to check if a number is palindrome or not +by reversing the number and comparing it with the original number */ +function palindrome(n){ + // create a temp variable to store the value of n + let temp = n; + // create a variable to store the reverse of n + let rev = 0; + // reverse the number + while(temp > 0){ + let lastDigit = temp % 10; + rev = rev * 10 + lastDigit; + temp = Math.floor(temp / 10); + } + // check if the reverse is equal to n + return rev == n; +} + +// using two pointers +function palindromeTwoPointer(n){ + // convert the number to string + let str = n.toString(); + // if the number is negative, remove the negative sign from the string + if (str[0] === '-') { + return false; + } + // create two pointers + let start = 0; + let end = str.length - 1; + // loop through the string + while(start < end){ + // check if the characters at the two pointers are equal + if(str[start] != str[end]){ + return false; + } + // increment start and decrement end + start++; + end--; + } + return true; +} +``` + +## How it works +### First Approach : +- Create a variable temp to store the value of n for later use. +- Create a variable rev to store the reverse of n. rev is initially set to 0. +- Use a while loop to reverse the number n. The loop continues as long as temp is greater than 0. +- a. In each iteration, the last digit of temp is calculated by taking the remainder when temp is divided by 10. + b. The last digit is then added to rev by multiplying rev by 10 and adding the last digit. + c. The value of temp is then updated by dividing it by 10 and flooring the result. This discards the last digit of temp. +- After the while loop, the function checks if the reversed number rev is equal to the original number n. +- If rev is equal to n, the function returns true, indicating that n is a palindrome. Otherwise, the function returns false. + +### Second Approach : +- First, the number is converted to a string. +- If the first character of the string is a negative sign (-), the function returns false as negative numbers cannot be palindromes. +- Two pointers are created, start and end, and initialized to the first and last characters of the string, respectively. +- The function then enters a loop, which continues as long as start is less than end. +- 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. +- If the characters are equal, start is incremented and end is decremented, and the loop continues. +- When the loop is finished, the function returns true, indicating that the number is a palindrome. + + +## References +- [Wikipedia](https://en.wikipedia.org/wiki/Palindromic_number#) + + +## Problem Added By +- [GitHub](https://www.github.com/hirentimbadiya) +- [LinkedIn](https://www.linkedin.com/in/hirentimbadiya74) +- [Twitter](https://twitter.com/heyhiru) + + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate.