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
Javascript
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 < j < k < l < = N ) 来自给定的数组。
例子:
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.
朴素方法:解决问题的最简单方法是迭代检查给定数组中 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
蟒蛇3
# 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
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
蟒蛇3
# 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 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live