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: 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
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:
- For each index j iterate to find a pair of indices (j, l) such that arr[j] = arr[l] and j < l.
- Use a hash table to keep count of the frequency of all array elements present in the indices [0, j – 1].
- While traversing through index j to l, simply add the frequency of each element between j and l to the final count.
- Repeat this process for every such possible pair of indices (j, l).
- Print the total count of quadruples after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTuples( int arr[], int N)
{
int ans = 0, val = 0;
unordered_map< int , int > freq;
for ( int j = 0; j < N - 2; j++) {
val = 0;
for ( int l = j + 1; l < N; l++) {
if (arr[j] == arr[l]) {
ans += val;
}
val += freq[arr[l]];
}
freq[arr[j]]++;
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 1, 2, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countTuples(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTuples( int arr[],
int N)
{
int ans = 0 , val = 0 ;
HashMap<Integer,
Integer> freq = new HashMap<Integer,
Integer>();
for ( int j = 0 ; j < N - 2 ; j++)
{
val = 0 ;
for ( int l = j + 1 ; l < N; l++)
{
if (arr[j] == arr[l])
{
ans += val;
}
if (freq.containsKey(arr[l]))
val += freq.get(arr[l]);
}
if (freq.containsKey(arr[j]))
{
freq.put(arr[j], freq.get(arr[j]) + 1 );
}
else
{
freq.put(arr[j], 1 );
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 2 , 2 , 2 };
int N = arr.length;
System.out.print(countTuples(arr, N));
}
}
|
Python3
def countTuples(arr, N):
ans = 0
val = 0
freq = {}
for j in range (N - 2 ):
val = 0
for l in range (j + 1 , N):
if (arr[j] = = arr[l]):
ans + = val
if arr[l] in freq:
val + = freq[arr[l]]
freq[arr[j]] = freq.get(arr[j], 0 ) + 1
return ans
if __name__ = = '__main__' :
arr = [ 1 , 2 , 1 , 2 , 2 , 2 ]
N = len (arr)
print (countTuples(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countTuples( int []arr,
int N)
{
int ans = 0, val = 0;
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
for ( int j = 0; j < N - 2; j++)
{
val = 0;
for ( int l = j + 1; l < N; l++)
{
if (arr[j] == arr[l])
{
ans += val;
}
if (freq.ContainsKey(arr[l]))
val += freq[arr[l]];
}
if (freq.ContainsKey(arr[j]))
{
freq[arr[j]] = freq[arr[j]] + 1;
}
else
{
freq.Add(arr[j], 1);
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 1, 2, 2, 2 };
int N = arr.Length;
Console.Write(countTuples(arr, N));
}
}
|
Javascript
<script>
function countTuples(arr, N)
{
var ans = 0, val = 0;
var freq = new Map();
for ( var j = 0; j < N - 2; j++)
{
val = 0;
for ( var l = j + 1; l < N; l++)
{
if (arr[j] == arr[l])
{
ans += val;
}
if (freq.has(arr[l]))
{
val += freq.get(arr[l]);
}
}
if (freq.has(arr[j]))
{
freq.set(arr[j], freq.get(arr[j]) + 1);
}
else
{
freq.set(arr[j], 1);
}
}
return ans;
}
var arr = [ 1, 2, 1, 2, 2, 2 ];
var N = arr.length;
document.write(countTuples(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
14 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...