📜  将二进制数组划分为子数组的方式,以使每个子数组恰好包含一个1

📅  最后修改于: 2021-04-29 05:45:50             🧑  作者: Mango

给出一个由数组{0,1}中的元素组成的整数数组arr [] 。任务是打印将数组划分为子数组的方式的数量,以使每个子数组恰好包含一个1

例子:

方法:

  • 当数组的所有元素均为0时,结果将为零。
  • 否则,两个相邻的对象之间必须只有一个分隔。因此,答案等于值pos i + 1 -pos i (对于所有有效对)的乘积,其中pos i是第i 1位置。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


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


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


PHP


输出:
4