Count triplets (a, b, c) such that a + b, b + c and a + c are all divisible by K
Last Updated :
16 Sep, 2022
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: 2
Explanation: 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++
#include<iostream>
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;
}
|
Java
class GFG {
static long 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;
}
public static void main(String[] args)
{
int n = 3 ;
int k = 2 ;
long ans = count_triples(n, k);
System.out.println(ans);
}
}
|
Python3
def count_triples(n, k):
count, i, j, l = 0 , 0 , 0 , 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 , n + 1 ):
for l in range ( 1 , n + 1 ):
if ((i + j) % k = = 0 and
(i + l) % k = = 0 and
(j + l) % k = = 0 ):
count + = 1
return count
if __name__ = = "__main__" :
n, k = 3 , 2
ans = count_triples(n, k)
print (ans)
|
C#
using System;
class GFG {
static long 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;
}
public static void Main()
{
int n = 3;
int k = 2;
long ans = count_triples(n, k);
Console.WriteLine(ans);
}
}
|
PHP
<?php
function count_triples( $n , $k )
{
$i = 0; $j = 0; $l = 0;
$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 ;
}
$n = 3;
$k = 2;
$ans = count_triples( $n , $k );
echo ( $ans );
?>
|
Javascript
<script>
function count_triples(n, k)
{
var i = 0, j = 0, l = 0;
var 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;
}
var n = 3;
var k = 2;
var ans = count_triples(n, k);
document.write(ans);
</script>
|
Complexity Analysis:
- Time Complexity: O(n3), since three loops are nested inside each other
- Auxiliary Space: O(1), since no extra array is used so constant space is used
Share your thoughts in the comments
Please Login to comment...