Posted on by Kalkicode
Code Number

Check if a number is jumbled or not

In this article, we will discuss how to determine if a given number is a jumbled number or not. A jumbled number is a number in which the difference between any two consecutive digits is either 0 or 1. For example, 123, 444, and -1234 are jumbled numbers, while 4234 and 14441 are not jumbled numbers.

Algorithm:

1. Start the program.
2. Define a function named is_jumbled_no that takes an integer parameter called data.
3. Set the variable status to 0. This variable will be used to determine if the number is jumbled or not.
4. Check if the given number data is negative. If so, convert it to a positive number.
5. Check if the number is a single digit (between 0 and 9). If it is, set status to 1.
6. If the number is not a single digit, extract the last digit l_digit from the number num using the modulo operator.
7. Create a variable c_digit to store the last digit of the current number.
8. Remove the last digit from the number num by dividing it by 10.
9. Set status to 1, indicating that everything is good so far.
10. Enter a while loop that continues as long as status is 1 and num is not equal to 0.
11. Inside the loop, extract the last digit c_digit of the current number num.
12. Check if the difference between c_digit and l_digit is 0 or 1. If it is, update l_digit to c_digit and remove the last digit from num by dividing it by 10.
13. If the difference between the digits is not 0 or 1, set status to 0, indicating that the number is not jumbled.
14. After the loop ends, check the value of status. If it is 0, print that the number is not a jumbled number. Otherwise, print that the number is a jumbled number.
15. Repeat steps 2-14 for each test case.
16. End the program.

Pseudocode:

// Determine whether given number is jumbled number or not
function is_jumbled_no(data):
	status = 0
	num = data
	if data < 0:
		num = -num
	if num >= 0 and num <= 9:
		status = 1
	else:
		l_digit = num % 10
		c_digit = 0
		num = num / 10
		status = 1
		while status == 1 and num != 0:
			c_digit = num % 10
			if c_digit == l_digit or c_digit + 1 == l_digit or c_digit - 1 == l_digit:
				l_digit = c_digit
				num = num / 10
			else:
				status = 0
	if status == 0:
		print("Number", data, "is not a jumbled number")
	else:
		print("Number", data, "is a jumbled number")

// Test cases
is_jumbled_no(123)
is_jumbled_no(4234)
is_jumbled_no(-1234)
is_jumbled_no(-444)
is_jumbled_no(14441)

Code Solution

Here given code implementation process.

// C Program
// Check if a number is jumbled or not
#include <stdio.h>

