# Triplet pair (a, b, c) such that a+b, b+c and a+c are all divisible by K

Given two integers ‘N’ and ‘K’, the task is to count the number of triplets (a, b, c) of positive integers not greater than ‘N’ such that ‘a+b’, ‘b+c’ and ‘c+a’ are all multiples of ‘K’. Note that ‘a’, ‘b’ and ‘c’ may or may not be the same in a triplet pair.

Examples:

Input: N = 2, K = 2
Output: 1
All possible triplets are
(1, 1, 1) and (2, 2, 2)

Input: N = 3, K = 2
Output: 9

Approach: Run three nested loops from ‘1’ to ‘N’ and check whether `i+j, j+l and l+i` are all divisible by ‘K’. Increment the count if the condition is true.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `class` `gfg ` `{ ` `    ``// Function returns the ` `    ``// count of the triplets ` `    ``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; ` ` `  `        ``// iterate for all ` `        ``// triples pairs (i, j, l) ` `        ``for` `(i = 1; i <= n; i++) ` `        ``{ ` `            ``for` `(j = 1; j <= n; j++) ` `            ``{ ` `                ``for` `(l = 1; l <= n; l++) ` `                ``{ ` ` `  `                    ``// if the condition ` `                    ``// is satisfied ` `                    ``if` `((i + j) % k == 0 ` `                        ``&& (i + l) % k == 0 ` `                        ``&& (j + l) % k == 0) ` `                        ``count++; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``int` `main() ` `    ``{ ` `        ``gfg g; ` `        ``int` `n = 3; ` `        ``int` `k = 2; ` `        ``long` `ans = g.count_triples(n, k); ` `        ``cout << ans; ` `    ``} ` `//This code is contributed by Soumik `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function returns the ` `    ``// count of the triplets ` `    ``static` `long` `count_triples(``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `i = ``0``, j = ``0``, l = ``0``; ` `        ``int` `count = ``0``; ` ` `  `        ``// iterate for all ` `        ``// triples pairs (i, j, l) ` `        ``for` `(i = ``1``; i <= n; i++) { ` `            ``for` `(j = ``1``; j <= n; j++) { ` `                ``for` `(l = ``1``; l <= n; l++) { ` ` `  `                    ``// if the condition ` `                    ``// is satisfied ` `                    ``if` `((i + j) % k == ``0` `                        ``&& (i + l) % k == ``0` `                        ``&& (j + l) % k == ``0``) ` `                        ``count++; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``3``; ` `        ``int` `k = ``2``; ` `        ``long` `ans = count_triples(n, k); ` `        ``System.out.println(ans); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the  ` `# above approach ` `def` `count_triples(n, k):  ` `     `  `    ``count, i, j, l ``=` `0``, ``0``, ``0``, ``0` ` `  `    ``# Iterate for all triples  ` `    ``# pairs (i, j, l)  ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``for` `j ``in` `range``(``1``, n ``+` `1``):  ` `            ``for` `l ``in` `range``(``1``, n ``+` `1``):  ` `                 `  `                ``# If the condition  ` `                ``# is satisfied  ` `                ``if` `((i ``+` `j) ``%` `k ``=``=` `0` `and` `                    ``(i ``+` `l) ``%` `k ``=``=` `0` `and` `                    ``(j ``+` `l) ``%` `k ``=``=` `0``):  ` `                    ``count ``+``=` `1` `         `  `    ``return` `count  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` `     `  `    ``n, k ``=` `3``, ``2` `    ``ans ``=` `count_triples(n, k)  ` `    ``print``(ans)  ` `     `  `# This code is contributed  ` `# by Rituraj Jain `

## C#

 `// C# implementation of the approach ` ` `  `using` `System; ` ` `  `class` `GFG { ` `  `  `    ``// Function returns the ` `    ``// count of the triplets ` `    ``static` `long` `count_triples(``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `i = 0, j = 0, l = 0; ` `        ``int` `count = 0; ` `  `  `        ``// iterate for all ` `        ``// triples pairs (i, j, l) ` `        ``for` `(i = 1; i <= n; i++) { ` `            ``for` `(j = 1; j <= n; j++) { ` `                ``for` `(l = 1; l <= n; l++) { ` `  `  `                    ``// if the condition ` `                    ``// is satisfied ` `                    ``if` `((i + j) % k == 0 ` `                        ``&& (i + l) % k == 0 ` `                        ``&& (j + l) % k == 0) ` `                        ``count++; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` `  `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 3; ` `        ``int` `k = 2; ` `        ``long` `ans = count_triples(n, k); ` `        ``Console.WriteLine(ans); ` `    ``} ` `} `

## PHP

 ` `

Output:

```9
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.