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