Count subsequence of length 4 having product of the first three elements equal to the fourth element
Last Updated :
24 Aug, 2021
Given an array arr[] consisting of N positive integers, the task is to find the number of subsequences of length 4 having product of the first three elements equal to the fourth element.
Examples:
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
Naive Approach: The simplest approach to solve the given problem is to generate all possible subsequences and count those subsequences satisfying the given criteria. After checking for all subsequences, print the total count obtained.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by finding all subsequences of length 3 and store the product of the three in the HashMap and then count the subsequence by fixing each element as the fourth element and increment the frequency of the product of triplets. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countQuadruples( int A[], int N)
{
int ans = 0;
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++) {
ans += freq[A[i]];
for ( int j = 0; j < i; j++) {
for ( int k = 0; k < j; k++) {
freq[A[i] * A[j] * A[k]]++;
}
}
}
return ans;
}
int main()
{
int arr[] = { 10, 2, 2, 7, 40, 160 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countQuadruples(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countQuadruples( int A[], int N)
{
int ans = 0 ;
HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
if (freq.containsKey(A[i]))
ans += freq.get(A[i]);
for ( int j = 0 ; j < i; j++) {
for ( int k = 0 ; k < j; k++) {
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 ans;
}
public static void main(String[] args)
{
int arr[] = { 10 , 2 , 2 , 7 , 40 , 160 };
int N = arr.length;
System.out.print(countQuadruples(arr, N));
}
}
|
Python3
def countQuadruples(A, N):
ans = 0
freq = {}
for i in range (N):
if A[i] in freq:
ans + = freq[A[i]]
else :
freq[A[i]] = 0
for j in range (i):
for k in range (j):
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 ans
if __name__ = = '__main__' :
arr = [ 10 , 2 , 2 , 7 , 40 , 160 ]
N = len (arr)
print (countQuadruples(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countQuadruples( int []A, int N)
{
int ans = 0;
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (freq.ContainsKey(A[i]))
ans += freq[A[i]];
else
freq.Add(A[i],0);
for ( int j = 0; j < i; j++) {
for ( int k = 0; k < j; k++) {
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 ans;
}
public static void Main()
{
int []arr = { 10, 2, 2, 7, 40, 160 };
int N = arr.Length;
Console.Write(countQuadruples(arr, N));
}
}
|
Javascript
<script>
function countQuadruples(A, N)
{
let ans = 0;
let freq = new Map();
for (let i = 0; i < N; i++) {
if (freq.has(arr[i])) {
ans += freq.get(A[i]);
}
for (let j = 0; j < i; j++) {
for (let k = 0; k < j; k++) {
if (freq.has(A[i] * A[j] * A[k])) {
freq.set(freq.get(A[i] * A[j] * A[k]), freq.get([A[i] * A[j] * A[k]]) + 1);
}
else {
freq.set(A[i] * A[j] * A[k], 1);
}
}
}
}
return ans;
}
let arr = [10, 2, 2, 7, 40, 160];
let N = arr.length;
document.write(countQuadruples(arr, N));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N3)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...