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 True
Below is the implementation of the above idea :
C++
#include <bits/stdc++.h>
using namespace std;
bool checkJumbled( int num)
{
if (num / 10 == 0)
return true ;
while (num != 0)
{
if (num / 10 == 0)
return true ;
int digit1 = num % 10;
int digit2 = (num / 10) % 10;
if ( abs (digit2 - digit1) > 1)
return false ;
num = num / 10;
}
return true ;
}
int main()
{
int num = -1234;
if (checkJumbled(num))
cout << "True \n" ;
else
cout << "False \n" ;
num = -1247;
if (checkJumbled(num))
cout << "True \n" ;
else
cout << "False \n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean checkJumbled( int num)
{
if (num / 10 == 0 )
return true ;
while (num != 0 )
{
if (num / 10 == 0 )
return true ;
int digit1 = num % 10 ;
int digit2 = (num / 10 ) % 10 ;
if (Math.abs(digit2 - digit1) > 1 )
return false ;
num = num / 10 ;
}
return true ;
}
public static void main (String[]args)
{
int num = - 1234 ;
if (checkJumbled(num))
System.out.println( "True " );
else
System.out.println( "False " );
num = - 1247 ;
if (checkJumbled(num))
System.out.println( "True " );
else
System.out.println( "False " );
}
}
|
Python3
def checkJumbled(num):
if (num / / 10 = = 0 ):
return True
while (num ! = 0 ):
if (num / / 10 = = 0 ):
return True
digit1 = num % 10
digit2 = (num / / 10 ) % 10
if ( abs (digit2 - digit1) > 1 ):
return False
num = num / / 10
return True
num = - 1234
if (checkJumbled( abs (num))):
print ( True )
else :
print ( False )
num = - 1247
if (checkJumbled( abs (num))):
print ( True )
else :
print ( False )
|
C#
using System;
class GFG
{
static bool checkJumbled( int num)
{
if (num / 10 == 0)
return true ;
while (num != 0)
{
if (num / 10 == 0)
return true ;
int digit1 = num % 10;
int digit2 = (num / 10) % 10;
if (Math.Abs(digit2 - digit1) > 1)
return false ;
num = num / 10;
}
return true ;
}
public static void Main ()
{
int num = -1234;
if (checkJumbled(num))
Console.WriteLine( "True " );
else
Console.WriteLine( "False " );
num = -1247;
if (checkJumbled(num))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
PHP
<?php
function checkJumbled( $num )
{
if ( $num / 10 == 0)
return true;
while ( $num != 0)
{
if ( $num / 10 == 0)
return true;
$digit1 = $num % 10;
$digit2 = ( $num / 10) % 10;
if ( abs ( $digit2 - $digit1 ) > 1)
return false;
$num = $num / 10;
}
return true;
}
$num = -1234;
if (checkJumbled( $num ))
echo "True \n" ;
else
echo "False \n" ;
$num = -1247;
if (checkJumbled( $num ))
echo "True \n" ;
else
echo "False \n" ;
?>
|
Javascript
<script>
function checkJumbled(num)
{
if (parseInt(num / 10, 10) == 0)
return true ;
while (num != 0)
{
if (parseInt(num / 10, 10) == 0)
return true ;
let digit1 = num % 10;
let digit2 = parseInt(num / 10, 10) % 10;
if (Math.abs(digit2 - digit1) > 1)
return false ;
num = parseInt(num / 10, 10);
}
return true ;
}
let num = -1234;
if (checkJumbled(num))
document.write( "True " + "</br>" );
else
document.write( "False " + "</br>" );
num = -1247;
if (checkJumbled(num))
document.write( "True " + "</br>" );
else
document.write( "False " + "</br>" );
</script>
|
Time complexity: O(log10N), where N is the given number.
Auxiliary space: O(1), as constant space is being used.
Related Article :
Stepping Numbers
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.