Count digit groupings of a number with given constraints | Function to find the subgroups ; Terminating Condition ; sum of digits ; Traverse all digits from current position to rest of the length of string ; If forward_sum is greater than the previous sum , then call the method again ; Note : We pass current sum as previous sum ; Total number of subgroups till current position ; Driver Code
< ? php function countGroups ( $ position , $ previous_sum , $ length , $ num ) { if ( $ position == $ length ) return 1 ; $ res = 0 ; $ sum = 0 ; for ( $ i = $ position ; $ i < $ length ; $ i ++ ) { $ sum += ( $ num [ $ i ] - '0' ) ; if ( $ sum >= $ previous_sum ) $ res += countGroups ( $ i + 1 , $ sum , $ length , $ num ) ; } return $ res ; } $ num = "1119" ; $ len = strlen ( $ num ) ; echo countGroups ( 0 , 0 , $ len , $ num ) ; ? >