Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | Java program for the above approach ; 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int countTriplets ( int N , int K ) { if ( K % 2 == 0 ) { int x = N / K ; int y = ( N + ( K / 2 ) ) / K ; return x * x * x + y * y * y ; } else { int x = N / K ; return x * x * x ; } } public static void main ( String [ ] args ) { int N = 2 , K = 2 ; System . out . print ( countTriplets ( N , K ) ) ; } }