Count of distinct alternate triplets of indices from given Array | Set 2
Last Updated :
19 Apr, 2022
Given a binary array arr[] of size N, the task is to find the count of distinct alternating triplets.
Note: A triplet is alternating if the values of those indices are in {0, 1, 0} or {1, 0, 1} form.
Examples:
Input: arr[] = {0, 0, 1, 1, 0, 1}
Output: 6
Explanation: Here four sequence of “010” and two sequence of “101” exist.
So, the total number of ways of alternating sequence of size 3 is 6.
Input: arr[] = {0, 0, 0, 0, 0}
Output: 0
Explanation: As there are no 1s in the array so we cannot find any 3 size alternating sequence.
Naive Approach: The naive approach and the approach based on dynamic programming is mentioned in the Set 1 of this article.
Efficient Approach: This problem can be solved efficiently using prefix sum based on the following idea:
- The possible groups that can be formed are {0, 1, 0} or {1, 0, 1}
- So for any 1 encountered in the array the total possible combinations can be calculated by finding the number of ways to select one 0 from its left and one 0 from its right. This value is same as the product of number of 0s to its left and the number of 0s to its right.
- For a 0, the number of possible triplets can be found in the same way.
- The final answer is the sum of these two values.
Follow the below steps to solve the problem:
- Traverse the array starting from the left and count the number of 0s in (say count1) and the total number of 1s (say count2).
- Then, initialize the left_count of both the numbers as 0.
- Traverse the array from i = 0 to N:
- Now, lets suppose 1 is encountered, so first calculate the combinations of {0, 1, 0} possible using this 1. For this, multiply left_count_Zero and count1 and add the result to our final answer.
- Add this value with the sum.
- Now, decrement the count2 as for the next element it appears in left and thus, increment the left_count_One.
- Similarly, do the same when 0 is encountered.
- Return the final sum.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long numberOfWays( int A[], int N)
{
int left_count_Zero = 0, count1 = 0;
int left_count_One = 0, count2 = 0;
long long ans = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 1)
count2++;
else
count1++;
}
for ( int i = 0; i < N; i++) {
if (A[i] == 1) {
ans += (left_count_Zero * count1);
left_count_One++;
count2--;
}
else {
ans += (left_count_One * count2);
left_count_Zero++;
count1--;
}
}
return ans;
}
int main()
{
int arr[] = { 0, 0, 1, 1, 0, 1 };
int N = 6;
cout << numberOfWays(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static long numberOfWays( int A[], int N)
{
int left_count_Zero = 0 , count1 = 0 ;
int left_count_One = 0 , count2 = 0 ;
long ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] == 1 )
count2++;
else
count1++;
}
for ( int i = 0 ; i < N; i++) {
if (A[i] == 1 ) {
ans += (left_count_Zero * count1);
left_count_One++;
count2--;
}
else {
ans += (left_count_One * count2);
left_count_Zero++;
count1--;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 0 , 0 , 1 , 1 , 0 , 1 };
int N = 6 ;
System.out.print(numberOfWays(arr, N));
}
}
|
Python3
def numberOfWays(A, N):
left_count_Zero,count1,left_count_One,count2 = 0 , 0 , 0 , 0
ans = 0
for i in range (N):
if (A[i] = = 1 ):
count2 + = 1
else :
count1 + = 1
for i in range (N):
if (A[i] = = 1 ):
ans + = (left_count_Zero * count1)
left_count_One + = 1
count2 - = 1
else :
ans + = (left_count_One * count2)
left_count_Zero + = 1
count1 - = 1
return ans
arr = [ 0 , 0 , 1 , 1 , 0 , 1 ]
N = 6
print (numberOfWays(arr, N))
|
C#
using System;
class GFG {
static long numberOfWays( int [] A, int N)
{
int left_count_Zero = 0, count1 = 0;
int left_count_One = 0, count2 = 0;
long ans = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 1)
count2++;
else
count1++;
}
for ( int i = 0; i < N; i++) {
if (A[i] == 1) {
ans += (left_count_Zero * count1);
left_count_One++;
count2--;
}
else {
ans += (left_count_One * count2);
left_count_Zero++;
count1--;
}
}
return ans;
}
public static int Main()
{
int [] arr = new int [] { 0, 0, 1, 1, 0, 1 };
int N = 6;
Console.Write(numberOfWays(arr, N));
return 0;
}
}
|
Javascript
<script>
const numberOfWays = (A, N) => {
let left_count_Zero = 0, count1 = 0;
let left_count_One = 0, count2 = 0;
let ans = 0;
for (let i = 0; i < N; i++) {
if (A[i] == 1)
count2++;
else
count1++;
}
for (let i = 0; i < N; i++) {
if (A[i] == 1) {
ans += (left_count_Zero * count1);
left_count_One++;
count2--;
}
else {
ans += (left_count_One * count2);
left_count_Zero++;
count1--;
}
}
return ans;
}
let arr = [0, 0, 1, 1, 0, 1];
let N = 6;
document.write(numberOfWays(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...