Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | C ++ implementation of the approach ; Function returns the count of the triplets ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver code
#include <iostream> NEW_LINE using namespace std ; class gfg { public : long count_triples ( int n , int k ) ; } ; long gfg :: count_triples ( int n , int k ) { int i = 0 , j = 0 , l = 0 ; int count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } int main ( ) { gfg g ; int n = 3 ; int k = 2 ; long ans = g . count_triples ( n , k ) ; cout << ans ; }