# Number of triplets such that each value is less than N and each pair sum is a multiple of K

Given two integers **N** and **K**. Find the numbers of triplets **(a, b, c)** such that **0 ≤ a, b, c ≤ N** and **(a + b)**, **(b + c)** and **(c + a)** are multiples of **K**.

**Examples:**

Input:N = 3, K = 2

Output:9

Triplets possible are:

{(1, 1, 1), (1, 1, 3), (1, 3, 1)

(1, 3, 3), (2, 2, 2), (3, 1, 1)

(3, 1, 1), (3, 1, 3), (3, 3, 3)}

Input:N = 5, K = 3

Output:1

Only possible triplet is (3, 3, 3)

**Approach:** Given that **(a + b)**, **(b + c)** and **(c + a)** are multiples of **K**. Hence, we can say that **(a + b) % K = 0**, **(b + c) % K = 0** and **(c + a) % K = 0**.

If **a** belongs to the **x** modulo class of **K** then **b** should be in the **(K – x)th** modulo class using the first condition.

From the second condition, it can be seen that **c** belongs to the **x** modulo class of **K**. Now as both **a** and **c** belong to the same modulo class and they have to satisfy the third relation which is **(a + c) % K = 0**. It could be only possible if **x = 0** or **x = K / 2**.

When **K** is an odd integer, **x = K / 2** is not valid.

Hence to solve the problem, count the number of elements from **0** to **N** in the **0 ^{th}** modulo class and the

**(K / 2)**modulo class of

^{th}**K**.

- If
**K**is**odd**then the result is**cnt[0]**^{3} - If
**K**is**even**then the result is**cnt[0]**.^{3}+ cnt[K / 2]^{3}

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of triplets ` `int` `NoofTriplets(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `cnt[K]; ` ` ` ` ` `// Initializing the count array ` ` ` `memset` `(cnt, 0, ` `sizeof` `(cnt)); ` ` ` ` ` `// Storing the frequency of each modulo class ` ` ` `for` `(` `int` `i = 1; i <= N; i += 1) { ` ` ` `cnt[i % K] += 1; ` ` ` `} ` ` ` ` ` `// If K is odd ` ` ` `if` `(K & 1) ` ` ` `return` `cnt[0] * cnt[0] * cnt[0]; ` ` ` ` ` `// If K is even ` ` ` `else` `{ ` ` ` `return` `(cnt[0] * cnt[0] * cnt[0] ` ` ` `+ cnt[K / 2] * cnt[K / 2] * cnt[K / 2]); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3, K = 2; ` ` ` ` ` `// Function Call ` ` ` `cout << NoofTriplets(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.Arrays; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number of triplets ` ` ` `static` `int` `NoofTriplets(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `int` `[] cnt = ` `new` `int` `[K]; ` ` ` ` ` `// Initializing the count array ` ` ` `Arrays.fill(cnt, ` `0` `, cnt.length, ` `0` `); ` ` ` ` ` `// Storing the frequency of each modulo class ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i += ` `1` `) ` ` ` `{ ` ` ` `cnt[i % K] += ` `1` `; ` ` ` `} ` ` ` ` ` `// If K is odd ` ` ` `if` `((K & ` `1` `) != ` `0` `) ` ` ` `{ ` ` ` `return` `cnt[` `0` `] * cnt[` `0` `] * cnt[` `0` `]; ` ` ` `} ` ` ` `// If K is even ` ` ` `else` ` ` `{ ` ` ` `return` `(cnt[` `0` `] * cnt[` `0` `] * cnt[` `0` `] ` ` ` `+ cnt[K / ` `2` `] * cnt[K / ` `2` `] * cnt[K / ` `2` `]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `N = ` `3` `, K = ` `2` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.println(NoofTriplets(N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number of triplets ` ` ` `static` `int` `NoofTriplets(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `int` `[] cnt = ` `new` `int` `[K]; ` ` ` ` ` `// Initializing the count array ` ` ` `Array.Fill(cnt, 0, cnt.Length, 0); ` ` ` ` ` `// Storing the frequency of each modulo class ` ` ` `for` `(` `int` `i = 1; i <= N; i += 1) ` ` ` `{ ` ` ` `cnt[i % K] += 1; ` ` ` `} ` ` ` ` ` `// If K is odd ` ` ` `if` `((K & 1) != 0) ` ` ` `{ ` ` ` `return` `cnt[0] * cnt[0] * cnt[0]; ` ` ` `} ` ` ` `// If K is even ` ` ` `else` ` ` `{ ` ` ` `return` `(cnt[0] * cnt[0] * cnt[0] ` ` ` `+ cnt[K / 2] * cnt[K / 2] * cnt[K / 2]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 3, K = 2; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(NoofTriplets(N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to return the number of triplets ` `def` `NoofTriplets(N, K) : ` ` ` ` ` `# Initializing the count array ` ` ` `cnt ` `=` `[` `0` `]` `*` `K; ` ` ` ` ` `# Storing the frequency of each modulo class ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `) : ` ` ` `cnt[i ` `%` `K] ` `+` `=` `1` `; ` ` ` ` ` `# If K is odd ` ` ` `if` `(K & ` `1` `) : ` ` ` `rslt ` `=` `cnt[` `0` `] ` `*` `cnt[` `0` `] ` `*` `cnt[` `0` `]; ` ` ` `return` `rslt ` ` ` ` ` `# If K is even ` ` ` `else` `: ` ` ` `rslt ` `=` `(cnt[` `0` `] ` `*` `cnt[` `0` `] ` `*` `cnt[` `0` `] ` `+` ` ` `cnt[K ` `/` `/` `2` `] ` `*` `cnt[K ` `/` `/` `2` `] ` `*` `cnt[K ` `/` `/` `2` `]); ` ` ` `return` `rslt ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `3` `; K ` `=` `2` `; ` ` ` ` ` `# Function Call ` ` ` `print` `(NoofTriplets(N, K)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Nth number whose sum of digit is multiple of 10
- Round the given number to nearest multiple of 10 | Set-2
- Check if a number is multiple of 5 without using / and % operators
- Round the given number to nearest multiple of 10
- Smallest N digit number which is a multiple of 5
- n'th multiple of a number in Fibonacci Series
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Check whether a very large number of the given form is a multiple of 3.
- Write an Efficient Method to Check if a Number is Multiple of 3
- Smallest multiple of N with exactly N digits in its Binary number representation
- Greatest number that can be formed from a pair in a given Array
- Generate Pythagorean Triplets
- Find sum of xor of all unordered triplets of the array
- Count non-negative triplets with sum equal to N
- Count Triplets such that one of the numbers can be written as sum of the other two

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.