Skip to content
Related Articles

Related Articles

Count quadruples of given type from given array
  • Difficulty Level : Medium
  • Last Updated : 22 Apr, 2021

C++




// C++ program of the
// above approach
 
#include <cstring>
#include <iostream>
 
using namespace std;
const int maxN = 2002;
 
// lcount[i][j]: Stores the count of
// i on left of index j
int lcount[maxN][maxN];
 
// rcount[i][j]: Stores the count of
// i on right of index j
int rcount[maxN][maxN];
 
// Function to count unique elements
// on left and right of any index
void fill_counts(int a[], int n)
{
    int i, j;
 
    // Find the maximum array element
    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++) {
 
        // Calculate prefix sum of
        // counts of each value
        for (j = 0; j < n; j++) {
            lcount[i][j] = lcount[i][j - 1]
                           + lcount[i][j];
        }
 
        // Calculate suffix sum of
        // counts of each value
        for (j = n - 2; j >= 0; j--) {
            rcount[i][j] = rcount[i][j + 1]
                           + rcount[i][j];
        }
    }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
int main()
{
    int a[7] = { 1, 2, 3, 2, 1, 3, 2 };
    cout << countSubsequence(a, 7);
 
    return 0;
}

Java




// Java program of the
// above approach
import java.util.*;
class GFG{
   
static int maxN = 2002;
 
// lcount[i][j]: Stores the
// count of i on left of index j
static int [][]lcount =
       new int[maxN][maxN];
 
// rcount[i][j]: Stores the
// count of i on right of index j
static int [][]rcount =
       new int[maxN][maxN];
 
// Function to count unique
// elements on left and right
// of any index
static void fill_counts(int a[],
                        int n)
{
  int i, j;
 
  // Find the maximum
  // array element
  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++)
  {
    // Calculate prefix sum of
    // counts of each value
    for (j = 1; j < n; j++)
    {
      lcount[i][j] = lcount[i][j - 1] +
                     lcount[i][j];
    }
 
    // Calculate suffix sum of
    // counts of each value
    for (j = n - 2; j >= 0; j--)
    {
      rcount[i][j] = rcount[i][j + 1] +
                     rcount[i][j];
    }
  }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
public static void main(String[] args)
{
  int a[] = {1, 2, 3, 2, 1, 3, 2};
  System.out.print(
  countSubsequence(a, a.length));
}
}
 
// This code is contributed by shikhasingrajput

Python3




# Python3 program of the
# above approach
 
maxN = 2002;
 
# lcount[i][j]: Stores the
# count of i on left of index j
lcount = [[0 for i in range(maxN)] for j in range(maxN)]
 
# rcount[i][j]: Stores the
# count of i on right of index j
rcount = [[0 for i in range(maxN)] for j in range(maxN)]
 
# Function to count unique
# elements on left and right
# of any index
def fill_counts(a, n):
 
    # Find the maximum
    # array element
    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):
       
        # Calculate prefix sum of
        # counts of each value
        for j in range(1, n):
            lcount[i][j] = lcount[i][j - 1] + lcount[i][j];
 
        # Calculate suffix sum of
        # counts of each value
        for j in range(n - 2, 0, -1):
            rcount[i][j] = rcount[i][j + 1] + rcount[i][j];
 
# Function to count quadruples
# of the required type
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;
 
# Driver Code
if __name__ == '__main__':
    a = [1, 2, 3, 2, 1, 3, 2];
    print(countSubsequence(a, len(a)));
 
# This code contributed by gauravrajput1

C#




// C# program of the
// above approach
using System;
 
class GFG{
 
static int maxN = 2002;
 
// lcount[i][j]: Stores the
// count of i on left of index j
static int[, ] lcount = new int[maxN, maxN];
 
// rcount[i][j]: Stores the
// count of i on right of index j
static int[, ] rcount = new int[maxN, maxN];
 
// Function to count unique
// elements on left and right
// of any index
static void fill_counts(int[] a, int n)
{
    int i, j;
 
    // Find the maximum
    // array element
    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++)
    {
         
        // Calculate prefix sum of
        // counts of each value
        for(j = 1; j < n; j++)
        {
            lcount[i, j] = lcount[i, j - 1] +
                           lcount[i, j];
        }
 
        // Calculate suffix sum of
        // counts of each value
        for(j = n - 2; j >= 0; j--)
        {
            rcount[i, j] = rcount[i, j + 1] +
                           rcount[i, j];
        }
    }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] a = { 1, 2, 3, 2, 1, 3, 2 };
     
    Console.Write(countSubsequence(a, a.Length));
}
}
 
