Storing frequencies of all remainders when divided by M .
var h = Array . from ( { length : M } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( var i = 0 ; i < M ; i ++ ) { for ( var j = i ; j < M ; j ++ ) {