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 0th modulo class and the (K / 2)th modulo class of 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++
#include <bits/stdc++.h>
using namespace std;
int NoofTriplets( int N, int K)
{
int cnt[K];
memset (cnt, 0, sizeof (cnt));
for ( int i = 1; i <= N; i += 1) {
cnt[i % K] += 1;
}
if (K & 1)
return cnt[0] * cnt[0] * cnt[0];
else {
return (cnt[0] * cnt[0] * cnt[0]
+ cnt[K / 2] * cnt[K / 2] * cnt[K / 2]);
}
}
int main()
{
int N = 3, K = 2;
cout << NoofTriplets(N, K);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int NoofTriplets( int N, int K)
{
int [] cnt = new int [K];
Arrays.fill(cnt, 0 , cnt.length, 0 );
for ( int i = 1 ; i <= N; i += 1 )
{
cnt[i % K] += 1 ;
}
if ((K & 1 ) != 0 )
{
return cnt[ 0 ] * cnt[ 0 ] * cnt[ 0 ];
}
else
{
return (cnt[ 0 ] * cnt[ 0 ] * cnt[ 0 ]
+ cnt[K / 2 ] * cnt[K / 2 ] * cnt[K / 2 ]);
}
}
public static void main(String[] args)
{
int N = 3 , K = 2 ;
System.out.println(NoofTriplets(N, K));
}
}
|
C#
using System;
class GFG
{
static int NoofTriplets( int N, int K)
{
int [] cnt = new int [K];
Array.Fill(cnt, 0, cnt.Length, 0);
for ( int i = 1; i <= N; i += 1)
{
cnt[i % K] += 1;
}
if ((K & 1) != 0)
{
return cnt[0] * cnt[0] * cnt[0];
}
else
{
return (cnt[0] * cnt[0] * cnt[0]
+ cnt[K / 2] * cnt[K / 2] * cnt[K / 2]);
}
}
static public void Main ()
{
int N = 3, K = 2;
Console.Write(NoofTriplets(N, K));
}
}
|
Python3
def NoofTriplets(N, K) :
cnt = [ 0 ] * K;
for i in range ( 1 , N + 1 ) :
cnt[i % K] + = 1 ;
if (K & 1 ) :
rslt = cnt[ 0 ] * cnt[ 0 ] * cnt[ 0 ];
return rslt
else :
rslt = (cnt[ 0 ] * cnt[ 0 ] * cnt[ 0 ] +
cnt[K / / 2 ] * cnt[K / / 2 ] * cnt[K / / 2 ]);
return rslt
if __name__ = = "__main__" :
N = 3 ; K = 2 ;
print (NoofTriplets(N, K));
|
Javascript
<script>
function NoofTriplets(N, K)
{
let cnt = Array(K);
for (let i = 0; i < K; i++)
cnt[i] = 0;
for (let i = 1; i <= N; i += 1)
{
cnt[i % K] += 1;
}
if (K & 1)
return cnt[0] * cnt[0] * cnt[0];
else
{
return (cnt[0] * cnt[0] * cnt[0] +
cnt[K / 2] * cnt[K / 2] *
cnt[K / 2]);
}
}
let N = 3;
let K = 2;
document.write(NoofTriplets(N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(K)