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, answer equals to 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 condition int 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 code int main() { int arr[] = { 1, 0, 1, 0, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countWays(arr, n); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach class GFG { // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition static 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 code public 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 |
chevron_right
filter_none
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 condition def 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 code if __name__ == '__main__': arr = [1, 0, 1, 0, 1] n = len(arr) print(countWays(arr, n)) # This code is contributed by # Surendra_Gangwar |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition static 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 code public 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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
4
Recommended Posts:
- Divide an array into K subarray with the given condition
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Number of ways to divide an array into K equal sum sub-arrays
- Count number of ways to divide an array into two halves with same sum
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Count subarrays consisting of only 0's and only 1's in a binary array
- Divide binary array into three equal parts with same value
- Queries for decimal values of subarrays of a binary array
- Sum of maximum of all subarrays | Divide and Conquer
- Minimum length subarray of 1s in a Binary Array
- Check in binary array the number represented by a subarray is odd or even
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Maximum Sum SubArray using Divide and Conquer | Set 2
- Maximum Subarray Sum using Divide and Conquer algorithm
- Count ways to divide circle using N non-intersecting chord | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