// This code is contributed by chitranayal

Output:

5

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++




// C++ program of the
// above approach
 
#include <iostream>
using namespace std;
 
const int maxN = 2002;
 
// Function to find the count of
// the subsequence of given type
int countSubsequece(int a[], int n)
{
    int i, j, k, l;
 
    // Stores the count
    // of quadruples
    int answer = 0;
 
    // Generate all possible
    // combinations of quadruples
    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++) {
 
                    // Check if 1st element is
                    // equal to 3rd element
                    if (a[j] == a[l] &&
 
                        // Check if 2nd element is
                        // equal to 4th element
                        a[i] == a[k]) {
                        answer++;
                    }
                }
            }
        }
    }
    return answer;
}
 
// Driver Code
int main()
{
    int a[7] = { 1, 2, 3, 2, 1, 3, 2 };
    cout << countSubsequece(a, 7);
 
    return 0;
}

Java




// Java program of the
// above approach
import java.util.*;
 
class GFG{
     
// Function to find the count of
// the subsequence of given type
static int countSubsequece(int a[], int n)
{
    int i, j, k, l;
  
    // Stores the count
    // of quadruples
    int answer = 0;
  
    // Generate all possible
    // combinations of quadruples
    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++)
                {
                     
                    // Check if 1st element is
                    // equal to 3rd element
                    if (a[j] == a[l] &&
  
                        // Check if 2nd element is
                        // equal to 4th element
                        a[i] == a[k])
                    {
                        answer++;
                    }
                }
            }
        }
    }
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    int[] a = { 1, 2, 3, 2, 1, 3, 2 };
     
    System.out.print(countSubsequece(a, 7));
}
}
 
// This code is contributed by code_hunt

Python3




# Python3 program of the
# above approach
maxN = 2002
 
# Function to find the count of
# the subsequence of given type
def countSubsequece(a, n):
     
    # Stores the count
    # of quadruples
    answer = 0
 
    # Generate all possible
    # combinations of quadruples
    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):
                     
                    # Check if 1st element is
                    # equal to 3rd element
                    if (a[j] == a[l] and
                     
                        # Check if 2nd element is
                        # equal to 4th element
                        a[i] == a[k]):
                        answer += 1
                         
    return answer
 
# Driver Code
if __name__ == '__main__':
     
    a = [ 1, 2, 3, 2, 1, 3, 2 ]
     
    print(countSubsequece(a, 7))
     
# This code is contributed by bgangwar59

C#




// C# program of the
// above approach
using System;
   
class GFG{
   
// Function to find the count of
// the subsequence of given type
static int countSubsequece(int[] a, int n)
{
    int i, j, k, l;
   
    // Stores the count
    // of quadruples
    int answer = 0;
   
    // Generate all possible
    // combinations of quadruples
    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++)
                {
                     
                    // Check if 1st element is
                    // equal to 3rd element
                    if (a[j] == a[l] &&
                     
                        // Check if 2nd element is
                        // equal to 4th element
                        a[i] == a[k])
                    {
                        answer++;
                    }
                }
            }
        }
    }
    return answer;
}
   
// Driver Code
public static void Main()
{
    int[] a = { 1, 2, 3, 2, 1, 3, 2 };
     
    Console.WriteLine(countSubsequece(a, 7));
}
}
 
// This code is contributed by susmitakundugoaldanga

Javascript




<script>
    // Javascript program of the above approach
     
    // Function to find the count of
    // the subsequence of given type
    function countSubsequece(a, n)
    {
        let i, j, k, l;
 
        // Stores the count
        // of quadruples
        let answer = 0;
 
        // Generate all possible
        // combinations of quadruples
        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++)
                    {
 
                        // Check if 1st element is
                        // equal to 3rd element
                        if (a[j] == a[l] &&
 
                            // Check if 2nd element is
                            // equal to 4th element
                            a[i] == a[k])
                        {
                            answer++;
                        }
                    }
                }
            }
        }
        return answer;
    }
     
    let a = [ 1, 2, 3, 2, 1, 3, 2 ];
    document.write(countSubsequece(a, 7));
     
    // This code is contributed by mukesh07.
</script>
Output: 
5

 

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++




// C++ program of the
// above approach
 
#include <cstring>
#include <iostream>
 
using namespace std;
const int maxN = 2002;
 
