Check if a number has digits in the given Order - GeeksforGeeks

11 Aug, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 orderbool isCorrectOrder(int n){    bool flag = true;         // to store the previous 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 previous 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 previous digit            if(prev == n % 10)            {                  flag = false;                break;            }            // check if the digit is greater than            // the previous one            // If true, then break from the loop as            // we are in descending order part            if(prev < n % 10)            {                          flag = false;                break;            }                          prev = n % 10;            n = n / 10;        }    }     return flag;} // Driver codeint main(){    int n = 123454321;         if(isCorrectOrder(n))        cout<<"YES";    else        cout<<"NO";         return 0;}
// Java program to check if the digits are in// the given orderimport java.io.*; class GFG { // Check if the digits follow the correct orderstatic boolean isCorrectOrder(int n){    boolean flag = true;         // to store the previous 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 previous 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 previous digit            if(prev == n % 10)            {                flag = false;                break;            }            // check if the digit is greater than            // the previous one            // If true, then break from the loop as            // we are in descending 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
# Python3 program to check if the# digits are in the given order # Check if the digits follow# the correct orderdef isCorrectOrder(n):     flag = True;         # to store the previous 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 previous 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 previous digit            if(prev == n % 10):                flag = False;                break;                         # check if the digit is greater            # than the previous one            # If true, then break from the            # loop as we are in descending            # order part            if(prev < n % 10):                flag = False;                break;                         prev = n % 10;            n = int(n / 10);     return flag; # Driver coden = 123454321; if(isCorrectOrder(n)):    print("YES");else:    print("NO"); # This Code is contributed by mits
// C# program to check if the// digits are in the given orderusing System; class GFG{ // Check if the digits follow// the correct orderstatic bool isCorrectOrder(int n){    bool flag = true;         // to store the previous 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 previous 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 previous digit            if(prev == n % 10)            {                flag = false;                break;            }                         // check if the digit is greater            // than the previous one            // If true, then break from the            // loop as we are in descending            // order part            if(prev < n % 10)            {                    flag = false;                break;            }                         prev = n % 10;            n = n / 10;        }    }     return flag;} // Driver codepublic static void Main (){    int n = 123454321;         if(isCorrectOrder(n))        Console.WriteLine("YES");    else        Console.WriteLine("NO");}} // This code is contributed by Shashank
<?php// PHP program to check if the// digits are in the given order // Check if the digits follow// the correct orderfunction isCorrectOrder($n){    $flag = true;         // to store the previous 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 previous 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 previous digit            if($prev == $n % 10)            {                $flag = false;                break;            }                         // check if the digit is greater            // than the previous one            // If true, then break from the            // loop as we are in descending            // 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?>
<script> // JavaScript program to check if the digits are in// the given order   // Check if the digits follow the correct orderfunction isCorrectOrder(n){    let flag = true;           // to store the previous digit    let prev = -1;            // pointer to tell what type of sequence     // are we dealing with    let type = -1;        while(n != 0)    {        if(type ===-1)        {            if(prev ===-1)            {                prev = n % 10;                n = Math.floor(n/10);                continue;            }            // check if we have same digit             // as the previous 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 = Math.floor(n/10);                continue;            }                           prev = n % 10;            n = Math.floor(n / 10);        }        else        {               // check if we have same digit            // as the previous digit            if(prev == n % 10)             {                   flag = false;                break;            }            // check if the digit is greater than             // the previous one             // If true, then break from the loop as             // we are in descending order part            if(prev < n % 10)             {                           flag = false;                break;            }                            prev = n % 10;            n = Math.floor(n / 10);        }    }       return flag;}   // Driver code     let n = 123454321;           if(isCorrectOrder(n))        document.write("YES");    else        document.write("NO");        // This code is contributed by Surbhi Tyagi </script>
YES
 
Time Complexity: O(logN)Auxiliary Space: O(1) 
jit_t
Shashank12
Mithun Kumar
surbhityagi15
arorakashish0911
pankajsharmagfg
number-theory
Technical Scripter 2018
Algorithms
Greedy
Mathematical
Technical Scripter
number-theory
Greedy
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Understanding Time Complexity with Simple Examples
Introduction to Algorithms
How to write a Pseudo Code?
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Huffman Coding | Greedy Algo-3