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
function func ( n , m , A ) { let ModArray = [ ] ; for ( let i = 0 ; i < n ; i ++ ) ModArray . push ( 0 ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ModArray [ i ] = A [ i ] % m ; sum += ModArray [ i ] ; } sum = sum % m ; if ( sum % m == 0 ) { document . write ( " " ) ; return ; } if ( sum % 2 != 0 ) { document . write ( " " ) ; } else { ModArray . shift ( ) ; let i = 0 ; let j = ModArray . length - 1 ; ModArray . sort ( function ( a , b ) { return a - b } ) ; sum = parseInt ( sum / 2 , 10 ) ; let i1 , i2 ; while ( i <= j ) { let s = ModArray [ i ] + ModArray [ j ] ; if ( s == sum ) { i1 = i ; i2 = j ; document . write ( " " ) ; break ; } else if ( s > sum ) j -- ; else i ++ ; } } } let m = 2 ; let a = [ 1 , 3 , 9 ] ; let n = a . length ; func ( n , m , a ) ;