// Determine whether given number is jumbled number or not
void is_jumbled_no(int data)
{
	//Used to determine jumbled number
	int status = 0;
	int num = data;
	if (data < 0)
	{
		//if number is negative then transform into a positive number
		num = -num;
	}
	if (num >= 0 && num <= 9)
	{
		//When given number is a single digit number
		status = 1;
	}
	else
	{
		//Get last digit of given number
		int l_digit = num % 10;
		//This is used to store last digit of current number
		int c_digit = 0;
		//Remove last digit
		num = num / 10;
		//Set the everything is good
		status = 1;
		while (status == 1 && num != 0)
		{
			//Get last digit of current number
			c_digit = num % 10;
			if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
			{
				//When digit difference are under a jumbled number
				//Get new last digit
				l_digit = c_digit;
				//Remove last digit
				num = num / 10;
			}
			else
			{
				//When not jumbled number
				status = 0;
			}
		}
	}
	if (status == 0)
	{
		//When number are not jumbled
		printf("\n Number %d are not jumbled number \n", data);
	}
	else
	{
		//When number is jumbled number
		printf("\n Number %d is jumbled number \n", data);
	}
}
int main()
{
	// Test case
	is_jumbled_no(123);
	is_jumbled_no(4234);
	is_jumbled_no(-1234);
	is_jumbled_no(-444);
	is_jumbled_no(14441);
	return 0;
}

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
// Java Program
// Check if a number is jumbled or not
public class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	public void is_jumbled_no(int data)
	{
		//Used to determine jumbled number
		int status = 0;
		//Store the given number
		int num = data;
		if (data < 0)
		{
			//if number is negative then transform into a positive number
			num = -num;
		}
		if (num >= 0 && num <= 9)
		{
			//When given number is a single digit number
			status = 1;
		}
		else
		{
			//Get last digit of given number
			int l_digit = num % 10;
			//This is used to store last digit of current number
			int c_digit = 0;
			//Remove last digit
			num = num / 10;
			//Set the everything is good
			status = 1;
			while (status == 1 && num != 0)
			{
				//Get last digit of current number
				c_digit = num % 10;
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					l_digit = c_digit;
					//Remove last digit
					num = num / 10;
				}
				else
				{
					//When not jumbled number
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			//When number are not jumbled
			System.out.print("\n Number " + data + " are not jumbled number \n");
		}
		else
		{
			//When number is jumbled number
			System.out.print("\n Number " + data + " is jumbled number \n");
		}
	}
	public static void main(String[] args)
	{
		JumbledNumber obj = new JumbledNumber();
		// Test case
		obj.is_jumbled_no(123);
		obj.is_jumbled_no(4234);
		obj.is_jumbled_no(-1234);
		obj.is_jumbled_no(-444);
		obj.is_jumbled_no(14441);
	}
}

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Check if a number is jumbled or not
class JumbledNumber
{
	public:
		// Determine whether given number is jumbled number or not
		void is_jumbled_no(int data)
		{
			//Used to determine jumbled number
			int status = 0;
			//Store the given number
			int num = data;
			if (data < 0)
			{
				//if number is negative then transform into a positive number
				num = -num;
			}
			if (num >= 0 && num <= 9)
			{
				//When given number is a single digit number
				status = 1;
			}
			else
			{
				//Get last digit of given number
				int l_digit = num % 10;
				//This is used to store last digit of current number
				int c_digit = 0;
				//Remove last digit
				num = num / 10;
				//Set the everything is good
				status = 1;
				while (status == 1 && num != 0)
				{
					//Get last digit of current number
					c_digit = num % 10;
					if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
					{
						//When digit difference are under a jumbled number
						//Get new last digit
						l_digit = c_digit;
						//Remove last digit
						num = num / 10;
					}
					else
					{
						//When not jumbled number
						status = 0;
					}
				}
			}
			if (status == 0)
			{
				//When number are not jumbled
				cout << "\n Number " << data << " are not jumbled number \n";
			}
			else
			{
				//When number is jumbled number
				cout << "\n Number " << data << " is jumbled number \n";
			}
		}
};
int main()
{
	JumbledNumber obj = JumbledNumber();
	// Test case
	obj.is_jumbled_no(123);
	obj.is_jumbled_no(4234);
	obj.is_jumbled_no(-1234);
	obj.is_jumbled_no(-444);
	obj.is_jumbled_no(14441);
	return 0;
}

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
//Include namespace system
using System;

// C# Program
// Check if a number is jumbled or not

public class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	public void is_jumbled_no(int data)
	{
		//Used to determine jumbled number
		int status = 0;
		//Store the given number
		int num = data;
		if (data < 0)
		{
			//if number is negative then transform into a positive number
			num = -num;
		}
		if (num >= 0 && num <= 9)
		{
			//When given number is a single digit number
			status = 1;
		}
		else
		{
			//Get last digit of given number
			int l_digit = num % 10;
			//This is used to store last digit of current number
			int c_digit = 0;
			//Remove last digit
			num = num / 10;
			//Set the everything is good
			status = 1;
			while (status == 1 && num != 0)
			{
				//Get last digit of current number
				c_digit = num % 10;
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					l_digit = c_digit;
					//Remove last digit
					num = num / 10;
				}
				else
				{
					//When not jumbled number
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			//When number are not jumbled
			Console.Write("\n Number " + data + " are not jumbled number \n");
		}
		else
		{
			//When number is jumbled number
			Console.Write("\n Number " + data + " is jumbled number \n");
		}
	}
	public static void Main(String[] args)
	{
		JumbledNumber obj = new JumbledNumber();
		// Test case
		obj.is_jumbled_no(123);
		obj.is_jumbled_no(4234);
		obj.is_jumbled_no(-1234);
		obj.is_jumbled_no(-444);
		obj.is_jumbled_no(14441);
	}
}

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
<?php
// Php Program
// Check if a number is jumbled or not

