Covering maximum array elements with given value | Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; Driver function
def maxArrayCover ( a , n , x ) : NEW_LINE INDENT a . sort ( ) NEW_LINE cc = 0 NEW_LINE s = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += a [ i ] NEW_LINE if ( s > x ) : NEW_LINE INDENT break NEW_LINE DEDENT cc += 1 NEW_LINE DEDENT if ( sum ( a ) == x ) : NEW_LINE INDENT return n NEW_LINE DEDENT else : NEW_LINE INDENT if ( cc == n ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return cc NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , x = 3 , 70 NEW_LINE a = [ 10 , 20 , 30 ] NEW_LINE print ( maxArrayCover ( a , n , x ) ) NEW_LINE DEDENT