human
Maximize array elements upto given number | variable to store maximum value that can be obtained ..
If entire array is traversed , then compare current value in num to overall maximum obtained so far ..
Case 1 : Subtract current element from value so far if result is greater than or equal to zero ..
Case 2 : Add current element to value so far if result is less than or equal to maxLimit ..
def to find maximum possible value that can be obtained using array elements and given number ..
variable to store current index position ..
call to utility def to find maximum possible value that can be obtained ..
Driver code"
How can the above be solved in Python?
 Write me a code to maximize array elements upto given number and the other steps required?
assistant
#Here is code to maximize array elements upto given number and the other steps required.
 ans = 0 ;
def findMaxValUtil ( arr , n , num , maxLimit , ind ) :
	global ans
	if ( ind == n ) :
		ans = max ( ans , num )
		return
	if ( num - arr [ ind ] >= 0 ) :
		findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 )
	if ( num + arr [ ind ] <= maxLimit ) :
		findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 )
def findMaxVal ( arr , n , num , maxLimit ) :
	global ans
	ind = 0
	findMaxValUtil ( arr , n , num , maxLimit , ind )
	return ans
num = 1
arr = [ 3 , 10 , 6 , 4 , 5 ]
n = len ( arr )
maxLimit = 15
print ( findMaxVal ( arr , n , num , maxLimit ) )