Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code
function countTriplets ( N , K ) { if ( K % 2 == 0 ) { var x = parseInt ( N / K ) ; var y = parseInt ( ( N + ( K / 2 ) ) / K ) ; return x * x * x + y * y * y ; } else { var x = parseInt ( N / K ) ; return x * x * x ; } } var N = 2 , K = 2 ; document . write ( countTriplets ( N , K ) ) ;