Count triplets (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.
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
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<iostream>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 approachclass 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 approachdef 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 codeif __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 approachusing 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
<?php//PHP implementation of the approach// Function returns the// count of the tripletsfunction count_triples($n, $k) { $i = 0; $j = 0; $l = 0; $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 $n = 3; $k = 2; $ans = count_triples($n, $k); echo ($ans); // This code is contributed by ajit?> |
Javascript
<script>// Javascript implementation of the approach// Function to find the quadratic// equation whose roots are a and bfunction count_triples(n, k){ var i = 0, j = 0, l = 0; var 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 Codevar n = 3;var k = 2;var ans = count_triples(n, k);document.write(ans);// This code is contributed by Khushboogoyal499 </script> |
9


