Skip to content
Related Articles

Related Articles

Count triplets (a, b, c) such that a + b, b + c and a + c are all divisible by K
  • Difficulty Level : Basic
  • Last Updated : 09 Apr, 2021

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:
All possible triplets are 
(1, 1, 1) and (2, 2, 2)
Input: N = 3, K = 2 
Output:
 

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




// C++ implementation of the approach
#include<iostream>
using namespace std;
class gfg
{
    // Function returns the
    // count of the triplets
    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;
 
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                for (l = 1; l <= n; l++)
                {
 
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
 
    // Driver code
    int main()
    {
        gfg g;
        int n = 3;
        int k = 2;
        long ans = g.count_triples(n, k);
        cout << ans;
    }
//This code is contributed by Soumik

Java




// Java implementation of the approach
class GFG {
 
    // Function returns the
    // count of the triplets
    static long count_triples(int n, int k)
    {
        int i = 0, j = 0, l = 0;
        int count = 0;
 
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                for (l = 1; l <= n; l++) {
 
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        int k = 2;
        long ans = count_triples(n, k);
        System.out.println(ans);
    }
}

Python3




# Python3 implementation of the
# above approach
def count_triples(n, k):
     
    count, i, j, l = 0, 0, 0, 0
 
    # Iterate for all triples
    # pairs (i, j, l)
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for l in range(1, n + 1):
                 
                # If the condition
                # is satisfied
                if ((i + j) % k == 0 and
                    (i + l) % k == 0 and
                    (j + l) % k == 0):
                    count += 1
         
    return count
 
# Driver code
if __name__ == "__main__":
     
    n, k = 3, 2
    ans = count_triples(n, k)
    print(ans)
     
# This code is contributed
# by Rituraj Jain

C#




// C# implementation of the approach
 
using System;
 
class GFG {
  
    // Function returns the
    // count of the triplets
    static long count_triples(int n, int k)
    {
        int i = 0, j = 0, l = 0;
        int count = 0;
  
        // iterate for all
        // triples pairs (i, j, l)
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                for (l = 1; l <= n; l++) {
  
                    // if the condition
                    // is satisfied
                    if ((i + j) % k == 0
                        && (i + l) % k == 0
                        && (j + l) % k == 0)
                        count++;
                }
            }
        }
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 3;
        int k = 2;
        long ans = count_triples(n, k);
        Console.WriteLine(ans);
    }
}

PHP




<?php
//PHP implementation of the approach
// Function returns the
// count of the triplets
function count_triples($n, $k)
    {
         $i = 0; $j = 0; $l = 0;
        $count = 0;
 
        // iterate for all
        // triples pairs (i, j, l)
        for ($i = 1; $i <= $n; $i++) {
            for ($j = 1; $j <= $n; $j++) {
                for ($l = 1; $l <= $n; $l++) {
 
                    // if the condition
                    // is satisfied
                    if (($i + $j) % $k == 0
                        && ($i + $l) % $k == 0
                        && ($j + $l) % $k == 0)
                        $count++;
                }
            }
        }
        return $count;
    }
 
    // Driver code
        $n = 3;
        $k = 2;
        $ans = count_triples($n, $k);
        echo ($ans);
     
// This code is contributed by ajit
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to find the quadratic
// equation whose roots are a and b
function count_triples(n, k)
{
    var i = 0, j = 0, l = 0;
    var count = 0;
 
    // iterate for all
    // triples pairs (i, j, l)
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            for(l = 1; l <= n; l++)
            {
 
                // if the condition
                // is satisfied
                if ((i + j) % k == 0
                    && (i + l) % k == 0
                    && (j + l) % k == 0)
                    count++;
            }
        }
    }
    return count;
}
// Driver Code
var n = 3;
var k = 2;
var ans = count_triples(n, k);
 
document.write(ans);
 
// This code is contributed by Khushboogoyal499
     
</script>
Output: 
9

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :