📌  相关文章
📜  计算二进制数组中仅包含0和1的子数组

📅  最后修改于: 2021-06-26 17:35:15             🧑  作者: Mango

给定一个仅由零和一组成的二进制数组。任务是找到:

  • 其中只有1个子数组的数目。
  • 其中只有0个子数组的数目。

例子:

方法:要对1计数,其想法是开始使用计数器遍历数组。如果当前元素为1,则增加计数器,否则将counter *(counter + 1)/ 2添加到子数组的数量,然后将counter重新初始化为0。类似地,找到其中只有0的子数组的数量。
下面是上述方法的实现:

C++
// C++ program to count the number of subarrays
// that having only 0's and only 1's
#include 
using namespace std;
 
// Function to count number of subarrays
void countSubarraysof1and0(int a[], int n)
{
    int count1 = 0, count0 = 0;
 
    int number1 = 0, number0 = 0;
 
    // Iterate in the array to find count
    // of subarrays with only 1 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 1 or not
        if (a[i] == 1) {
            count1 += 1;
        }
        else {
            number1 += (count1) * (count1 + 1) / 2;
            count1 = 0;
        }
    }
 
    // Iterate in the array to find count
    // of subarrays with only 0 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 0 or not
        if (a[i] == 0) {
            count0 += 1;
        }
        else {
            number0 += (count0) * (count0 + 1) / 2;
            count0 = 0;
        }
    }
 
    // After iteration completes,
    // check for the last set of subarrays
    if (count1)
        number1 += (count1) * (count1 + 1) / 2;
 
    if (count0)
        number0 += (count0) * (count0 + 1) / 2;
 
    cout << "Count of subarrays of 0 only: " << number0;
    cout << "\nCount of subarrays of 1 only: " << number1;
}
 
// Driver Code
int main()
{
    int a[] = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 };
    int n = sizeof(a) / sizeof(a[0]);
 
    countSubarraysof1and0(a, n);
 
    return 0;
}


Java
// Java program to count the number of subarrays
// that having only 0's and only 1's
 
import java.io.*;
 
class GFG {
     
// Function to count number of subarrays
static void countSubarraysof1and0(int a[], int n)
{
    int count1 = 0, count0 = 0;
 
    int number1 = 0, number0 = 0;
 
    // Iterate in the array to find count
    // of subarrays with only 1 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 1 or not
        if (a[i] == 1) {
            count1 += 1;
        }
        else {
            number1 += (count1) * (count1 + 1) / 2;
            count1 = 0;
        }
    }
 
    // Iterate in the array to find count
    // of subarrays with only 0 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 0 or not
        if (a[i] == 0) {
            count0 += 1;
        }
        else {
            number0 += (count0) * (count0 + 1) / 2;
            count0 = 0;
        }
    }
 
    // After iteration completes,
    // check for the last set of subarrays
    if (count1>0)
        number1 += (count1) * (count1 + 1) / 2;
 
    if (count0>0)
        number0 += (count0) * (count0 + 1) / 2;
 
    System.out.println("Count of subarrays of 0 only: " + number0);
    System.out.println( "\nCount of subarrays of 1 only: " + number1);
}
 
// Driver Code
 
 
    public static void main (String[] args) {
        int a[] = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 };
    int n = a.length;
 
    countSubarraysof1and0(a, n);;
    }
}
// This code is contributed by inder_verma..


Python3
# Python 3 program to count the number of
# subarrays that having only 0's and only 1's
 
# Function to count number of subarrays
def countSubarraysof1and0(a, n):
    count1 = 0
    count0 = 0
 
    number1 = 0
    number0 = 0
 
    # Iterate in the array to find count
    # of subarrays with only 1 in it
    for i in range(0, n, 1):
         
        # check if array element is 1 or not
        if (a[i] == 1):
            count1 += 1
        else:
            number1 += ((count1) *
                        (count1 + 1) / 2)
            count1 = 0
 
    # Iterate in the array to find count
    # of subarrays with only 0 in it
    for i in range(0, n, 1):
         
        # check if array element
        # is 0 or not
        if (a[i] == 0):
            count0 += 1
        else:
            number0 += (count0) * (count0 + 1) / 2
            count0 = 0
     
    # After iteration completes,
    # check for the last set of subarrays
    if (count1):
        number1 += (count1) * (count1 + 1) / 2
 
    if (count0):
        number0 += (count0) * (count0 + 1) / 2
 
    print("Count of subarrays of 0 only:",
                             int(number0))
    print("Count of subarrays of 1 only:",
                             int(number1))
 
# Driver Code
if __name__ == '__main__':
    a = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1]
    n = len(a)
 
    countSubarraysof1and0(a, n)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to count the number of subarrays
// that having only 0's and only 1's
 
using System;
 
class GFG {
     
// Function to count number of subarrays
static void countSubarraysof1and0(int []a, int n)
{
    int count1 = 0, count0 = 0;
 
    int number1 = 0, number0 = 0;
 
    // Iterate in the array to find count
    // of subarrays with only 1 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 1 or not
        if (a[i] == 1) {
            count1 += 1;
        }
        else {
            number1 += (count1) * (count1 + 1) / 2;
            count1 = 0;
        }
    }
 
    // Iterate in the array to find count
    // of subarrays with only 0 in it
    for (int i = 0; i < n; i++) {
        // check if array element
        // is 0 or not
        if (a[i] == 0) {
            count0 += 1;
        }
        else {
            number0 += (count0) * (count0 + 1) / 2;
            count0 = 0;
        }
    }
 
    // After iteration completes,
    // check for the last set of subarrays
    if (count1>0)
        number1 += (count1) * (count1 + 1) / 2;
 
    if (count0>0)
        number0 += (count0) * (count0 + 1) / 2;
 
    Console.WriteLine("Count of subarrays of 0 only: " + number0);
    Console.WriteLine( "\nCount of subarrays of 1 only: " + number1);
}
 
// Driver Code
 
 
    public static void Main () {
        int []a = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 };
    int n = a.Length;
 
    countSubarraysof1and0(a, n);;
    }
}
// This code is contributed by inder_verma..


PHP


Javascript


输出:
Count of subarrays of 0 only: 5
Count of subarrays of 1 only: 15

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。