给定一个大小为N的正整数数组 arr[] ,任务是计算数组中三元组的数量,使得a[i] 除以 a[j] 和 a[j] 除以 a[k]并且i < j <克。
例子:
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 [1]左阵列和计数可能A [1]”的数s 使得 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live