// lcount[i][j]: Stores the count of
// i on left of index j
int lcount[maxN][maxN];
 
// rcount[i][j]: Stores the count of
// i on right of index j
int rcount[maxN][maxN];
 
// Function to count unique elements
// on left and right of any index
void fill_counts(int a[], int n)
{
    int i, j;
 
    // Find the maximum array element
    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++) {
 
        // Calculate prefix sum of
        // counts of each value
        for (j = 0; j < n; j++) {
            lcount[i][j] = lcount[i][j - 1]
                           + lcount[i][j];
        }
 
        // Calculate suffix sum of
        // counts of each value
        for (j = n - 2; j >= 0; j--) {
            rcount[i][j] = rcount[i][j + 1]
                           + rcount[i][j];
        }
    }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
int main()
{
    int a[7] = { 1, 2, 3, 2, 1, 3, 2 };
    cout << countSubsequence(a, 7);
 
    return 0;
}

Java




// Java program of the
// above approach
import java.util.*;
 
class GFG{
static int maxN = 2002;
 
// lcount[i][j]: Stores the
// count of i on left of index j
static int [][]lcount =
       new int[maxN][maxN];
 
// rcount[i][j]: Stores the
// count of i on right of index j
static int [][]rcount =
       new int[maxN][maxN];
 
// Function to count unique
// elements on left and right
// of any index
static void fill_counts(int a[],
                        int n)
{
  int i, j;
 
  // Find the maximum
  // array element
  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++)
  {
    // Calculate prefix sum of
    // counts of each value
    for (j = 1; j < n; j++)
    {
      lcount[i][j] = lcount[i][j - 1] +
                     lcount[i][j];
    }
 
    // Calculate suffix sum of
    // counts of each value
    for (j = n - 2; j >= 0; j--)
    {
      rcount[i][j] = rcount[i][j + 1] +
                     rcount[i][j];
    }
  }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
public static void main(String[] args)
{
  int a[] = {1, 2, 3, 2, 1, 3, 2};
  System.out.print(
  countSubsequence(a, a.length));
}
}
 
// This code is contributed by shikhasingrajput

Python3




# Python3 program of the
# above approach
maxN = 2002
 
# lcount[i][j]: Stores the count of
# i on left of index j
lcount = [[0 for i in range(maxN)]
             for j in range(maxN)]
 
# rcount[i][j]: Stores the count of
# i on right of index j
rcount = [[0 for i in range(maxN)]
             for j in range(maxN)]
 
# Function to count unique elements
# on left and right of any index
def fill_counts(a, n):
 
    # Find the maximum array element
    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):
 
        # Calculate prefix sum of
        # counts of each value
        for j in range(n) :
            lcount[i][j] = (lcount[i][j - 1] +
                            lcount[i][j])
 
        # Calculate suffix sum of
        # counts of each value
        for j in range(n - 2, -1, -1):
            rcount[i][j] = (rcount[i][j + 1] + 
                            rcount[i][j])
 
# Function to count quadruples
# of the required type
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
     
# Driver Code
a = [ 1, 2, 3, 2, 1, 3, 2 ]
 
print(countSubsequence(a, 7))
 
# This code is contributed by divyesh072019

C#




// C# program of the
// above approach
using System;
 
class GFG{
     
static int maxN = 2002;
 
// lcount[i,j]: Stores the
// count of i on left of index j
static int [,]lcount = new int[maxN, maxN];
 
// rcount[i,j]: Stores the
// count of i on right of index j
static int [,]rcount = new int[maxN, maxN];
 
// Function to count unique
// elements on left and right
// of any index
static void fill_counts(int []a,
                        int n)
{
    int i, j;
     
    // Find the maximum
    // array element
    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++)
    {
         
        // Calculate prefix sum of
        // counts of each value
        for (j = 1; j < n; j++)
        {
            lcount[i, j] = lcount[i, j - 1] +
                           lcount[i, j];
        }
         
        // Calculate suffix sum of
        // counts of each value
        for(j = n - 2; j >= 0; j--)
        {
            rcount[i, j] = rcount[i, j + 1] +
                           rcount[i, j];
        }
    }
}
 
// Function to count quadruples
// of the required type
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;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []a = { 1, 2, 3, 2, 1, 3, 2 };
     
    Console.Write(
        countSubsequence(a, a.Length));
}
}
 
// This code is contributed by Princi Singh
Output: 
5

 

Time Complexity: O(N2)
Auxiliary Space: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :