# Count quadruples (i, j, k, l) in an array such that i < j < k < l and arr[i] = arr[k] and arr[j] = arr[l]

Given an array arr[] consisting of N integers, the task is to count the number of tuples (i, j, k, l) from the given array such that i < j < k < l and arr[i] = arr[k] and arr[j] = arr[l].

Examples:

Input: arr[] = {1, 2, 1, 2, 2, 2}
Output:
Explanation:
The tuples which satisfy the given condition are:
1) (0, 1, 2, 3) since arr = arr = 1 and arr = arr = 2
2) (0, 1, 2, 4) since arr = arr = 1 and arr = arr = 2
3) (0, 1, 2, 5) since arr = arr = 1 and arr = arr = 2
4) (1, 3, 4, 5) since arr = arr = 2 and arr = arr = 2

Input: arr[] = {2, 5, 2, 2, 5, 4}
Output: 2

Naive Approach: The simplest approach is to generate all the possible quadruples and check if the given condition holds true. If found to be true, then increase the final count. Print the final count obtained.

Time Complexity: O(N4
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use Hashing. Below are the steps:

1. For each index j iterate to find a pair of indices (j, l) such that arr[j] = arr[l] and j < l.
2. Use a hash table to keep count of the frequency of all array elements present in the indices [0, j – 1].
3. While traversing through index j to l, simply add the frequency of each element between j and l to the final count.
4. Repeat this process for every such possible pair of indices (j, l).
5. Print the total count of quadruples after the above steps.

Below is the implementation of the above approach:

## 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<``int``, ``int``> 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);`   `    ``// 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<``int``,` `               ``int``> freq = ``new` `Dictionary<``int``,` `                                          ``int``>();` `    `  `    ``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`

Output:

```4

```

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

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 Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.