class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	public	function is_jumbled_no($data)
	{
		//Used to determine jumbled number
		$status = 0;
		//Store the given number
		$num = $data;
		if ($data < 0)
		{
			//if number is negative then transform into a positive number
			$num = -$num;
		}
		if ($num >= 0 && $num <= 9)
		{
			//When given number is a single digit number
			$status = 1;
		}
		else
		{
			//Get last digit of given number
			$l_digit = $num % 10;
			//This is used to store last digit of current number
			$c_digit = 0;
			//Remove last digit
			$num = intval($num / 10);
			//Set the everything is good
			$status = 1;
			while ($status == 1 && $num != 0)
			{
				//Get last digit of current number
				$c_digit = $num % 10;
				if ($c_digit == $l_digit || $c_digit + 1 == $l_digit || $c_digit - 1 == $l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					$l_digit = $c_digit;
					//Remove last digit
					$num = intval($num / 10);
				}
				else
				{
					//When not jumbled number
					$status = 0;
				}
			}
		}
		if ($status == 0)
		{
			//When number are not jumbled
			echo "\n Number ". $data ." are not jumbled number \n";
		}
		else
		{
			//When number is jumbled number
			echo "\n Number ". $data ." is jumbled number \n";
		}
	}
}

function main()
{
	$obj = new JumbledNumber();
	// Test case
	$obj->is_jumbled_no(123);
	$obj->is_jumbled_no(4234);
	$obj->is_jumbled_no(-1234);
	$obj->is_jumbled_no(-444);
	$obj->is_jumbled_no(14441);
}
main();

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
// Node Js Program
// Check if a number is jumbled or not
class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	is_jumbled_no(data)
	{
		//Used to determine jumbled number
		var status = 0;
		//Store the given number
		var num = data;
		if (data < 0)
		{
			//if number is negative then transform into a positive number
			num = -num;
		}
		if (num >= 0 && num <= 9)
		{
			//When given number is a single digit number
			status = 1;
		}
		else
		{
			//Get last digit of given number
			var l_digit = num % 10;
			//This is used to store last digit of current number
			var c_digit = 0;
			//Remove last digit
			num = parseInt(num / 10);
			//Set the everything is good
			status = 1;
			while (status == 1 && num != 0)
			{
				//Get last digit of current number
				c_digit = num % 10;
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					l_digit = c_digit;
					//Remove last digit
					num = parseInt(num / 10);
				}
				else
				{
					//When not jumbled number
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			//When number are not jumbled
			process.stdout.write("\n Number " + data + " are not jumbled number \n");
		}
		else
		{
			//When number is jumbled number
			process.stdout.write("\n Number " + data + " is jumbled number \n");
		}
	}
}

function main()
{
	var obj = new JumbledNumber();
	// Test case
	obj.is_jumbled_no(123);
	obj.is_jumbled_no(4234);
	obj.is_jumbled_no(-1234);
	obj.is_jumbled_no(-444);
	obj.is_jumbled_no(14441);
}
main();

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
#  Python 3 Program
#  Check if a number is jumbled or not
class JumbledNumber :
	#  Determine whether given number is jumbled number or not
	def is_jumbled_no(self, data) :
		# Used to determine jumbled number
		status = 0
		# Store the given number
		num = data
		if (data < 0) :
			# if number is negative then transform into a positive number
			num = -num
		
		if (num >= 0 and num <= 9) :
			# When given number is a single digit number
			status = 1
		else :
			# Get last digit of given number
			l_digit = num % 10
			# This is used to store last digit of current number
			c_digit = 0
			# Remove last digit
			num = int(num / 10)
			# Set the everything is good
			status = 1
			while (status == 1 and num != 0) :
				# Get last digit of current number
				c_digit = num % 10
				if (c_digit == l_digit or c_digit + 1 == l_digit or c_digit - 1 == l_digit) :
					# When digit difference are under a jumbled number
					# Get new last digit
					l_digit = c_digit
					# Remove last digit
					num = int(num / 10)
				else :
					# When not jumbled number
					status = 0
				
			
		
		if (status == 0) :
			# When number are not jumbled
			print("\n Number ", data ," are not jumbled number \n", end = "")
		else :
			# When number is jumbled number
			print("\n Number ", data ," is jumbled number \n", end = "")
		
	

