Skip to content
Related Articles

Related Articles

Count of triplets (a, b, c) in the Array such that a divides b and b divides c

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 May, 2022
Improve Article
Save Article

Given an array arr[] of positive integers of size N, the task is to count number of triplets in the array such that a[i] divides a[j] and a[j] divides a[k] and i < j < k.
Examples:
 

Input: arr[] = {1, 2, 3, 4, 5, 6} 
Output:
Explanation: 
The triplets are: (1, 2, 4), (1, 2, 6), (1, 3, 6).
Input: arr[] = {1, 2, 2} 
Output:
Explanation: 
The triplet is (1, 2, 2) 
 

 

Naive Approach: To solve the problem mentioned above, we will try to implement brute force solution. Traverse the array for all three numbers a[i], a[j] and a[k] and count the number of triplets which satisfies the given condition.
Time complexity: O(N3
Auxiliary Space complexity: O(1)
Efficient Approach: To optimize the above method we can traverse the array for the middle element from index 1 to n-2 and for every middle element we can traverse the left array for a[i] and count number of possible a[i]’s such that a[i] divides a[j]. Similarly, we can traverse in the right array and do the same thing for a[k].
Below is the implementation of the above approach: 
 

C++




// C++ program to find count of triplets
// (a, b, c) in the Array such that
// a divides b and b divides c
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count triplets
int getCount(int arr[], int n)
{
    int count = 0;
 
    // Iterate for middle element
    for (int j = 1; j < n - 1; j++) {
        int p = 0, q = 0;
 
        // Iterate left array for a[i]
        for (int i = 0; i < j; i++) {
 
            if (arr[j] % arr[i] == 0)
                p++;
        }
 
        // Iterate right array for a[k]
        for (int k = j + 1; k < n; k++) {
 
            if (arr[k] % arr[j] == 0)
                q++;
        }
 
        count += p * q;
    }
    // return the final result
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << getCount(arr, N) << endl;
 
    return 0;
}


Java




// Java program to find count of triplets
// (a, b, c) in the Array such that
// a divides b and b divides c
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to count triplets
static int getCount(int arr[], int n)
{
    int count = 0;
 
    // Iterate for middle element
    for(int j = 1; j < n - 1; j++)
    {
       int p = 0, q = 0;
        
       // Iterate left array for a[i]
       for(int i = 0; i < j; i++)
       {
          if (arr[j] % arr[i] == 0)
              p++;
       }
        
       // Iterate right array for a[k]
       for(int k = j + 1; k < n; k++)
       {
          if (arr[k] % arr[j] == 0)
              q++;
       }
        
       count += p * q;
    }
     
    // return the final result
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2 };
    int N = arr.length;
     
    System.out.println(getCount(arr, N));
}
}
 
// This code is contributed by coder001


Python3




# Python3 program to find the count of
# triplets (a, b, c) in the Array such
# that a divides b and b divides c
 
# Function to count triplets
def getCount(arr, n):
    count = 0
 
    # Iterate for middle element
    for j in range(1, n - 1):
        p, q = 0, 0
 
        # Iterate left array for a[i]
        for i in range(j):
 
            if (arr[j] % arr[i] == 0):
                p += 1
 
        # Iterate right array for a[k]
        for k in range(j + 1, n):
 
            if (arr[k] % arr[j] == 0):
                q += 1
 
        count += p * q
         
    # Return the final result
    return count
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 1, 2, 2 ]
    N = len(arr)
     
    print(getCount(arr, N))
     
# This code is contributed by mohit kumar 29   


C#




// C# program to find count of triplets
// (a, b, c) in the Array such that
// a divides b and b divides c
using System;
 
class GFG{
 
// Function to count triplets
public static int getCount(int[] arr, int n)
{
    int count = 0;
 
    // Iterate for middle element
    for(int j = 1; j < n - 1; j++)
    {
        int p = 0, q = 0;
 
        // Iterate left array for a[i]
        for(int i = 0; i < j; i++)
        {
            if (arr[j] % arr[i] == 0)
                p++;
        }
 
        // Iterate right array for a[k]
        for(int k = j + 1; k < n; k++)
        {
            if (arr[k] % arr[j] == 0)
                q++;
        }
        count += p * q;
    }
 
    // return the final result
    return count;
}
 
// Driver code
public static void Main()
{
    int[] arr = { 1, 2, 2 };
    int N = arr.Length;
 
    Console.WriteLine(getCount(arr, N));
}
}
 
// This code is contributed by jrishabh99


Javascript




<script>
 
// Javascript program to find count of triplets
// (a, b, c) in the Array such that
// a divides b and b divides c
 
// Function to count triplets
function getCount(arr, n)
{
    var count = 0;
 
    // Iterate for middle element
    for(var j = 1; j < n - 1; j++)
    {
       var p = 0, q = 0;
        
       // Iterate left array for a[i]
       for(var i = 0; i < j; i++)
       {
          if (arr[j] % arr[i] == 0)
              p++;
       }
        
       // Iterate right array for a[k]
       for(var k = j + 1; k < n; k++)
       {
          if (arr[k] % arr[j] == 0)
              q++;
       }
        
       count += p * q;
    }
     
    // return the final result
    return count;
}
 
// Driver Code
var arr = [ 1, 2, 2 ];
var N = arr.length;
 
document.write(getCount(arr, N));
 
// This code is contributed by Khushboogoyal499
     
</script>


Output: 

1

Time Complexity: O(N2), as we are using a nested loops to traverse N*N times.
Auxiliary Space: O(1), as we are not using any extra space.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!