Open In App

Count of distinct alternate triplets of indices from given Array | Set 2

Last Updated : 19 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the possible
// number of ways to select 3 numbers
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;
 
    // Storing the right counts of
    // 1s and 2s in the array
    for (int i = 0; i < N; i++) {
        if (A[i] == 1)
            count2++;
        else
            count1++;
    }
 
    // Traversing the array from left side
    for (int i = 0; i < N; i++) {
 
        // If 1 is encountered,
        // looking for all combinations of
        // 0, 1, 0 possible
        if (A[i] == 1) {
 
            // Number of ways to select one
            // 0 from left side * Number of
            // ways to select one 0 from right
            ans += (left_count_Zero * count1);
 
            // Decrement right_count of 1
            // and increment left_count of 1
            left_count_One++;
            count2--;
        }
 
        // If 0 is encountered,
        // looking for all combinations
        // of 1, 0, 1 possible
        else {
 
            // Number of ways to select
            // one 1 from left side
            // * Number of ways to select a 1
            // from right
            ans += (left_count_One * count2);
 
            // Decrement right_count of 2
            // and increment left_count of 2
            left_count_Zero++;
            count1--;
        }
    }
    return ans;
}
 
// Drivers code
int main()
{
    int arr[] = { 0, 0, 1, 1, 0, 1 };
    int N = 6;
 
    // Function call
    cout << numberOfWays(arr, N);
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to calculate the possible
  // number of ways to select 3 numbers
  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;
 
    // Storing the right counts of
    // 1s and 2s in the array
    for (int i = 0; i < N; i++) {
      if (A[i] == 1)
        count2++;
      else
        count1++;
    }
 
    // Traversing the array from left side
    for (int i = 0; i < N; i++) {
 
      // If 1 is encountered,
      // looking for all combinations of
      // 0, 1, 0 possible
      if (A[i] == 1) {
 
        // Number of ways to select one
        // 0 from left side * Number of
        // ways to select one 0 from right
        ans += (left_count_Zero * count1);
 
        // Decrement right_count of 1
        // and increment left_count of 1
        left_count_One++;
        count2--;
      }
 
      // If 0 is encountered,
      // looking for all combinations
      // of 1, 0, 1 possible
      else {
 
        // Number of ways to select
        // one 1 from left side
        // * Number of ways to select a 1
        // from right
        ans += (left_count_One * count2);
 
        // Decrement right_count of 2
        // and increment left_count of 2
        left_count_Zero++;
        count1--;
      }
    }
    return ans;
  }
 
  public static void main(String[] args)
  {
    int arr[] = { 0, 0, 1, 1, 0, 1 };
    int N = 6;
 
    // Function call
    System.out.print(numberOfWays(arr, N));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code for the above approach:
 
# Function to calculate the possible
# number of ways to select 3 numbers
def numberOfWays(A, N):
    left_count_Zero,count1,left_count_One,count2 = 0,0,0,0
    ans = 0
 
    # Storing the right counts of
    # 1s and 2s in the array
    for i in range(N):
        if (A[i] == 1):
            count2 += 1
        else:
            count1 += 1
 
    # Traversing the array from left side
    for i in range(N):
 
        # If 1 is encountered,
        # looking for all combinations of
        # 0, 1, 0 possible
        if (A[i] == 1):
 
            # Number of ways to select one
            # 0 from left side * Number of
            # ways to select one 0 from right
            ans += (left_count_Zero * count1)
 
            # Decrement right_count of 1
            # and increment left_count of 1
            left_count_One += 1
            count2 -= 1
 
        # If 0 is encountered,
        # looking for all combinations
        # of 1, 0, 1 possible
        else:
 
            # Number of ways to select
            # one 1 from left side
            # * Number of ways to select a 1
            # from right
            ans += (left_count_One * count2)
 
            # Decrement right_count of 2
            # and increment left_count of 2
            left_count_Zero += 1
            count1 -= 1
 
    return ans
 
# Drivers code
arr = [0, 0, 1, 1, 0, 1]
N = 6
 
# Function call
print(numberOfWays(arr, N))
 
# This code is contributed by shinjanpatra


C#




// C# code for the above approach
using System;
class GFG {
 
  // Function to calculate the possible
  // number of ways to select 3 numbers
  static long numberOfWays(int[] A, int N)
  {
    int left_count_Zero = 0, count1 = 0;
    int left_count_One = 0, count2 = 0;
    long ans = 0;
 
    // Storing the right counts of
    // 1s and 2s in the array
    for (int i = 0; i < N; i++) {
      if (A[i] == 1)
        count2++;
      else
        count1++;
    }
 
    // Traversing the array from left side
    for (int i = 0; i < N; i++) {
 
      // If 1 is encountered,
      // looking for all combinations of
      // 0, 1, 0 possible
      if (A[i] == 1) {
 
        // Number of ways to select one
        // 0 from left side * Number of
        // ways to select one 0 from right
        ans += (left_count_Zero * count1);
 
        // Decrement right_count of 1
        // and increment left_count of 1
        left_count_One++;
        count2--;
      }
 
      // If 0 is encountered,
      // looking for all combinations
      // of 1, 0, 1 possible
      else {
 
        // Number of ways to select
        // one 1 from left side
        // * Number of ways to select a 1
        // from right
        ans += (left_count_One * count2);
 
        // Decrement right_count of 2
        // and increment left_count of 2
        left_count_Zero++;
        count1--;
      }
    }
    return ans;
  }
 
  public static int Main()
  {
    int[] arr = new int[] { 0, 0, 1, 1, 0, 1 };
    int N = 6;
 
    // Function call
    Console.Write(numberOfWays(arr, N));
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript




<script>
    // JavaScript code for the above approach:
 
    // Function to calculate the possible
    // number of ways to select 3 numbers
    const numberOfWays = (A, N) => {
        let left_count_Zero = 0, count1 = 0;
        let left_count_One = 0, count2 = 0;
        let ans = 0;
 
        // Storing the right counts of
        // 1s and 2s in the array
        for (let i = 0; i < N; i++) {
            if (A[i] == 1)
                count2++;
            else
                count1++;
        }
 
        // Traversing the array from left side
        for (let i = 0; i < N; i++) {
 
            // If 1 is encountered,
            // looking for all combinations of
            // 0, 1, 0 possible
            if (A[i] == 1) {
 
                // Number of ways to select one
                // 0 from left side * Number of
                // ways to select one 0 from right
                ans += (left_count_Zero * count1);
 
                // Decrement right_count of 1
                // and increment left_count of 1
                left_count_One++;
                count2--;
            }
 
            // If 0 is encountered,
            // looking for all combinations
            // of 1, 0, 1 possible
            else {
 
                // Number of ways to select
                // one 1 from left side
                // * Number of ways to select a 1
                // from right
                ans += (left_count_One * count2);
 
                // Decrement right_count of 2
                // and increment left_count of 2
                left_count_Zero++;
                count1--;
            }
        }
        return ans;
    }
 
    // Drivers code
 
    let arr = [0, 0, 1, 1, 0, 1];
    let N = 6;
 
    // Function call
    document.write(numberOfWays(arr, N));
 
// This code is contributed by rakeshsahni
 
</script>


Output

6

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads