Number of triplets such that each value is less than N and each pair sum is a multiple of K | Function to return the number of triplets ; Initializing the count array ; Storing the frequency of each modulo class ; If K is odd ; If K is even ; Driver Code ; Function Call
function NoofTriplets ( N , K ) { let cnt = Array ( K ) ; for ( let i = 0 ; i < K ; i ++ ) cnt [ i ] = 0 ; for ( let i = 1 ; i <= N ; i += 1 ) { cnt [ i % K ] += 1 ; } if ( K & 1 ) return cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; else { return ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K / 2 ] * cnt [ K / 2 ] * cnt [ K / 2 ] ) ; } } let N = 3 ; let K = 2 ; document . write ( NoofTriplets ( N , K ) ) ;