给定一个包含N个整数的数组arr [] ,任务是计算给定数组中的元组(i,j,k,l)的数量,以使i
例子:
Input: arr[] = {1, 2, 1, 2, 2, 2}
Output: 4
Explanation:
The tuples which satisfy the given condition are:
1) (0, 1, 2, 3) since arr[0] = arr[2] = 1 and arr[1] = arr[3] = 2
2) (0, 1, 2, 4) since arr[0] = arr[2] = 1 and arr[1] = arr[4] = 2
3) (0, 1, 2, 5) since arr[0] = arr[2] = 1 and arr[1] = arr[5] = 2
4) (1, 3, 4, 5) since arr[1] = arr[4] = 2 and arr[3] = arr[5] = 2
Input: arr[] = {2, 5, 2, 2, 5, 4}
Output: 2
天真的方法:最简单的方法是生成所有可能的四元组,并检查给定条件是否成立。如果发现是真实的,则增加最终计数。打印获得的最终计数。
时间复杂度: O(N 4 )
辅助空间: O(1)
高效方法:为了优化上述方法,我们的想法是使用哈希。步骤如下:
- 对于每个索引j迭代找到一对索引(j,l) ,使得arr [j] = arr [l]且j
。 - 使用哈希表来计算索引[0,j – 1]中存在的所有数组元素的频率。
- 在遍历索引j到l时,只需将j和l之间的每个元素的频率添加到最终计数中即可。
- 对每个这样的可能的索引对(j,l)重复此过程。
- 完成上述步骤后,打印四倍的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count total number
// of required tuples
int countTuples(int arr[], int N)
{
int ans = 0, val = 0;
// Initialize unordered map
unordered_map freq;
for (int j = 0; j < N - 2; j++) {
val = 0;
// Find the pairs (j, l) such
// that arr[j] = arr[l] and j < l
for (int l = j + 1; l < N; l++) {
// elements are equal
if (arr[j] == arr[l]) {
// Update the count
ans += val;
}
// Add the frequency of
// arr[l] to val
val += freq[arr[l]];
}
// Update the frequency of
// element arr[j]
freq[arr[j]]++;
}
// Return the answer
return ans;
}
// Driver code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 1, 2, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << countTuples(arr, N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to count total number
// of required tuples
static int countTuples(int arr[],
int N)
{
int ans = 0, val = 0;
// Initialize unordered map
HashMap freq = new HashMap();
for (int j = 0; j < N - 2; j++)
{
val = 0;
// Find the pairs (j, l) such
// that arr[j] = arr[l] and j < l
for (int l = j + 1; l < N; l++)
{
// elements are equal
if (arr[j] == arr[l])
{
// Update the count
ans += val;
}
// Add the frequency of
// arr[l] to val
if(freq.containsKey(arr[l]))
val += freq.get(arr[l]);
}
// Update the frequency of
// element arr[j]
if(freq.containsKey(arr[j]))
{
freq.put(arr[j], freq.get(arr[j]) + 1);
}
else
{
freq.put(arr[j], 1);
}
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {1, 2, 1, 2, 2, 2};
int N = arr.length;
// Function Call
System.out.print(countTuples(arr, N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to count total number
# of required tuples
def countTuples(arr, N):
ans = 0
val = 0
# Initialize unordered map
freq = {}
for j in range(N - 2):
val = 0
# Find the pairs (j, l) such
# that arr[j] = arr[l] and j < l
for l in range(j + 1, N):
# Elements are equal
if (arr[j] == arr[l]):
# Update the count
ans += val
# Add the frequency of
# arr[l] to val
if arr[l] in freq:
val += freq[arr[l]]
# Update the frequency of
# element arr[j]
freq[arr[j]] = freq.get(arr[j], 0) + 1
# Return the answer
return ans
# Driver code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 2, 1, 2, 2, 2 ]
N = len(arr)
# Function call
print(countTuples(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count total number
// of required tuples
static int countTuples(int []arr,
int N)
{
int ans = 0, val = 0;
// Initialize unordered map
Dictionary freq = new Dictionary();
for(int j = 0; j < N - 2; j++)
{
val = 0;
// Find the pairs (j, l) such
// that arr[j] = arr[l] and j < l
for(int l = j + 1; l < N; l++)
{
// Elements are equal
if (arr[j] == arr[l])
{
// Update the count
ans += val;
}
// Add the frequency of
// arr[l] to val
if (freq.ContainsKey(arr[l]))
val += freq[arr[l]];
}
// Update the frequency of
// element arr[j]
if (freq.ContainsKey(arr[j]))
{
freq[arr[j]] = freq[arr[j]] + 1;
}
else
{
freq.Add(arr[j], 1);
}
}
// Return the answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 1, 2, 1, 2, 2, 2 };
int N = arr.Length;
// Function call
Console.Write(countTuples(arr, N));
}
}
// This code is contributed by Amit Katiyar
4
时间复杂度: O(N 2 )
辅助空间: O(N)