Check if a number has digits in the given Order

Given a number N. The task is to check if the digits of the number follow any of the below order:

  • The digits are in strictly increasing order.
  • Or, the digits are in strictly decreasing order.
  • Or, the digits follow strictly increasing order first and then strictly decreasing.

If the number follows any of the above order then print YES otherwise print NO.

Examples:

Input : N = 415
Output : NO

Input : N = 123454321
Output : YES


Traverse the number from right to left by extracting each digit one by one. Keep a pointer to tell that whether the current sequence is descending or ascending sequence, -1 denotes strictly ascending and 1 denotes strictly descending sequence. At first the sequence should be strictly increasing as we are going from right to left. As we encounter a digit which is lesser than the previous digit, change the flag to decreasing(i.e -1) and while in increasing order we get any digit which is equal to the previous digit we directly print NO.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if the digits are in
// the given order
#include<bits/stdc++.h>
using namespace std;
  
// Check if the digits follow the correct order
bool isCorrectOrder(int n)
{
    bool flag = true;
      
    // to store the prevoius digit
    int prev = -1; 
      
    // pointer to tell what type of sequence 
    // are we dealing with
    int type = -1; 
  
    while(n != 0)
    {
        if(type ==-1)
        {
            if(prev ==-1)
            {
                prev = n % 10;
                n = n/10;
                continue;
            }
            // check if we have same digit 
            // as the prevoius digit
            if(prev == n % 10) 
            {   
                flag = false;
                break;
            }
            // checking the peak point of the number
            if(prev > n % 10) 
            {  
                type = 1;
                prev = n % 10;
                n = n/10;
                continue;
            }
              
            prev = n % 10;
            n = n / 10;
        }
        else
        {   
            // check if we have same digit
            // as the prevoius digit
            if(prev == n % 10) 
            {   
                flag = false;
                break;
            }
            // check if the digit is greater than 
            // the prevoius one 
            // If true, then break from the loop as 
            // we are in decending order part
            if(prev < n % 10) 
            {           
                flag = false;
                break;
            }
               
            prev = n % 10;
            n = n / 10;
        }
    }
  
    return flag;
}
  
