Given an array arr[] of size N consisting of only 0s, 1s and 2s, the task is to find the count of triplets of indices (i, j, k) containing distinct array elements such that i < j < k and the array elements are not equidistant, i.e, (j – i )!= (k – j).
Examples:
Input: arr[] = { 0, 1, 2, 1 }
Output:1
Explanation:
Only triplet (0, 2, 3) contains distinct array elements and (2 – 0) != (3 – 2).
Therefore, the required output is 1.
Input: arr[] = { 0, 1, 2 }
Output: 0
Explanation:
No triplet exists that satisfy the condition.
Therefore, the required output is 0.
Approach: The idea is to store the indices of array elements 0s, 1s and 2s in three separate arrays, then find the count triplets that satisfy the given conditions. Follow the steps below to solve the problem:
- Initialize two arrays, say zero_i[] and one_i[], to store the indices of 0s and 1s from the given array respectively.
- Initialize a map, say mp, to store the indices of 2s from the given array.
- Find the total count of all possible triplets by multiplying the size of zero_i[], one_i[] and mp.
- Now, subtract all those triplets which violate the given conditions.
- For finding such triplets, traverse both the arrays zero_i[] and one_i[] and try to find a third index in the Map that violates the condition.
- To find the third index which violates the conditions, following three cases arise:
- The third index is equidistant from both the indices and is present in between them.
- The third index is equidistant from both the indices and is present on the left side of the first index.
- The third index is equidistant from both the indices and is present on the right side of the second index.
- Remove all such triplets from the count of the total number of triplets.
- Finally, print the total count of triplets obtained.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int * arr, int N)
{
vector< int > zero_i;
vector< int > one_i;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0)
zero_i.push_back(i + 1);
else if (arr[i] == 1)
one_i.push_back(i + 1);
else
mp[i + 1] = 1;
}
int total = zero_i.size()
* one_i.size() * mp.size();
for ( int i = 0; i < zero_i.size();
i++) {
for ( int j = 0; j < one_i.size();
j++) {
int p = zero_i[i];
int q = one_i[j];
int r = 2 * p - q;
if (mp[r] > 0)
total--;
r = 2 * q - p;
if (mp[r] > 0)
total--;
r = (p + q) / 2;
if (mp[r] > 0 && abs (r - p) == abs (r - q))
total--;
}
}
cout << total;
}
int main()
{
int arr[] = { 0, 1, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
countTriplets(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countTriplets( int []arr, int N)
{
Vector<Integer> zero_i = new Vector<Integer>();
Vector<Integer> one_i = new Vector<Integer>();
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
zero_i.add(i + 1 );
else if (arr[i] == 1 )
one_i.add(i + 1 );
else
mp.put(i + 1 , 1 );
}
int total = zero_i.size() *
one_i.size() * mp.size();
for ( int i = 0 ; i < zero_i.size(); i++)
{
for ( int j = 0 ; j < one_i.size(); j++)
{
int p = zero_i.get(i);
int q = one_i.get(j);
int r = 2 * p - q;
if (mp.containsKey(r) && mp.get(r) > 0 )
total--;
r = 2 * q - p;
if (mp.containsKey(r) && mp.get(r) > 0 )
total--;
r = (p + q) / 2 ;
if (mp.containsKey(r) &&
mp.get(r) > 0 &&
Math.abs(r - p) == Math.abs(r - q))
total--;
}
}
System.out.print(total);
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 2 , 1 };
int N = arr.length;
countTriplets(arr, N);
}
}
|
Python3
def countTriplets(arr, N):
zero_i = []
one_i = []
mp = {}
for i in range (N):
if (arr[i] = = 0 ):
zero_i.append(i + 1 )
elif (arr[i] = = 1 ):
one_i.append(i + 1 )
else :
mp[i + 1 ] = 1
total = len (zero_i) * len (one_i) * len (mp)
for i in range ( len (zero_i)):
for j in range ( len (one_i)):
p = zero_i[i]
q = one_i[j]
r = 2 * p - q
if (r in mp):
total - = 1
r = 2 * q - p
if (r in mp):
total - = 1
r = (p + q) / / 2
if ((r in mp) and abs (r - p) = = abs (r - q)):
total - = 1
print (total)
if __name__ = = '__main__' :
arr = [ 0 , 1 , 2 , 1 ]
N = len (arr)
countTriplets(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countTriplets( int []arr, int N)
{
List< int > zero_i = new List< int >();
List< int > one_i = new List< int >();
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
zero_i.Add(i + 1);
else if (arr[i] == 1)
one_i.Add(i + 1);
else
mp.Add(i + 1, 1);
}
int total = zero_i.Count *
one_i.Count * mp.Count;
for ( int i = 0; i < zero_i.Count; i++)
{
for ( int j = 0; j < one_i.Count; j++)
{
int p = zero_i[i];
int q = one_i[j];
int r = 2 * p - q;
if (mp.ContainsKey(r) && mp[r] > 0)
total--;
r = 2 * q - p;
if (mp.ContainsKey(r) && mp[r] > 0)
total--;
r = (p + q) / 2;
if (mp.ContainsKey(r) &&
mp[r] > 0 &&
Math.Abs(r - p) == Math.Abs(r - q))
total--;
}
}
Console.Write(total);
}
public static void Main(String[] args)
{
int []arr = { 0, 1, 2, 1 };
int N = arr.Length;
countTriplets(arr, N);
}
}
|
Javascript
<script>
function countTriplets(arr, N)
{
var zero_i = [];
var one_i = [];
var mp = new Map();
for ( var i = 0; i < N; i++) {
if (arr[i] == 0)
zero_i.push(i + 1);
else if (arr[i] == 1)
one_i.push(i + 1);
else
mp.set(i + 1, 1);
}
var total = zero_i.length
* one_i.length * mp.size;
for ( var i = 0; i < zero_i.length;
i++) {
for ( var j = 0; j < one_i.length;
j++) {
var p = zero_i[i];
var q = one_i[j];
var r = 2 * p - q;
if (mp.has(r))
total--;
r = 2 * q - p;
if (mp.has(r))
total--;
r = (p + q) / 2;
if (mp.has(r) && Math.abs(r - p) == Math.abs(r - q))
total--;
}
}
document.write( total);
}
var arr = [0, 1, 2, 1];
var N = arr.length;
countTriplets(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jun, 2021
Like Article
Save Article