Smallest index such that there are no 0 or 1 to its right | Function to find the smallest index such that there are no 0 or 1 to its right ; Initially ; Traverse in the array ; Check if array element is 1 ; a [ i ] = 0 ; Return minimum of both ; Driver code
< ? php function smallestIndex ( $ a , $ n ) { $ right1 = 0 ; $ right0 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 1 ) $ right1 = $ i ; else $ right0 = $ i ; } return min ( $ right1 , $ right0 ) ; } $ a = array ( 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ a ) ; echo smallestIndex ( $ a , $ n ) ; ? >