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

• Difficulty Level : Basic
• Last Updated : 09 Apr, 2021

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.
Examples:

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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

 ``

## Javascript

 ``
Output:
`9`

My Personal Notes arrow_drop_up