📌  相关文章
📜  计算数组中的四元组 (i, j, k, l) 使得 i < j < k < l 和 arr[i] = arr[k] 和 arr[j] = arr[l]

📅  最后修改于: 2021-10-28 01:44:14             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是计算给定数组中元(i, j, k, l)的数量,使得i < j < k < larr[i] = arr[ k]arr[j] = arr[l]

例子:

朴素方法:最简单的方法是生成所有可能的四元组并检查给定条件是否成立。如果发现为真,则增加最终计数。打印获得的最终计数。

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

高效的方法:为了优化上述方法,想法是使用Hashing。以下是步骤:

  1. 对于每个索引j迭代以找到一对索引(j, l)使得arr[j] = arr[l]j < l
  2. 使用哈希表来计算索引[0, j – 1] 中存在的所有数组元素的频率。
  3. 在遍历索引jl 时,只需将jl之间每个元素的频率添加到最终计数中。
  4. 对每对这样的可能的索引(j, l)重复此过程。
  5. 在上述步骤后打印四元组的总数。

下面是上述方法的实现:

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


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程