Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | Function that prints the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code
def goodInteger ( arr , n , h , l , r ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( h ) ] for j in range ( n + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( h ) : NEW_LINE INDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT h1 = ( j + arr [ i ] ) % h NEW_LINE h2 = ( j + arr [ i ] - 1 ) % h NEW_LINE dp [ i + 1 ] [ h1 ] = max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( h1 >= l and h1 <= r ) ) NEW_LINE dp [ i + 1 ] [ h2 ] = max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( h2 >= l and h2 <= r ) ) NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( h ) : NEW_LINE INDENT if ( dp [ n ] [ i ] != - 1 ) : NEW_LINE INDENT ans = max ( ans , dp [ n ] [ i ] ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 16 , 17 , 14 , 20 , 20 , 11 , 22 ] NEW_LINE H = 24 NEW_LINE L = 21 NEW_LINE R = 23 NEW_LINE size = len ( A ) NEW_LINE goodInteger ( A , size , H , L , R ) NEW_LINE DEDENT