Check if any valid sequence is divisible by M | Function to check if any valid sequence is divisible by M ; DEclare mod array ; Calculate the mod array ; Check if sum is divisible by M ; Check if sum is not divisible by 2 ; Remove the first element from the ModArray since it is not possible to place minus on the first element ; Decrease the size of array ; Sort the array ; Loop until the pointer cross each other ; Check if sum becomes equal ; Increase and decrease the pointer accordingly ; Driver code ; Function call
def func ( n , m , A ) : NEW_LINE INDENT ModArray = [ 0 ] * n NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ModArray [ i ] = A [ i ] % m NEW_LINE Sum += ModArray [ i ] NEW_LINE DEDENT Sum = Sum % m NEW_LINE if ( Sum % m == 0 ) : NEW_LINE INDENT print ( " True " ) NEW_LINE return NEW_LINE DEDENT if ( Sum % 2 != 0 ) : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT else : NEW_LINE INDENT ModArray . pop ( 0 ) NEW_LINE i = 0 NEW_LINE j = len ( ModArray ) - 1 NEW_LINE ModArray . sort ( ) NEW_LINE Sum = Sum // 2 NEW_LINE while ( i <= j ) : NEW_LINE INDENT s = ModArray [ i ] + ModArray [ j ] NEW_LINE if ( s == Sum ) : NEW_LINE INDENT i1 = i NEW_LINE i2 = j NEW_LINE print ( " True " ) NEW_LINE break NEW_LINE DEDENT elif ( s > Sum ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT m = 2 NEW_LINE a = [ 1 , 3 , 9 ] NEW_LINE n = len ( a ) NEW_LINE func ( n , m , a ) NEW_LINE