Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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) 
 

 

Brute Force Approach:

  1. Initialize a variable count to 0.
  2. Traverse the array from 0 to n-3.
  3. For each i-th element of the array, traverse the array from i+1 to n-2.
  4. For each j-th element of the array, traverse the array from j+1 to n-1.
  5. For each k-th element of the array, if arr[k] is divisible by arr[j] and arr[j] is divisible by arr[i], then increment the count variable by 1.
  6. Return the count variable as the final answer.
     

Below is the implementation of the above approach: 

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to count triplets
int getCount(int arr[], int n)
{
    int count = 0;
 
    // consider all possible triplets (i, j, k)
    for (int i = 0; i < n - 2; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            for (int k = j + 1; k < n; k++) {
                // check if a[i] divides a[j] and a[j] divides a[k]
                if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0) {
                    count++;
                }
            }
        }
    }
 
    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




import java.util.*;
 
public class Main {
     
    // Function to count triplets
    static int getCount(int arr[], int n) {
        int count = 0;
 
        // consider all possible triplets (i, j, k)
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                for (int k = j + 1; k < n; k++) {
                    // check if a[i] divides a[j] and a[j] divides a[k]
                    if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0) {
                        count++;
                    }
                }
            }
        }
 
        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));
    }
}


Python3




def get_count(arr):
    n = len(arr)
    count = 0
 
    # consider all possible triplets (i, j, k)
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                # check if a[i] divides a[j] and a[j] divides a[k]
                if arr[j] % arr[i] == 0 and arr[k] % arr[j] == 0:
                    count += 1
 
    return count
 
# Driver code
arr = [1, 2, 2]
print(get_count(arr))


C#




using System;
 
class Program
{
    // Function to count triplets
    static int GetCount(int[] arr)
    {
        int count = 0;
        int n = arr.Length;
 
        // Consider all possible triplets (i, j, k)
        for (int i = 0; i < n - 2; i++)
        {
            for (int j = i + 1; j < n - 1; j++)
            {
                for (int k = j + 1; k < n; k++)
                {
                    // Check if arr[i] divides arr[j] and arr[j] divides arr[k]
                    if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0)
                    {
                        count++;
                    }
                }
            }
        }
 
        return count;
    }
 
    // Driver code
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 2 };
        Console.WriteLine(GetCount(arr));
    }
}


Javascript




// Function to count triplets
function getCount(arr) {
    let count = 0;
 
    // consider all possible triplets (i, j, k)
    for (let i = 0; i < arr.length - 2; i++) {
        for (let j = i + 1; j < arr.length - 1; j++) {
            for (let k = j + 1; k < arr.length; k++) {
                // check if arr[i] divides arr[j] and arr[j] divides arr[k]
                if (arr[j] % arr[i] === 0 && arr[k] % arr[j] === 0) {
                    count++;
                }
            }
        }
    }
 
    return count;
}
 
// Driver code
const arr = [1, 2, 2];
console.log(getCount(arr));


Output

1





Time Complexity: O(N^3), where N is the size of the array

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.



Last Updated : 01 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads