计数长度为 4 且前三个元素的乘积等于第四个元素的子序列
给定一个由N个正整数组成的数组arr[] ,任务是找到前三个元素的乘积等于第四个元素的长度为 4 的子序列的数量。
例子:
Input: arr[] = {10, 2, 2, 7, 40, 160}
Output: 2
Explanation:
Following are the subsequences of length 4 satisfying the given criteria:
- {10, 2, 2, 40}, the product of the first three elements is 10*2*2 = 40(= fourth element).
- {2, 2, 40, 160}, the product of the first three elements is 2*2*40 = 160(= fourth element).
Therefore, the total count of subsequence is 2.
Input: arr[] = {1, 1, 1, 1}
Output: 1
朴素方法:解决给定问题的最简单方法是生成所有可能的子序列并计算满足给定标准的子序列。检查所有子序列后,打印获得的总计数。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:上述方法也可以通过查找所有长度为3的子序列并将三者的乘积存储在HashMap中,然后通过将每个元素固定为第四个元素来计算子序列并增加三元组乘积的频率来进行优化。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如ans为0 ,它存储结果子序列的计数。
- 初始化一个无序映射M ,它存储三元组乘积的频率。
- 使用变量i遍历给定的数组并执行以下步骤:
- 通过将 ans 的值视为子序列的第四个元素,将ans的值增加arr[i] 。
- 在[0, i – 1]范围内生成所有可能的数组对,并将两者与arr[i]的乘积的频率存储在映射M中。
- 完成上述步骤后,打印ans的值作为子序列的结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total number of
// subsequences satisfying the given
// criteria
int countQuadruples(int A[], int N)
{
// Stores the count of quadruplets
int ans = 0;
// Stores the frequency of product
// of the triplet
unordered_map freq;
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Consider arr[i] as fourth
// element of subsequences
ans += freq[A[i]];
// Generate all possible pairs
// of the array [0, i - 1]
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
// Increment the frequency
// of the triplet
freq[A[i] * A[j] * A[k]]++;
}
}
}
// Return the total count obtained
return ans;
}
// Driver Code
int main()
{
int arr[] = { 10, 2, 2, 7, 40, 160 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << countQuadruples(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the total number of
// subsequences satisfying the given
// criteria
static int countQuadruples(int A[], int N)
{
// Stores the count of quadruplets
int ans = 0;
// Stores the frequency of product
// of the triplet
HashMap freq = new HashMap();
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Consider arr[i] as fourth
// element of subsequences
if (freq.containsKey(A[i]))
ans += freq.get(A[i]);
// Generate all possible pairs
// of the array [0, i - 1]
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
// Increment the frequency
// of the triplet
if (freq.containsKey(A[i] * A[j] * A[k]))
{
freq.put(A[i] * A[j] * A[k], freq.get(A[i] * A[j] * A[k]) + 1);
}
else
{
freq.put(A[i] * A[j] * A[k], 1);
}
}
}
}
// Return the total count obtained
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 10, 2, 2, 7, 40, 160 };
int N = arr.length;
System.out.print(countQuadruples(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
# Function to find the total number of
# subsequences satisfying the given
# criteria
def countQuadruples(A, N):
# Stores the count of quadruplets
ans = 0
# Stores the frequency of product
# of the triplet
freq = {}
# Traverse the given array arr[]
for i in range(N):
# Consider arr[i] as fourth
# element of subsequences
if A[i] in freq:
ans += freq[A[i]]
else:
freq[A[i]] = 0
# Generate all possible pairs
# of the array [0, i - 1]
for j in range(i):
for k in range(j):
# Increment the frequency
# of the triplet
if A[i] * A[j] * A[k] in freq:
freq[A[i] * A[j] * A[k]] += 1
else:
freq[A[i] * A[j] * A[k]] = 1
# Return the total count obtained
return ans
# Driver Code
if __name__ == '__main__':
arr = [10, 2, 2, 7, 40, 160]
N = len(arr)
print(countQuadruples(arr, N))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the total number of
// subsequences satisfying the given
// criteria
static int countQuadruples(int []A, int N)
{
// Stores the count of quadruplets
int ans = 0;
// Stores the frequency of product
// of the triplet
Dictionary freq = new Dictionary();
// Traverse the given array arr[]
for (int i = 0; i < N; i++) {
// Consider arr[i] as fourth
// element of subsequences
if(freq.ContainsKey(A[i]))
ans += freq[A[i]];
else
freq.Add(A[i],0);
// Generate all possible pairs
// of the array [0, i - 1]
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
// Increment the frequency
// of the triplet
if(freq.ContainsKey(A[i] * A[j] * A[k]))
freq[A[i] * A[j] * A[k]]++;
else
freq.Add(A[i] * A[j] * A[k],1);
}
}
}
// Return the total count obtained
return ans;
}
// Driver Code
public static void Main()
{
int []arr = { 10, 2, 2, 7, 40, 160 };
int N = arr.Length;
Console.Write(countQuadruples(arr, N));
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
2
时间复杂度: O(N 3 )
辅助空间: O(N 3 )