Minimum number of sets with numbers less than Y | Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver Code
< ? php function minimumSets ( $ s , $ y ) { $ cnt = 0 ; $ num = 0 ; $ l = strlen ( $ s ) ; $ f = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; if ( $ num <= $ y ) $ f = 1 ; { if ( $ f ) $ cnt += 1 ; $ num = $ s [ $ i ] - '0' ; $ f = 0 ; if ( $ num <= $ y ) $ f = 1 ; else $ num = 0 ; } } if ( $ f ) $ cnt += 1 ; return $ cnt ; } $ s = "1234" ; $ y = 30 ; echo ( minimumSets ( $ s , $ y ) ) ; ? >