Data Structures and Algorithms(Day-3 #I4G10DaysOfCodeChallenge)

Palindrome Number Challenge

Data Structures and Algorithms(Day-3 #I4G10DaysOfCodeChallenge)

Introduction

Hello guys!!! This is the thrid day of the I4G10DaysOfCodeChallenge and today's challenge is the Palindrome Number Challenge on Leetcode. Let's hop over to the challenge.

Challenge

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.

Click Here to view the challenge and its examples.

Explanation

From the challenge, it is required if the given integer, x, is a palindrome, the function returns true, and if it is not, it returns false.

First, understand what a palindrome is. A palindrome is a word, phrase, number or any other sequence of units which has the same property of reading the same forwards as it does backwards, character for character.

Don't be blown by the definition above. Simply put, a palindrome is a string or integer that reads the same, from left to right and from right to left. Also, note that a single character/integer is a palindrome.

Examples:
1. 121=> 
        from left to right = 121
        from right to left = 121
  121 is a palindrome

2. 64746=>
        from left to right = 64746
        from right to left = 64746
 64746 is a palindrome

3. 568=>
      from left to right = 568
      from right to left = 865
  568 is not a palindrome

The challenge requires checking if the given integer is a palindrome without having to do any string conversion.

Solution

  1. The first step to solving this is to note that for an integer to be a palindrome, its reversed form must be equal to the original form (left to right equals right to left). Then start the reversal to check if it a palindrome.

  2. To reverse a given integer, a loop is required (while loop), declaration and initialization of two variable; one variable will hold the reversed integer after the process, the other will be assigned to the given integer.

  3. The while loop is given a defined constraint, that the logic inside the loop will continue to run as long as the variable assigned to the given integer remains greater that zero.

//declare and intialize a variable to hold the reversed integer, reversed variable
//declare and assign another variable to the given integer, assigned variable

while( assigned variable to the given integer remains greater than zero){
    //reversal logic
}
  1. Inside the while loop, equate the reversed variable to the sum of the product of the itself and 10, and the modulus of the assigned variable to the integer to mod(%) 10. Also, equate the integer assigned variable to rounded off value of the division of the itself by 10.
while(...){
 reversed variable = product of reversed variable and 10, plus integer assigned variable mod 10
assigned variable = assigned variable divided by 10

// The first line  will always reverse the given integer starting from the last digits
// The second is to reduce the integer after removing the last digit.
// E.g 564 , reversed variable=0 , assigned variable=564
// reversed variable=4 , assigned variable=56
//reversed variable=46 , assigned variable=5
//reversed variable=465 , assigned variable=0
}
  1. When the above description exits out, the string is reversed. The compare the reversed variable with the given integer. If it matches(equal to) the given integer, it a palindrome, else it is not a palindrome. Return true for palindrome and false if otherwise.

day-3.png Thanks for Reading.