// Driver code
int main()
{
    int n = 123454321;
      
    if(isCorrectOrder(n))
        cout<<"YES";
    else
        cout<<"NO";
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if the digits are in
// the given order
import java.io.*;
  
class GFG {
  
// Check if the digits follow the correct order
static boolean isCorrectOrder(int n)
{
    boolean flag = true;
      
    // to store the prevoius digit
    int prev = -1
      
    // pointer to tell what type of sequence 
    // are we dealing with
    int type = -1
  
    while(n != 0)
    {
        if(type ==-1)
        {
            if(prev ==-1)
            {
                prev = n % 10;
                n = n/10;
                continue;
            }
            // check if we have same digit 
            // as the prevoius digit
            if(prev == n % 10
            
                flag = false;
                break;
            }
            // checking the peak point of the number
            if(prev > n % 10
            
                type = 1;
                prev = n % 10;
                n = n/10;
                continue;
            }
              
            prev = n % 10;
            n = n / 10;
        }
        else
        
            // check if we have same digit
            // as the prevoius digit
            if(prev == n % 10
            
                flag = false;
                break;
            }
            // check if the digit is greater than 
            // the prevoius one 
            // If true, then break from the loop as 
            // we are in decending order part
            if(prev < n % 10
            {         
                flag = false;
                break;
            }
              
            prev = n % 10;
            n = n / 10;
        }
    }
  
    return flag;
}
  
// Driver code
  
    public static void main (String[] args) {
        int n = 123454321;
      
    if(isCorrectOrder(n))
        System.out.println("YES");
    else
        System.out.println("NO");
    }
}
  
 // This code is contributed by ajit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if the 
# digits are in the given order 
  
# Check if the digits follow 
# the correct order 
def isCorrectOrder(n): 
  
    flag = True
      
    # to store the prevoius digit 
    prev = -1
      
    # pointer to tell what type of 
    # sequence are we dealing with 
    type = -1
  
    while(n != 0):
        if(type ==-1):
            if(prev ==-1):
                prev = n % 10
                n = int(n / 10); 
                continue
              
            # check if we have same digit 
            # as the prevoius digit 
            if(prev == n % 10): 
                flag = False
                break
              
            # checking the peak point 
            # of the number 
            if(prev > n % 10):
                type = 1
                prev = n % 10
                n = int(n / 10); 
                continue
              
            prev = n % 10
            n = int(n / 10); 
        else:
              
            # check if we have same digit 
            # as the prevoius digit 
            if(prev == n % 10):
                flag = False
                break
              
            # check if the digit is greater 
            # than the prevoius one 
            # If true, then break from the 
            # loop as we are in decending
            # order part 
            if(prev < n % 10): 
                flag = False
                break
              
            prev = n % 10
            n = int(n / 10); 
  
    return flag; 
  
# Driver code 
n = 123454321
  
if(isCorrectOrder(n)): 
    print("YES"); 
else:
    print("NO"); 
  
# This Code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if the 
// digits are in the given order
using System;
  
class GFG 
{
  
// Check if the digits follow
// the correct order
static bool isCorrectOrder(int n)
{
    bool flag = true;
      
    // to store the prevoius digit
    int prev = -1; 
      
    // pointer to tell what type of 
    // sequence are we dealing with
    int type = -1; 
  
    while(n != 0)
    {
        if(type == -1)
        {
            if(prev == -1)
            {
                prev = n % 10;
                n = n / 10;
                continue;
            }
              
            // check if we have same digit 
            // as the prevoius digit
            if(prev == n % 10) 
            
                flag = false;
                break;
            }
              
            // checking the peak point 
            // of the number
            if(prev > n % 10) 
            
                type = 1;
                prev = n % 10;
                n = n / 10;
                continue;
            }
              
            prev = n % 10;
            n = n / 10;
        }
        else
        
            // check if we have same digit
            // as the prevoius digit
            if(prev == n % 10) 
            
                flag = false;
                break;
            }
              
            // check if the digit is greater 
            // than the prevoius one 
            // If true, then break from the
            // loop as we are in decending
            // order part
            if(prev < n % 10) 
            {     
                flag = false;
                break;
            }
              
            prev = n % 10;
            n = n / 10;
        }
    }
  
    return flag;
}
  
// Driver code
public static void Main () 
{
    int n = 123454321;
      
    if(isCorrectOrder(n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed by Shashank

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if the 
// digits are in the given order 
  
// Check if the digits follow 
// the correct order 
function isCorrectOrder($n
    $flag = true; 
      
    // to store the prevoius digit 
    $prev = -1; 
      
    // pointer to tell what type of 
    // sequence are we dealing with 
    $type = -1; 
  
    while($n != 0) 
    
        if($type ==-1) 
        
            if($prev ==-1) 
            
                $prev = $n % 10; 
                $n = (int)$n / 10; 
                continue
            
              
            // check if we have same digit 
            // as the prevoius digit 
            if($prev == $n % 10) 
            
                $flag = false; 
                break
            
              
            // checking the peak point 
            // of the number 
            if($prev > $n % 10) 
            
                $type = 1; 
                $prev = $n % 10; 
                $n = (int)$n / 10; 
                continue
            
              
            $prev = $n % 10; 
            $n = (int)$n / 10; 
        
        else
        
            // check if we have same digit 
            // as the prevoius digit 
            if($prev == $n % 10) 
            
                $flag = false; 
                break
            
              
            // check if the digit is greater 
            // than the prevoius one 
            // If true, then break from the 
            // loop as we are in decending
            // order part 
            if($prev < $n % 10) 
            {     
                $flag = false; 
                break
            
              
            $prev = $n % 10; 
            $n = (int) $n / 10; 
        
    
  
    return $flag
  
// Driver code 
$n = 123454321; 
  
if(isCorrectOrder($n)) 
    echo "YES"
else
    echo "NO"
  
// This Code is contributed by ajit
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, Shashank12, Mithun Kumar