PALL01 - Editorial

Problem Link - The Block Game Practice Problem in 500 to 1000 difficulty problems

Problem Statement:

The problem asks to determine whether a given number is a palindrome. A number is considered a palindrome if it reads the same from both ends. In other words, if the number is reversed, it should remain identical to the original number.

The task is to take multiple test cases, check each number, and determine if it is a palindrome or not, outputting “wins” if it’s a palindrome and “loses” if it is not.

Approach:

To solve this problem, we can use the reverse of a number and compare it to the original number.

Reverse the Number Using Modulo and Division:

  • For a given number N, we can reverse the number using the following steps:
    • Take the last digit of N using N % 10.
    • Append this digit to a new number, which is initially set to 0, by multiplying the new number by 10.
    • Divide N by 10 to remove the last digit.
    • Continue this process until N becomes 0.
  • The new number generated will be the reverse of the original number.

Check for Palindrome:

  • After reversing the number, compare the reversed number with the original number.
  • If they are equal, then the number is a palindrome and the output is “wins”.
  • If they are not equal, the output is “loses”.

Complexity;

  • Time Complexity: The time complexity for each test case is O(d) where d is the number of digits in the number N.
  • Space Complexity: O(1) since we are only using a constant amount of extra space.