Count triplets having product 0 from a given array
Last Updated :
03 Apr, 2023
Given an array arr[] of size N, the task is to count the number of triplets (arr[i], arr[j], arr[k]) such that arr[i] * arr[j] = arr[j] * arr[k] = 0 (i < j < k).
Examples:
Input: arr[] = {0, 8, 12, 0}
Output: 2
Explanation:
Triplets satisfying the given conditions are (0, 8, 0) and (0, 12, 0).
Therefore, the required output is 2.
Input: arr[] = {1, 0, 2, 3}
Output: 2
Naive Approach: The simplest approach to solve this problem is to generate all possible triplets from the given array and print the count of triplets that satisfy the given conditions.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to store the count of 0s on the left side and right side of each array element using the Prefix Sum technique. Traverse the array and count the number of triplets that satisfy the given condition by considering the current element of the array as the value of arr[j]. Finally, print the count of triplets that satisfy the given condition. Follow the steps below to solve the problem:
- Initialize an array, say prefixZero[], to store for every index, the count of 0s present in the preceding indices.
- Initialize a variable tripletCnt to store the count of triplets that satisfy the given conditions.
- Traverse the array and check if arr[i] equals to 0 or not. If found to be true, then increment the value of cntTriplet by i * (N – i -1).
- Otherwise, increment the value of tripletCnt by prefixZero[i] * (prefixZero[N – 1] – prefixZero[i]).
- Finally, print the value of tripletCnt.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntTriplet( int arr[], int N)
{
int preZero[N] = { 0 };
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
preZero[i]
= preZero[max(i - 1, 0)] + 1;
}
else {
preZero[i]
= preZero[max(i - 1, 0)];
}
}
int tripletCount = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
int X = i;
int Y = N - i - 1;
tripletCount += X * Y;
}
else {
int X = preZero[i];
int Y = preZero[N - 1]
- preZero[i];
tripletCount += X * Y;
}
}
return tripletCount;
}
int main()
{
int arr[] = { 1, 0, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntTriplet(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int cntTriplet( int arr[],
int N)
{
int []preZero = new int [N];
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
{
preZero[i] = preZero[(Math.max(i - 1 ,
0 ))] + 1 ;
}
else
{
preZero[i] = preZero[(Math.max(i - 1 , 0 ))];
}
}
int tripletCount = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
{
int X = i;
int Y = N - i - 1 ;
tripletCount += X * Y;
}
else
{
int X = preZero[i];
int Y = preZero[N - 1 ] -
preZero[i];
tripletCount += X * Y;
}
}
return tripletCount;
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 2 , 3 };
int N = arr.length;
System.out.print(cntTriplet(arr, N));
}
}
|
Python3
def cntTriplet(arr, N):
preZero = [ 0 ] * N
for i in range (N):
if (arr[i] = = 0 ):
preZero[i] = preZero[
max (i - 1 , 0 )] + 1
else :
preZero[i] = preZero[
max (i - 1 , 0 )]
tripletCount = 0
for i in range (N):
if (arr[i] = = 0 ):
X = i
Y = N - i - 1
tripletCount + = X * Y
else :
X = preZero[i]
Y = preZero[N - 1 ] - preZero[i]
tripletCount + = X * Y
return tripletCount
if __name__ = = '__main__' :
arr = [ 1 , 0 , 2 , 3 ]
N = len (arr)
print (cntTriplet(arr, N))
|
C#
using System;
class GFG{
static int cntTriplet( int [] arr,
int N)
{
int [] preZero = new int [N];
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
preZero[i] = preZero[(Math.Max(i - 1,
0))] + 1;
}
else
{
preZero[i] = preZero[(Math.Max(i - 1, 0))];
}
}
int tripletCount = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
int X = i;
int Y = N - i - 1;
tripletCount += X * Y;
}
else
{
int X = preZero[i];
int Y = preZero[N - 1] -
preZero[i];
tripletCount += X * Y;
}
}
return tripletCount;
}
public static void Main(String[] args)
{
int [] arr = {1, 0, 2, 3};
int N = arr.Length;
Console.Write(cntTriplet(arr, N));
}
}
|
Javascript
<script>
function cntTriplet(arr, N)
{
var preZero = Array(N).fill(0);
for ( var i = 0; i < N; i++) {
if (arr[i] == 0) {
preZero[i]
= preZero[(Math.max(i - 1, 0))] + 1;
}
else {
preZero[i]
= preZero[(Math.max(i - 1, 0))];
}
}
var tripletCount = 0;
for ( var i = 0; i < N; i++) {
if (arr[i] == 0) {
var X = i;
var Y = N - i - 1;
tripletCount += X * Y;
}
else {
var X = preZero[i];
var Y = preZero[N - 1]
- preZero[i];
tripletCount += X * Y;
}
}
return tripletCount;
}
var arr = [ 1, 0, 2, 3 ];
var N = arr.length;
document.write( cntTriplet(arr, N));
</script>
|
Time Complexity: O(N) // only one traversal of area is done
Auxiliary Space: O(N) // an extra array of N space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...