VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-number-is-jumbled-or-not/

⇱ Check if a number is jumbled or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a number is jumbled or not

Last Updated : 22 Sep, 2022

Write a program to check if a given integer is jumbled or not. A number is said to be Jumbled if for every digit, its neighbours digit differs by max 1. 

Examples : 

Input : 6765
Output : True
All neighbour digits differ by atmost 1.

Input : 1223
Output : True

Input : 1235
Output : False


 

Approach:

  • Find the adjacent digits in the number while num > 0
    • if the absolute difference of the digits is greater than 1.
      • Return false
  • Return True

Below is the implementation of the above idea : 


Output
True 
False 

Time complexity: O(log10N), where N is the given number.
Auxiliary space: O(1), as constant space is being used.


Related Article : 
Stepping Numbers
 

Comment
Article Tags: