Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1

Give an integer array arr[] consisting of elements from the set {0, 1}. The task is to print the number of ways the array can be divided into sub-arrays such that each sub-array contains exactly one 1.
Examples:Â
Input: arr[] = {1, 0, 1, 0, 1}Â
Output: 4Â
Below are the possible ways:Â
- {1, 0}, {1, 0}, {1}
- {1}, {0, 1, 0}, {1}
- {1, 0}, {1}, {0, 1}
- {1}, {0, 1}, {0, 1}
Input: arr[] = {0, 0, 0}Â
Output: 0Â
Approach:
- When all the elements of the array are 0, then the result will be zero.
- Else, between two adjacent ones, we must have only one separation. So, the answer equals the product of values posi + 1 – posi (for all valid pairs) where posi is the position of ith 1.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the number of ways// the array can be divided into sub-arrays// satisfying the given conditionint countWays(int arr[], int n){Â
    int pos[n], p = 0, i;Â
    // for loop for saving the positions of all 1s    for (i = 0; i < n; i++) {        if (arr[i] == 1) {            pos[p] = i + 1;            p++;        }    }Â
    // If array contains only 0s    if (p == 0)        return 0;Â
    int ways = 1;    for (i = 0; i < p - 1; i++) {        ways *= pos[i + 1] - pos[i];    }Â
    // Return the total ways    return ways;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 0, 1, 0, 1 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << countWays(arr, n);Â Â Â Â return 0;} |
Java
// Java implementation of the approachclass GFG{Â Â Â Â Â // Function to return the number of ways// the array can be divided into sub-arrays// satisfying the given conditionstatic int countWays(int arr[], int n){Â Â Â Â int pos[] = new int[n]; Â Â Â Â int p = 0, i;Â
    // for loop for saving the     // positions of all 1s    for (i = 0; i < n; i++)     {        if (arr[i] == 1)         {            pos[p] = i + 1;            p++;        }    }Â
    // If array contains only 0s    if (p == 0)        return 0;Â
    int ways = 1;    for (i = 0; i < p - 1; i++)     {        ways *= pos[i + 1] - pos[i];    }Â
    // Return the total ways    return ways;}Â
// Driver codepublic static void main(String args[]){Â Â Â Â int[] arr = { 1, 0, 1, 0, 1 };Â Â Â Â int n = arr.length;Â Â Â Â System.out.println(countWays(arr, n));}}Â
// This code is contributed // by Akanksha Rai |
Python3
# Python 3 implementation of the approachÂ
# Function to return the number of ways# the array can be divided into sub-arrays# satisfying the given conditiondef countWays(are, n):Â Â Â Â pos = [0 for i in range(n)]Â Â Â Â p = 0Â
    # for loop for saving the positions    # of all 1s    for i in range(n):        if (arr[i] == 1):            pos[p] = i + 1            p += 1Â
    # If array contains only 0s    if (p == 0):        return 0Â
    ways = 1    for i in range(p - 1):        ways *= pos[i + 1] - pos[i]Â
    # Return the total ways    return waysÂ
# Driver codeif __name__ == '__main__':Â Â Â Â arr = [1, 0, 1, 0, 1]Â Â Â Â n = len(arr)Â Â Â Â print(countWays(arr, n))Â Â Â Â Â # This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approachusing System;Â
class GFG{Â Â Â Â Â // Function to return the number of ways// the array can be divided into sub-arrays// satisfying the given conditionstatic int countWays(int[] arr, int n){Â Â Â Â int[] pos = new int[n]; Â Â Â Â int p = 0, i;Â
    // for loop for saving the positions    // of all 1s    for (i = 0; i < n; i++)     {        if (arr[i] == 1)         {            pos[p] = i + 1;            p++;        }    }Â
    // If array contains only 0s    if (p == 0)        return 0;Â
    int ways = 1;    for (i = 0; i < p - 1; i++)     {        ways *= pos[i + 1] - pos[i];    }Â
    // Return the total ways    return ways;}Â
// Driver codepublic static void Main(){Â Â Â Â int[] arr = { 1, 0, 1, 0, 1 };Â Â Â Â int n = arr.Length;Â Â Â Â Console.Write(countWays(arr, n));}}Â
// This code is contributed // by Akanksha Rai |
PHP
<?php// PHP implementation of the approach Â
// Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition function countWays($arr, $n) { Â Â Â Â $pos = array_fill(0, $n, 0); Â Â Â Â $p = 0 ;Â
    // for loop for saving the positions    // of all 1s     for ($i = 0; $i < $n; $i++)    {         if ($arr[$i] == 1)         {             $pos[$p] = $i + 1;             $p++;         }     } Â
    // If array contains only 0s     if ($p == 0)         return 0; Â
    $ways = 1;     for ($i = 0; $i < $p - 1; $i++)     {         $ways *= $pos[$i + 1] - $pos[$i];     } Â
    // Return the total ways     return $ways; } Â
// Driver code $arr = array(1, 0, 1, 0, 1); $n = sizeof($arr); echo countWays($arr, $n); Â
// This code is contributed by Ryuga?> |
Javascript
<script>      // JavaScript implementation of the approach      // Function to return the number of ways      // the array can be divided into sub-arrays      // satisfying the given condition      function countWays(arr, n) {        var pos = new Array(n).fill(0);        var p = 0, i;Â
        // for loop for saving the positions        // of all 1s        for (i = 0; i < n; i++) {          if (arr[i] === 1) {            pos[p] = i + 1;            p++;          }        }Â
        // If array contains only 0s        if (p === 0)             return 0;Â
        var ways = 1;        for (i = 0; i < p - 1; i++) {          ways *= pos[i + 1] - pos[i];        }Â
        // Return the total ways        return ways;      }Â
      // Driver code      var arr = [1, 0, 1, 0, 1];      var n = arr.length;      document.write(countWays(arr, n));</script> |
Output
4
Complexity Analysis:
- Time Complexity: O(n), where n is the size of the given array
- Auxiliary Space: O(n), as extra space of size n was used
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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



