📜  从给定数组计算给定类型的四倍

📅  最后修改于: 2021-04-24 04:36:27             🧑  作者: Mango

C++
// C++ program of the
// above approach
 
#include 
#include 
 
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


C++
// C++ program of the
// above approach
 
#include 
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


C++
// C++ program of the
// above approach
 
#include 
#include 
 
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


输出:

5

给定数组arr [] ,任务是查找形式为a [i] = a [k]a [j] = a [l]的四倍数(0 <= i )。

例子:

天真的方法:解决问题的最简单方法是迭代检查给定数组中4个元素的所有组合,并检查其是否满足给定条件。

下面是上述方法的实现:

C++

// C++ program of the
// above approach
 
#include 
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
输出:
5

时间复杂度: O(N 4 )
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是维护两个数组,以在每个索引的左侧和右侧存储元素X的计数。请按照以下步骤解决问题:

  • 维护两个数组lcount [i] [j]rcount [i] [j] ,它们在小于j的索引中存储元素i的计数,而rcount [i] [j]在索引中存储元素i的计数大于j
  • 循环从1到N的嵌套循环,找到XYXY类型的所有子序列
answer += lcount[a[i]][j-1] * rcount[a[j]][i-1]

下面是上述方法的实现:

C++

// C++ program of the
// above approach
 
#include 
#include 
 
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
输出:
5

时间复杂度: O(N 2 )
辅助空间: O(N 2 )