Check if all people can vote on two machines | Returns true if n people can vote using two machines in x time . ; calculate total sum i . e total time taken by all people ; if total time is less than x then all people can definitely vote hence return true ; sort the list ; declare a list presum of same size as that of a and initialize it with 0 ; prefixsum for first element will be element itself ; fill the array ; Set i and j and check if array from i to j - 1 gives sum <= x ; Driver code
def canVote ( a , n , x ) : NEW_LINE INDENT total_sum = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT total_sum += a [ i ] NEW_LINE DEDENT if ( total_sum <= x ) : NEW_LINE INDENT return True NEW_LINE DEDENT a . sort ( ) NEW_LINE presum = [ 0 for i in range ( len ( a ) ) ] NEW_LINE presum [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , len ( presum ) ) : NEW_LINE INDENT presum [ i ] = presum [ i - 1 ] + a [ i ] NEW_LINE DEDENT for i in range ( 0 , len ( presum ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( presum ) ) : NEW_LINE INDENT arr1_sum = ( presum [ i ] + ( total_sum - presum [ j ] ) ) NEW_LINE if ( ( arr1_sum <= x ) and ( total_sum - arr1_sum ) <= x ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT n = 3 NEW_LINE x = 4 NEW_LINE a = [ 2 , 4 , 2 ] NEW_LINE if ( canVote ( a , n , x ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT