Comprobar si un número está mezclado o no

Escriba un programa para verificar si un entero dado está mezclado o no. Se dice que un número está mezclado si para cada dígito, el dígito de su vecino difiere en un máximo de 1. 
Ejemplos: 
 

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

Input : 1223
Output : True

Input : 1235
Output : False

Para encontrar si un número está desordenado, solo necesitamos verificar si un dígito tiene un vecino 
con una diferencia mayor a 1. Si se encuentra dicho dígito, el número no está desordenado.
A continuación se muestra la implementación de la idea anterior: 
 

C++

// CPP code to check if a
// number is jumbled or not
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a
// number is jumbled or not
bool checkJumbled(int num)
{
    // Single digit number
    if (num / 10 == 0)
        return true;
 
    // Checking every digit
    // through a loop
    while (num != 0)
    {
 
        // All digits were checked
        if (num / 10 == 0)
            return true;
 
        // Digit at index i
        int digit1 = num % 10;
 
        // Digit at index i-1
        int digit2 = (num / 10) % 10;
 
        // If difference is
        // greater than 1
        if (abs(digit2 - digit1) > 1)
            return false;
 
        num = num / 10;
    }
 
    // Number checked
    return true;
}
 
// Driver code
int main()
{
 
    //-1234 to be checked
    int num = -1234;
 
    if (checkJumbled(num))
        cout << "True \n";
    else
        cout << "False \n";
 
    // 287 to be checked
    num = -1247;
 
    if (checkJumbled(num))
        cout << "True \n";
    else
        cout << "False \n";
 
    return 0;
}

Java

// Java code to check if a
// number is jumbled or not
import java.io.*;
 
class GFG
{
     
    // Function to check if a
    // number is jumbled or not
    static boolean checkJumbled(int num)
    {
        // Single digit number
        if (num / 10 == 0)
            return true;
     
        // Checking every digit
        // through a loop
        while (num != 0)
        {
     
            // All digits were checked
            if (num / 10 == 0)
                return true;
     
            // Digit at index i
            int digit1 = num % 10;
     
            // Digit at index i-1
            int digit2 = (num / 10) % 10;
     
            // If difference is
            // greater than 1
            if (Math.abs(digit2 - digit1) > 1)
                return false;
     
            num = num / 10;
        }
     
        // Number checked
        return true;
    }
     
    // Driver code
    public static void main (String[]args)
    {
        //-1234 to be checked
        int num = -1234;
     
        if (checkJumbled(num))
            System.out.println("True ");
        else
            System.out.println("False ");
     
        // 287 to be checked
        num = -1247;
     
        if (checkJumbled(num))
            System.out.println("True ");
                 
        else
            System.out.println("False ");
             
    }
}
 
// This code is contributed by vt_m.

Python3

# Python code to check if
# a number is jumbled or not
 
# Function to check if a
# number is jumbled or not
def checkJumbled(num):
 
    # Single digit number
    if (num // 10 == 0):
        return True
 
    # Checking every digit
    # through a loop
    while (num != 0):
         
        # All digits were checked
        if (num // 10 == 0):
            return True
 
        # Digit at index i
        digit1 = num % 10
 
        # Digit at index i-1
        digit2 = (num // 10) % 10
 
        # If difference is
        # greater than 1
        if (abs(digit2 - digit1) > 1):
            return False
 
        num = num // 10
         
    # Number checked
    return True
     
# Driver code
 
# -1234 to be checked
num = -1234
if (checkJumbled(abs(num))):
    print (True)
else:
    print (False)
     
# -1247 to be checked
num = -1247
if (checkJumbled(abs(num))):
    print (True)
else:
    print (False)
 
# This code is contributed
# by Sachin Bisht

C#

// C# code to check if a number
// is jumbled or not
using System;
 
class GFG
{
     
    // Function to check if a
    // number is jumbled or not
    static bool checkJumbled(int num)
    {
         
        // Single digit number
        if (num / 10 == 0)
            return true;
     
        // Checking every digit
        // through a loop
        while (num != 0)
        {
     
            // All digits were checked
            if (num / 10 == 0)
                return true;
     
            // Digit at index i
            int digit1 = num % 10;
     
            // Digit at index i-1
            int digit2 = (num / 10) % 10;
     
            // If difference is
            // greater than 1
            if (Math.Abs(digit2 - digit1) > 1)
                return false;
     
            num = num / 10;
        }
     
        // Number checked
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
        //-1234 to be checked
        int num = -1234;
     
        if (checkJumbled(num))
            Console.WriteLine("True ");
        else
            Console.WriteLine("False ");
     
        // 287 to be checked
        num = -1247;
     
        if (checkJumbled(num))
            Console.WriteLine("True");
                 
        else
            Console.WriteLine("False");
             
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP code to check if a
// number is jumbled or not
 
// Function to check if a
// number is jumbled or not
function checkJumbled($num)
{
    // Single digit number
    if ($num / 10 == 0)
        return true;
 
    // Checking every digit
    // through a loop
    while ($num != 0)
    {
 
        // All digits were checked
        if ($num / 10 == 0)
            return true;
 
        // Digit at index i
        $digit1 = $num % 10;
 
        // Digit at index i-1
        $digit2 = ($num / 10) % 10;
 
        // If difference is
        // greater than 1
        if (abs($digit2 - $digit1) > 1)
            return false;
 
        $num = $num / 10;
    }
 
    // Number checked
    return true;
}
 
// Driver code
 
//-1234 to be checked
$num = -1234;
 
if (checkJumbled($num))
    echo "True \n";
else
    echo "False \n";
 
// 287 to be checked
$num = -1247;
 
if (checkJumbled($num))
    echo "True \n";
else
    echo "False \n";
 
// This code is contributed by ajit
?>

Javascript

<script>
    // Javascript code to check if a number is jumbled or not
     
    // Function to check if a
    // number is jumbled or not
    function checkJumbled(num)
    {
           
        // Single digit number
        if (parseInt(num / 10, 10) == 0)
            return true;
       
        // Checking every digit
        // through a loop
        while (num != 0)
        {
       
            // All digits were checked
            if (parseInt(num / 10, 10) == 0)
                return true;
       
            // Digit at index i
            let digit1 = num % 10;
       
            // Digit at index i-1
            let digit2 = parseInt(num / 10, 10) % 10;
       
            // If difference is
            // greater than 1
            if (Math.abs(digit2 - digit1) > 1)
                return false;
       
            num = parseInt(num / 10, 10);
        }
       
        // Number checked
        return true;
    }
     
    //-1234 to be checked
    let num = -1234;
 
    if (checkJumbled(num))
      document.write("True " + "</br>");
    else
      document.write("False " + "</br>");
 
    // 287 to be checked
    num = -1247;
 
    if (checkJumbled(num))
      document.write("True " + "</br>");
 
    else
      document.write("False " + "</br>");
     
    // This code is contributed by rameshtravel07.
</script>

Producción : 
 

True
False

Artículo relacionado: 
Números escalonados
Este artículo es una contribución de Rohit Thapliyal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *