# Jumbled number: Check if a number is jumbled number

Problem: Write a program to check if a given number is jumbled number.

## What is a jumbled number?

A jumbled number is a number in which for every digit, it’s neighbor digit (left or right) differs by max 1.
For example:

Number Is jumbled number?
1234 true
8987 true
2689 false
13 false

Algorithm:
To find a jumbled number we have to compare every digit of the number with the neighboring digits. So we have to get 2 neighboring digits of the number at a time and check if the difference is > 1. Let us see the algorithm.

```Say, n is the given number.

while n % 10 != 0 (Until all the digits of n are checked)
digit1 = n % 10      // first digit in reverse order
digit2 = (n/10) % 10 // (n/10) gives previous digit

if absolute difference between digit1 and digit2 is greater than 1
return false    // Not a jumbled number
end if
end while

// Difference between neighboring digits is less than 1 for all the digits
// of n. So
return true```

If there are n digits in a given number then above algorithm will have maximum n iterations. So worst-case complexity of above algorithm would be O(n).

Java program:
We will write a Java program for above algorithm.

```public class JumbledNumberChecker {

private static boolean isJumbledNumber(int number) {
while(number % 10 != 0){
int digit1 = number % 10;
int digit2 = (number/10) % 10;
if(Math.abs(digit2-digit1) > 1 && digit2 != 0)
return false;
number /= 10;
}
return true;
}

public static void main(String[] args) {
int n1 = -1234;
int n2 = 287;
System.out.println("Is " + n1 + " jumbled number? : "
+ isJumbledNumber(n1));
System.out.println("Is " + n2 + " jumbled number? : "
+ isJumbledNumber(n4));
}
}```

Output:

```Is -1234 jumbled number? : true
Is 287 jumbled number? : false```

I hope you liked the solution.