Open In App
Related Articles

Count triplets (a, b, c) such that a + b, b + c and a + c are all divisible by K | Set 2

Improve Article
Improve
Save Article
Save
Like Article
Like

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++




// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
int countTriplets(int N, int K)
{
    // If K is even
    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;
    }
 
    // Otherwise
    else {
        long long int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
int main()
{
    int N = 2, K = 2;
    cout << countTriplets(N, K);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
static int countTriplets(int N, int K)
{
     
    // If K is even
    if (K % 2 == 0)
    {
        int x = N / K;
        int y = (N + (K / 2)) / K;
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else
    {
        int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2, K = 2;
     
    System.out.print(countTriplets(N, K));
}
}
 
// This code is contributed by Kingash


Python3




# Python3 program for the above approach
 
# Function to count the number of
# triplets from the range [1, N - 1]
# having sum of all pairs divisible by K
def countTriplets(N, K):
 
    # If K is even
    if (K % 2 == 0):
        x = N // K
        y = (N + (K // 2)) // K
 
        return x * x * x + y * y * y
 
    # Otherwise
    else:
        x = N // K
        return x * x * x
 
# Driver Code
if __name__ == "__main__":
 
    N = 2
    K = 2
     
    print(countTriplets(N, K))
 
# This code is contributed by ukasp


Javascript




<script>
 
// Javascript program for the above approach 
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
function countTriplets(N, K)
{
     
    // If K is even
    if (K % 2 == 0)
    {
        var x = parseInt(N / K);
        var y = parseInt((N + (K / 2)) / K);
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else
    {
        var x = parseInt(N / K);
        return x * x * x;
    }
}
 
// Driver Code
var N = 2, K = 2;
 
document.write(countTriplets(N, K));
 
// This code is contributed by Ankita saini
 
</script>


C#




// C# program for the above approach
 
using System;
 
class GFG{
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
static int countTriplets(int N, int K)
{
     
    // If K is even
    if (K % 2 == 0)
    {
        int x = N / K;
        int y = (N + (K / 2)) / K;
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else
    {
        int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
    static void Main() {
       int N = 2, K = 2;
     
       Console.Write(countTriplets(N, K));
    }
}
 
// This code is contributed by SoumikMondal


Javascript




<script>
 
    // JavaScript program for the above approach
    // Function to count the number of
    // triplets from the range [1, N - 1]
    // having sum of all pairs divisible by K
    function countTriplets(N,K)
    {
        // If K is even
        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;
        }
 
        // Otherwise
        else {
            let x = parseInt(N / K, 10);
            return x * x * x;
        }
    }
 
    // Driver Code
 
    let N = 2, K = 2;
    document.write(countTriplets(N, K));
     
</script>


Output: 

2

 

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
Previous
Next
Similar Reads
Complete Tutorials