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

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: 3
Explanation:
The triplets are: (1, 2, 4), (1, 2, 6), (1, 3, 6).

Input: arr[] = {1, 2, 2}
Output: 1
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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    

chevron_right


Output:

1

Time complexity: O(N2)

Auxiliary Space complexity: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.