Open In App

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

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:

=> a + b ? b + c ? c + a ? 0(mod K)
=> a+b ? b+c (mod K)
=> a ? c(mod K)



=> 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:

Below is the implementation of the above approach:




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




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




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


Article Tags :