Count of triplets (a, b, c) in the Array such that a divides b and b divides c
Last Updated :
01 Oct, 2023
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)
Brute Force Approach:
Initialize a variable count to 0.
Traverse the array from 0 to n-3.
For each i-th element of the array, traverse the array from i+1 to n-2.
For each j-th element of the array, traverse the array from j+1 to n-1.
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.
Return the count variable as the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getCount(int arr[], int n)
{
int count = 0;
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++) {
if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0) {
count++;
}
}
}
}
return count;
}
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 {
static int getCount(int arr[], int n) {
int count = 0;
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++) {
if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0) {
count++;
}
}
}
}
return count;
}
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
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
if arr[j] % arr[i] == 0 and arr[k] % arr[j] == 0:
count += 1
return count
arr = [1, 2, 2]
print(get_count(arr))
C#
using System;
class Program
{
static int GetCount(int[] arr)
{
int count = 0;
int n = arr.Length;
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++)
{
if (arr[j] % arr[i] == 0 && arr[k] % arr[j] == 0)
{
count++;
}
}
}
}
return count;
}
static void Main(string[] args)
{
int[] arr = { 1, 2, 2 };
Console.WriteLine(GetCount(arr));
}
}
Javascript
function getCount(arr) {
let count = 0;
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++) {
if (arr[j] % arr[i] === 0 && arr[k] % arr[j] === 0) {
count++;
}
}
}
}
return count;
}
const arr = [1, 2, 2];
console.log(getCount(arr));
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++
#include <bits/stdc++.h>
using namespace std;
int getCount(int arr[], int n)
{
int count = 0;
for (int j = 1; j < n - 1; j++) {
int p = 0, q = 0;
for (int i = 0; i < j; i++) {
if (arr[j] % arr[i] == 0)
p++;
}
for (int k = j + 1; k < n; k++) {
if (arr[k] % arr[j] == 0)
q++;
}
count += p * q;
}
return count;
}
int main()
{
int arr[] = { 1, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getCount(arr, N) << endl;
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG {
static int getCount(int arr[], int n)
{
int count = 0;
for(int j = 1; j < n - 1; j++)
{
int p = 0, q = 0;
for(int i = 0; i < j; i++)
{
if (arr[j] % arr[i] == 0)
p++;
}
for(int k = j + 1; k < n; k++)
{
if (arr[k] % arr[j] == 0)
q++;
}
count += p * q;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1, 2, 2 };
int N = arr.length;
System.out.println(getCount(arr, N));
}
}
Python3
def getCount(arr, n):
count = 0
for j in range(1, n - 1):
p, q = 0, 0
for i in range(j):
if (arr[j] % arr[i] == 0):
p += 1
for k in range(j + 1, n):
if (arr[k] % arr[j] == 0):
q += 1
count += p * q
return count
if __name__ == '__main__':
arr = [ 1, 2, 2 ]
N = len(arr)
print(getCount(arr, N))
C#
using System;
class GFG{
public static int getCount(int[] arr, int n)
{
int count = 0;
for(int j = 1; j < n - 1; j++)
{
int p = 0, q = 0;
for(int i = 0; i < j; i++)
{
if (arr[j] % arr[i] == 0)
p++;
}
for(int k = j + 1; k < n; k++)
{
if (arr[k] % arr[j] == 0)
q++;
}
count += p * q;
}
return count;
}
public static void Main()
{
int[] arr = { 1, 2, 2 };
int N = arr.Length;
Console.WriteLine(getCount(arr, N));
}
}
Javascript
<script>
function getCount(arr, n)
{
var count = 0;
for(var j = 1; j < n - 1; j++)
{
var p = 0, q = 0;
for(var i = 0; i < j; i++)
{
if (arr[j] % arr[i] == 0)
p++;
}
for(var k = j + 1; k < n; k++)
{
if (arr[k] % arr[j] == 0)
q++;
}
count += p * q;
}
return count;
}
var arr = [ 1, 2, 2 ];
var N = arr.length;
document.write(getCount(arr, N));
</script>
Time Complexity: O(N 2 ), as we are using a nested loops to traverse N*N times. Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...