Given two positive integers N and K, the task is to count the number of triplets (a, b, c) such that 0 < a, b, c < N and (a + b), (b + c) and (c + a) are all multiples of K.
Examples:
Input: N = 2, K = 2
Output: 2
Explanation: All possible triplets that satisfy the given property are (1, 1, 1) and (2, 2, 2).
Therefore, the total count is 2.
Input: N = 3, K = 2
Output: 9
Naive Approach: Refer to the previous post for the simplest approach to solve this problem.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the following observations:
- The given condition is expressed by a congruence formula:
=> a + b ? b + c ? c + a ? 0(mod K)
=> a+b ? b+c (mod K)
=> a ? c(mod K)
- The above relation can also be observed without using the congruence formula as:
- As (a + b) is a multiple of K and (c + b) is a multiple of K. Therefore, (a + b) ? (c + b) = a – c is also a multiple of K, i.e., a ? b ? c (mod K).
- Therefore, the expression can be further evaluated to:
=> a + b ? 0 (mod K)
=> a + a ? 0 (mod K) (since a is congruent to b)
=> 2a ? 0 (mod K)
From the above observations, the result can be calculated for the following two cases:
- If K is odd, then a ? b ? c ? 0(mod K) since all three are congruent and the total number of triplets can be calculated as (N / K)3.
- If K is even, then K is divisible by 2 and a ? 0 (mod K), b ? 0 (mod K), and c ? 0 (mod K). Therefore, the total number of triplets can be calculated as (N / K)3 ((N + (K/2)) / K)3.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int countTriplets( int N, int K)
{
if (K % 2 == 0) {
long long int x = N / K;
long long int y = (N + (K / 2)) / K;
return x * x * x + y * y * y;
}
else {
long long int x = N / K;
return x * x * x;
}
}
int main()
{
int N = 2, K = 2;
cout << countTriplets(N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int countTriplets( int N, int K)
{
if (K % 2 == 0 )
{
int x = N / K;
int y = (N + (K / 2 )) / K;
return x * x * x + y * y * y;
}
else
{
int x = N / K;
return x * x * x;
}
}
public static void main(String[] args)
{
int N = 2 , K = 2 ;
System.out.print(countTriplets(N, K));
}
}
|
Python3
def countTriplets(N, K):
if (K % 2 = = 0 ):
x = N / / K
y = (N + (K / / 2 )) / / K
return x * x * x + y * y * y
else :
x = N / / K
return x * x * x
if __name__ = = "__main__" :
N = 2
K = 2
print (countTriplets(N, K))
|
Javascript
<script>
function countTriplets(N, K)
{
if (K % 2 == 0)
{
var x = parseInt(N / K);
var y = parseInt((N + (K / 2)) / K);
return x * x * x + y * y * y;
}
else
{
var x = parseInt(N / K);
return x * x * x;
}
}
var N = 2, K = 2;
document.write(countTriplets(N, K));
</script>
|
C#
using System;
class GFG{
static int countTriplets( int N, int K)
{
if (K % 2 == 0)
{
int x = N / K;
int y = (N + (K / 2)) / K;
return x * x * x + y * y * y;
}
else
{
int x = N / K;
return x * x * x;
}
}
static void Main() {
int N = 2, K = 2;
Console.Write(countTriplets(N, K));
}
}
|
Javascript
<script>
function countTriplets(N,K)
{
if (K % 2 == 0) {
let x = parseInt(N / K, 10);
let y = parseInt((N + parseInt(K / 2, 10)) / K, 10);
return x * x * x + y * y * y;
}
else {
let x = parseInt(N / K, 10);
return x * x * x;
}
}
let N = 2, K = 2;
document.write(countTriplets(N, K));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Jul, 2021
Like Article
Save Article