cpp

Two method for finding the given number is palindrome or not

carnato

Vishal Yadav

Posted on January 11, 2022

Two method for finding the given number is palindrome or not

https://www.freecodecamp.org/news/everything-you-need-to-know-about-cookies-for-web-development/

  • Method 1
  • Basically we can sole the palindrome problem in two way

  • first is by using the reminder concept means that we can find the number using modulo operator and reverse the number and if given number is equal to that number then we can say number is palindrome.

 if(x<0) return false;
         if(x==0) return true;
         if(x%10==x) return true;
        if(x/10==0) return false;


         while(x>0)
         {
              rem=x%10;
            n=n*10+rem;
             x=x/10;

         }
         if(n==y)
             return true;
         else
             return false;
 }
Enter fullscreen mode Exit fullscreen mode
  • Method 2:

  • This method is used when number is given using the Linked List or array form or vector form

  • For example number is given in the linked list then we can create a vector v and store the value in vector and check from left pointing value and from right and if at least one number which is not equal the return false and if not then return true after the termination of loop

Algo 2 :

ListNode *temp=head;
        vector<int> v;
        while(temp!=NULL)
        {
            v.push_back(temp->val);
            temp=temp->next;
        }
        int n=v.size()-1;
        for(int i=0;i<v.size()/2;i++)
        {
            if(v[i]!=v[n])
                return false;
            n--;
        }
        return true;
Enter fullscreen mode Exit fullscreen mode

Thanks for reading
Happy Coding

πŸ’– πŸ’ͺ πŸ™… 🚩
carnato
Vishal Yadav

Posted on January 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related