human
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"
Solution in Python:
assistant
#Here is code to maximize occurrences of values between L and R on sequential addition of Array elements with modulo H and the other steps required.
 def goodInteger ( arr , n , h , l , r ) :
	dp = [ [ - 1 for i in range ( h ) ] for j in range ( n + 1 ) ]
	dp [ 0 ] [ 0 ] = 0
	for i in range ( n ) :
		for j in range ( h ) :
			if ( dp [ i ] [ j ] != - 1 ) :
				h1 = ( j + arr [ i ] ) % h
				h2 = ( j + arr [ i ] - 1 ) % h
				dp [ i + 1 ] [ h1 ] = max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( h1 >= l and h1 <= r ) )
				dp [ i + 1 ] [ h2 ] = max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( h2 >= l and h2 <= r ) )
	ans = 0
	for i in range ( h ) :
		if ( dp [ n ] [ i ] != - 1 ) :
			ans = max ( ans , dp [ n ] [ i ] )
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	A = [ 16 , 17 , 14 , 20 , 20 , 11 , 22 ]
	H = 24
	L = 21
	R = 23
	size = len ( A )
	goodInteger ( A , size , H , L , R )