Count quadruples of given type from given array
Given an array arr[], the task is to find the number of quadruples of the form a[i] = a[k] and a[j] = a[l] ( 0 <= i < j < k < l <= N ) from the given array.
Examples:
Input: arr[] = {1, 2, 4, 2, 1, 5, 2}
Output: 2
Explanation: The quadruple {1, 2, 1, 2} occurs twice in the array, at indices {0, 1, 4, 6} and {0, 3, 4, 6}. Therefore, the required count is 2.
Input: arr[] = {1, 2, 3, 2, 1, 3, 2}
Output: 5
Explanation: The quadruple {1, 2, 1, 2} occurs twice in the array at indices {0, 1, 4, 6} and {0, 3, 4, 6}. The quadruples {1, 3, 1, 3}, {3, 2, 3, 2} and {2, 3, 2, 3} occurs once each. Therefore, the required count is 5.
Naive Approach: The simplest approach to solve the problem is to iteratively check all combinations of 4 elements from the given array and check if it satisfies the given condition or not.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int maxN = 2002;
int countSubsequece( int a[], int n)
{
int i, j, k, l;
int answer = 0;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
for (k = j + 1; k < n; k++) {
for (l = k + 1; l < n; l++) {
if (a[j] == a[l] &&
a[i] == a[k]) {
answer++;
}
}
}
}
}
return answer;
}
int main()
{
int a[7] = { 1, 2, 3, 2, 1, 3, 2 };
cout << countSubsequece(a, 7);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countSubsequece( int a[], int n)
{
int i, j, k, l;
int answer = 0 ;
for (i = 0 ; i < n; i++)
{
for (j = i + 1 ; j < n; j++)
{
for (k = j + 1 ; k < n; k++)
{
for (l = k + 1 ; l < n; l++)
{
if (a[j] == a[l] &&
a[i] == a[k])
{
answer++;
}
}
}
}
}
return answer;
}
public static void main(String[] args)
{
int [] a = { 1 , 2 , 3 , 2 , 1 , 3 , 2 };
System.out.print(countSubsequece(a, 7 ));
}
}
|
Python3
maxN = 2002
def countSubsequece(a, n):
answer = 0
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
for l in range (k + 1 , n):
if (a[j] = = a[l] and
a[i] = = a[k]):
answer + = 1
return answer
if __name__ = = '__main__' :
a = [ 1 , 2 , 3 , 2 , 1 , 3 , 2 ]
print (countSubsequece(a, 7 ))
|
C#
using System;
class GFG{
static int countSubsequece( int [] a, int n)
{
int i, j, k, l;
int answer = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
for (k = j + 1; k < n; k++)
{
for (l = k + 1; l < n; l++)
{
if (a[j] == a[l] &&
a[i] == a[k])
{
answer++;
}
}
}
}
}
return answer;
}
public static void Main()
{
int [] a = { 1, 2, 3, 2, 1, 3, 2 };
Console.WriteLine(countSubsequece(a, 7));
}
}
|
Javascript
<script>
function countSubsequece(a, n)
{
let i, j, k, l;
let answer = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
for (k = j + 1; k < n; k++)
{
for (l = k + 1; l < n; l++)
{
if (a[j] == a[l] &&
a[i] == a[k])
{
answer++;
}
}
}
}
}
return answer;
}
let a = [ 1, 2, 3, 2, 1, 3, 2 ];
document.write(countSubsequece(a, 7));
</script>
|
Time Complexity: O(N4)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to maintain two arrays to store the count of element X on the left and right side of every index. Follow the steps below to solve the problem:
- Maintain two arrays lcount[i][j] and rcount[i][j] which stores the count of the element i in the indices less than j and rcount[i][j] stores the count of the element i in the indices greater than j.
- Iterate over the nested loop from 1 to N and find all the subsequence of type XYXY
answer += lcount[a[i]][j-1] * rcount[a[j]][i-1]
Below is the implementation of the above approach:
C++
#include <cstring>
#include <iostream>
using namespace std;
const int maxN = 2002;
int lcount[maxN][maxN];
int rcount[maxN][maxN];
void fill_counts( int a[], int n)
{
int i, j;
int maxA = a[0];
for (i = 0; i < n; i++) {
if (a[i] > maxA) {
maxA = a[i];
}
}
memset (lcount, 0, sizeof (lcount));
memset (rcount, 0, sizeof (rcount));
for (i = 0; i < n; i++) {
lcount[a[i]][i] = 1;
rcount[a[i]][i] = 1;
}
for (i = 0; i <= maxA; i++) {
for (j = 0; j < n; j++) {
lcount[i][j] = lcount[i][j - 1]
+ lcount[i][j];
}
for (j = n - 2; j >= 0; j--) {
rcount[i][j] = rcount[i][j + 1]
+ rcount[i][j];
}
}
}
int countSubsequence( int a[], int n)
{
int i, j;
fill_counts(a, n);
int answer = 0;
for (i = 1; i < n; i++) {
for (j = i + 1; j < n - 1; j++) {
answer += lcount[a[j]][i - 1]
* rcount[a[i]][j + 1];
}
}
return answer;
}
int main()
{
int a[7] = { 1, 2, 3, 2, 1, 3, 2 };
cout << countSubsequence(a, 7);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxN = 2002 ;
static int [][]lcount =
new int [maxN][maxN];
static int [][]rcount =
new int [maxN][maxN];
static void fill_counts( int a[],
int n)
{
int i, j;
int maxA = a[ 0 ];
for (i = 0 ; i < n; i++)
{
if (a[i] > maxA)
{
maxA = a[i];
}
}
for (i = 0 ; i < n; i++)
{
lcount[a[i]][i] = 1 ;
rcount[a[i]][i] = 1 ;
}
for (i = 0 ; i <= maxA; i++)
{
for (j = 1 ; j < n; j++)
{
lcount[i][j] = lcount[i][j - 1 ] +
lcount[i][j];
}
for (j = n - 2 ; j >= 0 ; j--)
{
rcount[i][j] = rcount[i][j + 1 ] +
rcount[i][j];
}
}
}
static int countSubsequence( int a[],
int n)
{
int i, j;
fill_counts(a, n);
int answer = 0 ;
for (i = 1 ; i < n; i++)
{
for (j = i + 1 ; j < n - 1 ; j++)
{
answer += lcount[a[j]][i - 1 ] *
rcount[a[i]][j + 1 ];
}
}
return answer;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 2 , 1 , 3 , 2 };
System.out.print(
countSubsequence(a, a.length));
}
}
|
Python3
maxN = 2002
lcount = [[ 0 for i in range (maxN)]
for j in range (maxN)]
rcount = [[ 0 for i in range (maxN)]
for j in range (maxN)]
def fill_counts(a, n):
maxA = a[ 0 ]
for i in range (n):
if (a[i] > maxA):
maxA = a[i]
for i in range (n):
lcount[a[i]][i] = 1
rcount[a[i]][i] = 1
for i in range (maxA + 1 ):
for j in range (n) :
lcount[i][j] = (lcount[i][j - 1 ] +
lcount[i][j])
for j in range (n - 2 , - 1 , - 1 ):
rcount[i][j] = (rcount[i][j + 1 ] +
rcount[i][j])
def countSubsequence(a, n):
fill_counts(a, n)
answer = 0
for i in range ( 1 , n):
for j in range (i + 1 , n - 1 ):
answer + = (lcount[a[j]][i - 1 ] *
rcount[a[i]][j + 1 ])
return answer
a = [ 1 , 2 , 3 , 2 , 1 , 3 , 2 ]
print (countSubsequence(a, 7 ))
|
C#
using System;
class GFG{
static int maxN = 2002;
static int [,]lcount = new int [maxN, maxN];
static int [,]rcount = new int [maxN, maxN];
static void fill_counts( int []a,
int n)
{
int i, j;
int maxA = a[0];
for (i = 0; i < n; i++)
{
if (a[i] > maxA)
{
maxA = a[i];
}
}
for (i = 0; i < n; i++)
{
lcount[a[i], i] = 1;
rcount[a[i], i] = 1;
}
for (i = 0; i <= maxA; i++)
{
for (j = 1; j < n; j++)
{
lcount[i, j] = lcount[i, j - 1] +
lcount[i, j];
}
for (j = n - 2; j >= 0; j--)
{
rcount[i, j] = rcount[i, j + 1] +
rcount[i, j];
}
}
}
static int countSubsequence( int []a,
int n)
{
int i, j;
fill_counts(a, n);
int answer = 0;
for (i = 1; i < n; i++)
{
for (j = i + 1; j < n - 1; j++)
{
answer += lcount[a[j], i - 1] *
rcount[a[i], j + 1];
}
}
return answer;
}
public static void Main(String[] args)
{
int []a = { 1, 2, 3, 2, 1, 3, 2 };
Console.Write(
countSubsequence(a, a.Length));
}
}
|
Javascript
<script>
let maxN = 2002;
let lcount = new Array(maxN);
let rcount = new Array(maxN);
for (let i = 0; i < maxN; i++)
{
lcount[i] = new Array(maxN);
rcount[i] = new Array(maxN);
for (let j = 0; j < maxN; j++)
{
lcount[i][j] = 0;
rcount[i][j] = 0;
}
}
function fill_counts(a,n)
{
let i, j;
let maxA = a[0];
for (i = 0; i < n; i++)
{
if (a[i] > maxA)
{
maxA = a[i];
}
}
for (i = 0; i < n; i++)
{
lcount[a[i]][i] = 1;
rcount[a[i]][i] = 1;
}
for (i = 0; i <= maxA; i++)
{
for (j = 1; j < n; j++)
{
lcount[i][j] = lcount[i][j - 1] +
lcount[i][j];
}
for (j = n - 2; j >= 0; j--)
{
rcount[i][j] = rcount[i][j + 1] +
rcount[i][j];
}
}
}
function countSubsequence(a,n)
{
let i, j;
fill_counts(a, n);
let answer = 0;
for (i = 1; i < n; i++)
{
for (j = i + 1; j < n - 1; j++)
{
answer += lcount[a[j]][i - 1] *
rcount[a[i]][j + 1];
}
}
return answer;
}
let a=[1, 2, 3, 2, 1, 3, 2];
document.write(countSubsequence(a, a.length));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Last Updated :
20 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...