def main() :
	obj = JumbledNumber()
	#  Test case
	obj.is_jumbled_no(123)
	obj.is_jumbled_no(4234)
	obj.is_jumbled_no(-1234)
	obj.is_jumbled_no(-444)
	obj.is_jumbled_no(14441)

if __name__ == "__main__": main()

Output

 Number  123  is jumbled number

 Number  4234  are not jumbled number

 Number  -1234  is jumbled number

 Number  -444  is jumbled number

 Number  14441  are not jumbled number
#  Ruby Program
#  Check if a number is jumbled or not
class JumbledNumber

	#  Determine whether given number is jumbled number or not
	def is_jumbled_no(data)
	
		# Used to determine jumbled number
		status = 0
		# Store the given number
		num = data
		if (data < 0)
		
			# if number is negative then transform into a positive number
			num = -num
		end
		if (num >= 0 && num <= 9)
		
			# When given number is a single digit number
			status = 1
		else
		
			# Get last digit of given number
			l_digit = num % 10
			# This is used to store last digit of current number
			c_digit = 0
			# Remove last digit
			num = num / 10
			# Set the everything is good
			status = 1
			while (status == 1 && num != 0)
			
				# Get last digit of current number
				c_digit = num % 10
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				
					# When digit difference are under a jumbled number
					# Get new last digit
					l_digit = c_digit
					# Remove last digit
					num = num / 10
				else
				
					# When not jumbled number
					status = 0
				end
			end
		end
		if (status == 0)
		
			# When number are not jumbled
			print("\n Number ", data ," are not jumbled number \n")
		else
		
			# When number is jumbled number
			print("\n Number ", data ," is jumbled number \n")
		end
	end
end
def main()

	obj = JumbledNumber.new()
	#  Test case
	obj.is_jumbled_no(123)
	obj.is_jumbled_no(4234)
	obj.is_jumbled_no(-1234)
	obj.is_jumbled_no(-444)
	obj.is_jumbled_no(14441)
end
main()

Output

 Number 123 is jumbled number 

 Number 4234 are not jumbled number 

 Number -1234 is jumbled number 

 Number -444 is jumbled number 

 Number 14441 are not jumbled number 
