给定大小为N的正整数的数组arr [] ,任务是对数组中的三元组数进行计数,以使a [i]除以a [j],a [j]除以a [k],并且i
例子:
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)
天真的方法:为解决上述问题,我们将尝试实施蛮力解决方案。遍历所有三个数字a [i],a [j]和a [k]的数组,并计算满足给定条件的三元组的数量。
时间复杂度: O(N 3 )
辅助空间复杂度: O(1)
高效方法:为优化上述方法,我们可以遍历索引从索引1到n-2的中间元素的数组,并且对于每个中间元素,我们都可以遍历左侧数组a [i]并计算可能的a [i]’的数量使得a [i]除以a [j]。同样,我们可以遍历正确的数组并对a [k]执行相同的操作。
下面是上述方法的实现:
C++
// C++ program to find count of triplets
// (a, b, c) in the Array such that
// a divides b and b divides c
#include
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
输出:
1
时间复杂度: O(N 2 )
辅助空间复杂度: O(1)