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++
#include <bits/stdc++.h>
using namespace std;
int countWays( int arr[], int n)
{
int pos[n], p = 0, i;
for (i = 0; i < n; i++) {
if (arr[i] == 1) {
pos[p] = i + 1;
p++;
}
}
if (p == 0)
return 0;
int ways = 1;
for (i = 0; i < p - 1; i++) {
ways *= pos[i + 1] - pos[i];
}
return ways;
}
int main()
{
int arr[] = { 1, 0, 1, 0, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countWays(arr, n);
return 0;
}
|
Java
class GFG
{
static int countWays( int arr[], int n)
{
int pos[] = new int [n];
int p = 0 , i;
for (i = 0 ; i < n; i++)
{
if (arr[i] == 1 )
{
pos[p] = i + 1 ;
p++;
}
}
if (p == 0 )
return 0 ;
int ways = 1 ;
for (i = 0 ; i < p - 1 ; i++)
{
ways *= pos[i + 1 ] - pos[i];
}
return ways;
}
public static void main(String args[])
{
int [] arr = { 1 , 0 , 1 , 0 , 1 };
int n = arr.length;
System.out.println(countWays(arr, n));
}
}
|
Python3
def countWays(are, n):
pos = [ 0 for i in range (n)]
p = 0
for i in range (n):
if (arr[i] = = 1 ):
pos[p] = i + 1
p + = 1
if (p = = 0 ):
return 0
ways = 1
for i in range (p - 1 ):
ways * = pos[i + 1 ] - pos[i]
return ways
if __name__ = = '__main__' :
arr = [ 1 , 0 , 1 , 0 , 1 ]
n = len (arr)
print (countWays(arr, n))
|
C#
using System;
class GFG
{
static int countWays( int [] arr, int n)
{
int [] pos = new int [n];
int p = 0, i;
for (i = 0; i < n; i++)
{
if (arr[i] == 1)
{
pos[p] = i + 1;
p++;
}
}
if (p == 0)
return 0;
int ways = 1;
for (i = 0; i < p - 1; i++)
{
ways *= pos[i + 1] - pos[i];
}
return ways;
}
public static void Main()
{
int [] arr = { 1, 0, 1, 0, 1 };
int n = arr.Length;
Console.Write(countWays(arr, n));
}
}
|
PHP
<?php
function countWays( $arr , $n )
{
$pos = array_fill (0, $n , 0);
$p = 0 ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 1)
{
$pos [ $p ] = $i + 1;
$p ++;
}
}
if ( $p == 0)
return 0;
$ways = 1;
for ( $i = 0; $i < $p - 1; $i ++)
{
$ways *= $pos [ $i + 1] - $pos [ $i ];
}
return $ways ;
}
$arr = array (1, 0, 1, 0, 1);
$n = sizeof( $arr );
echo countWays( $arr , $n );
?>
|
Javascript
<script>
function countWays(arr, n) {
var pos = new Array(n).fill(0);
var p = 0, i;
for (i = 0; i < n; i++) {
if (arr[i] === 1) {
pos[p] = i + 1;
p++;
}
}
if (p === 0)
return 0;
var ways = 1;
for (i = 0; i < p - 1; i++) {
ways *= pos[i + 1] - pos[i];
}
return ways;
}
var arr = [1, 0, 1, 0, 1];
var n = arr.length;
document.write(countWays(arr, n));
</script>
|
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
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...