// Scala Program
// Check if a number is jumbled or not
class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	def is_jumbled_no(data: Int): Unit = {
		//Used to determine jumbled number
		var status: Int = 0;
		//Store the given number
		var num: Int = data;
		if (data < 0)
		{
			//if number is negative then transform into a positive number
			num = -num;
		}
		if (num >= 0 && num <= 9)
		{
			//When given number is a single digit number
			status = 1;
		}
		else
		{
			//Get last digit of given number
			var l_digit: Int = num % 10;
			//This is used to store last digit of current number
			var c_digit: Int = 0;
			//Remove last digit
			num = (num / 10).toInt;
			//Set the everything is good
			status = 1;
			while (status == 1 && num != 0)
			{
				//Get last digit of current number
				c_digit = num % 10;
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					l_digit = c_digit;
					//Remove last digit
					num = (num / 10).toInt;
				}
				else
				{
					//When not jumbled number
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			//When number are not jumbled
			print("\n Number " + data + " are not jumbled number \n");
		}
		else
		{
			//When number is jumbled number
			print("\n Number " + data + " is jumbled number \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: JumbledNumber = new JumbledNumber();
		// Test case
		obj.is_jumbled_no(123);
		obj.is_jumbled_no(4234);
		obj.is_jumbled_no(-1234);
		obj.is_jumbled_no(-444);
		obj.is_jumbled_no(14441);
	}
}

Output

 Number 123 is jumbled number

 Number 4234 are not jumbled number

 Number -1234 is jumbled number

 Number -444 is jumbled number

 Number 14441 are not jumbled number
// Swift Program
// Check if a number is jumbled or not
class JumbledNumber
{
	// Determine whether given number is jumbled number or not
	func is_jumbled_no(_ data: Int)
	{
		//Used to determine jumbled number
		var status: Int = 0;
		//Store the given number
		var num: Int = data;
		if (data < 0)
		{
			//if number is negative then transform into a positive number
			num = -num;
		}
		if (num >= 0 && num <= 9)
		{
			//When given number is a single digit number
			status = 1;
		}
		else
		{
			//Get last digit of given number
			var l_digit: Int = num % 10;
			//This is used to store last digit of current number
			var c_digit: Int = 0;
			//Remove last digit
			num = num / 10;
			//Set the everything is good
			status = 1;
			while (status == 1 && num != 0)
			{
				//Get last digit of current number
				c_digit = num % 10;
				if (c_digit == l_digit || c_digit + 1 == l_digit || c_digit - 1 == l_digit)
				{
					//When digit difference are under a jumbled number
					//Get new last digit
					l_digit = c_digit;
					//Remove last digit
					num = num / 10;
				}
				else
				{
					//When not jumbled number
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			//When number are not jumbled
			print("\n Number ", data ," are not jumbled number \n", terminator: "");
		}
		else
		{
			//When number is jumbled number
			print("\n Number ", data ," is jumbled number \n", terminator: "");
		}
	}
}
func main()
{
	let obj: JumbledNumber = JumbledNumber();
	// Test case
	obj.is_jumbled_no(123);
	obj.is_jumbled_no(4234);
	obj.is_jumbled_no(-1234);
	obj.is_jumbled_no(-444);
	obj.is_jumbled_no(14441);
}
main();

Output

 Number  123  is jumbled number

 Number  4234  are not jumbled number

 Number  -1234  is jumbled number

 Number  -444  is jumbled number

 Number  14441  are not jumbled number

Explanation:

The code starts by defining a function named is_jumbled_no that takes an integer parameter called data. It initializes the status variable to 0, which will be used to determine if the number is jumbled or not. If the given number data is negative, it converts it to a positive number.

The code then checks if the number is a single digit (between 0 and 9). If it is, it sets status to 1, indicating that the number is jumbled. Otherwise, it enters a loop to check the jumbledness of the number.

In the loop, the code extracts the last digit l_digit of the current number num using the modulo operator. It then removes the last digit from the number num by dividing it by 10. The code compares the difference between the current digit c_digit and the last digit l_digit to determine if they are within the range of a jumbled number. If the difference is 0 or 1, it updates l_digit to c_digit and continues with the next iteration of the loop. If the difference is not 0 or 1, it sets status to 0, indicating that the number is not jumbled, and breaks out of the loop.

Finally, the code checks the value of status and prints whether the number is a jumbled number or not. The code then provides the test cases with their respective outputs.

Time Complexity:

The time complexity of the code is determined by the number of digits in the given number. Let's assume the number has 'n' digits.

In the worst case, the code iterates through each digit of the number once, checking if it satisfies the jumbled number condition. Therefore, the time complexity of the code can be expressed as O(n).

Since the code only performs basic arithmetic operations and conditional checks, the time complexity remains linear, which means the execution time increases linearly with the size of the input.

In terms of space complexity, the code uses a few integer variables to store intermediate results, which require a constant amount of space. Hence, the space complexity is O(1), constant space.

To summarize, the code checks whether a given number is jumbled or not. It iterates through each digit of the number, comparing the difference between consecutive digits to determine if they fall within the jumbled number condition. The time complexity of the code is O(n), where 'n' is the number of digits in the input number, and the space complexity is O(1).

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment