📌  相关文章
📜  m个奇数的子数组的数量

📅  最后修改于: 2022-05-13 01:57:50.330000             🧑  作者: Mango

m个奇数的子数组的数量

给定一个包含 n 个元素和一个整数 m 的数组,我们需要编写一个程序来查找数组中恰好包含 m 个奇数的连续子数组的数量。

例子 :

朴素方法:朴素方法是生成所有可能的子数组并同时检查具有 m 个奇数的子数组。
下面是上述方法的实现:

C++
// CPP program to count the
// Number of subarrays with
// m odd numbers
#include 
using namespace std;

// function that returns
// the count of subarrays
// with m odd numbers
int countSubarrays(int a[], int n, int m)
{
    int count = 0;

    // traverse for all
    // possible subarrays
    for (int i = 0; i < n; i++) 
    {
        int odd = 0;
        for (int j = i; j < n; j++) 
        {
            if (a[j] % 2)
                odd++;

            // if count of odd numbers in
            // subarray is m
            if (odd == m)
                count++;
        }
    }
    return count;
}

// Driver Code
int main()
{
    int a[] = { 2, 2, 5, 6, 9, 2, 11 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = 2;

    cout << countSubarrays(a, n, m);
    return 0;
}


Java
// Java program to count the number of
// subarrays with m odd numbers
import java.util.*;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    static int countSubarrays(int a[], int n, int m)
    {

        int count = 0;

        // traverse for all possible
        // subarrays
        for (int i = 0; i < n; i++) 
        {
            int odd = 0;
            for (int j = i; j < n; j++) 
            {
                if (a[j] % 2 != 0)
                    odd++;

                // if count of odd numbers
                // in subarray is m
                if (odd == m)
                    count++;
            }
        }

        return count;
    }

    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.length;
        int m = 2;

        System.out.println(countSubarrays(a, n, m));
    }
}

// This code is contributed by akash1295.


Python3
# Python3 program to count the
# Number of subarrays with
# m odd numbers

# function that returns the count
# of subarrays with m odd numbers


def countSubarrays(a, n, m):
    count = 0

    # traverse for all
    # possible subarrays
    for i in range(n):
        odd = 0
        for j in range(i, n):
            if (a[j] % 2):
                odd += 1

            # if count of odd numbers
            # in subarray is m
            if (odd == m):
                count += 1
    return count


# Driver Code
a = [2, 2, 5, 6, 9, 2, 11]
n = len(a)
m = 2

print(countSubarrays(a, n, m))

# This code is contributed by mits


C#
// C# program to count the number of
// subarrays with m odd numbers
using System;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    static int countSubarrays(int[] a, int n, int m)
    {

        int count = 0;

        // traverse for all possible
        // subarrays
        for (int i = 0; i < n; i++) 
        {
            int odd = 0;
            for (int j = i; j < n; j++)
            {
                if (a[j] % 2 == 0)
                    odd++;

                // if count of odd numbers
                // in subarray is m
                if (odd == m)
                    count++;
            }
        }

        return count;
    }

    // Driver code
    public static void Main()
    {
        int[] a = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.Length;
        int m = 2;

        Console.WriteLine(countSubarrays(a, n, m));
    }
}

// This code is contributed by anuj_67.


PHP


Javascript


C++
// CPP program to count the Number
// of subarrays with m odd numbers
// O(N) approach
#include 
using namespace std;

// function that returns the count
// of subarrays with m odd numbers
int countSubarrays(int a[], int n, int m)
{
    int count = 0;
    int prefix[n + 1] = { 0 };
    int odd = 0;

    // traverse in the array
    for (int i = 0; i < n; i++) 
    {

        prefix[odd]++;

        // if array element is odd
        if (a[i] & 1)
            odd++;

        // when number of odd elements>=M
        if (odd >= m)
            count += prefix[odd - m];
    }

    return count;
}

// Driver Code
int main()
{
    int a[] = { 2, 2, 5, 6, 9, 2, 11 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = 2;

    cout << countSubarrays(a, n, m);

    return 0;
}


Java
// Java program to count the
// number of subarrays with
// m odd numbers
import java.util.*;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    public static int countSubarrays(int a[], int n, int m)
    {
        int count = 0;
        int prefix[] = new int[n + 1];
        int odd = 0;

        // Traverse in the array
        for (int i = 0; i < n; i++) 
        {
            prefix[odd]++;

            // If array element is odd
            if ((a[i] & 1) == 1)
                odd++;

            // When number of odd
            // elements >= M
            if (odd >= m)
                count += prefix[odd - m];
        }

        return count;
    }

    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.length;
        int m = 2;

        // Function call
        System.out.println(countSubarrays(a, n, m));
    }
}

// This code is contributed by akash1295.


Python3
# Python3 program to count the Number
# of subarrays with m odd numbers
# O(N) approach

# function that returns the count
# of subarrays with m odd numbers


def countSubarrays(a, n, m):
    count = 0
    prefix = [0] * (n+1)
    odd = 0

    # traverse in the array
    for i in range(n):
        prefix[odd] += 1

        # if array element is odd
        if (a[i] & 1):
            odd += 1

        # when number of odd elements>=M
        if (odd >= m):
            count += prefix[odd - m]

    return count


# Driver Code
a = [2, 2, 5, 6, 9, 2, 11]
n = len(a)
m = 2

print(countSubarrays(a, n, m))

# This code is contributed 29Ajaykumar


C#
// C# program to count the number of
// subarrays with m odd numbers
using System;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    public static int countSubarrays(int[] a, int n, int m)
    {
        int count = 0;
        int[] prefix = new int[n + 1];
        int odd = 0;

        // traverse in the array
        for (int i = 0; i < n; i++) 
        {
            prefix[odd]++;

            // if array element is odd
            if ((a[i] & 1) == 1)
                odd++;

            // when number of odd
            // elements >= M
            if (odd >= m)
                count += prefix[odd - m];
        }

        return count;
    }

    // Driver code
    public static void Main()
    {
        int[] a = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.Length;
        int m = 2;

        Console.WriteLine(countSubarrays(a, n, m));
    }
}

// This code is contributed by anuj_67.


PHP
=M 
        if ($odd >= $m) 
            $count += $prefix[$odd - $m]; 
    } 

    return $count; 
} 

// Driver Code 
$a = array(2, 2, 5, 6, 9, 2, 11 ); 
$n = sizeof($a); 
$m = 2; 

echo countSubarrays($a, $n, $m); 

// This code is contributed 
// by Shivi_Aggarwal
?>


Javascript


输出 :

8

时间复杂度: O(n 2 )

高效方法:一种有效的方法是在遍历时计算prefix[]数组。 Prefix[i] 存储其中包含“i”个奇数的前缀的数量。如果数组元素是奇数,我们增加奇数的计数。当奇数个数超过或等于 m 时,将具有“(odd-m)”个数的前缀数添加到答案中。在每一步奇数> = m,我们在前缀数组的帮助下计算形成的子数组的数量直到特定索引。 prefix[odd-m] 为我们提供了具有“odd-m”奇数的前缀的数量,将其添加到计数中以获得直到索引的子数组的数量。
下面是上述方法的实现:

C++

// CPP program to count the Number
// of subarrays with m odd numbers
// O(N) approach
#include 
using namespace std;

// function that returns the count
// of subarrays with m odd numbers
int countSubarrays(int a[], int n, int m)
{
    int count = 0;
    int prefix[n + 1] = { 0 };
    int odd = 0;

    // traverse in the array
    for (int i = 0; i < n; i++) 
    {

        prefix[odd]++;

        // if array element is odd
        if (a[i] & 1)
            odd++;

        // when number of odd elements>=M
        if (odd >= m)
            count += prefix[odd - m];
    }

    return count;
}

// Driver Code
int main()
{
    int a[] = { 2, 2, 5, 6, 9, 2, 11 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = 2;

    cout << countSubarrays(a, n, m);

    return 0;
}

Java

// Java program to count the
// number of subarrays with
// m odd numbers
import java.util.*;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    public static int countSubarrays(int a[], int n, int m)
    {
        int count = 0;
        int prefix[] = new int[n + 1];
        int odd = 0;

        // Traverse in the array
        for (int i = 0; i < n; i++) 
        {
            prefix[odd]++;

            // If array element is odd
            if ((a[i] & 1) == 1)
                odd++;

            // When number of odd
            // elements >= M
            if (odd >= m)
                count += prefix[odd - m];
        }

        return count;
    }

    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.length;
        int m = 2;

        // Function call
        System.out.println(countSubarrays(a, n, m));
    }
}

// This code is contributed by akash1295.

Python3

# Python3 program to count the Number
# of subarrays with m odd numbers
# O(N) approach

# function that returns the count
# of subarrays with m odd numbers


def countSubarrays(a, n, m):
    count = 0
    prefix = [0] * (n+1)
    odd = 0

    # traverse in the array
    for i in range(n):
        prefix[odd] += 1

        # if array element is odd
        if (a[i] & 1):
            odd += 1

        # when number of odd elements>=M
        if (odd >= m):
            count += prefix[odd - m]

    return count


# Driver Code
a = [2, 2, 5, 6, 9, 2, 11]
n = len(a)
m = 2

print(countSubarrays(a, n, m))

# This code is contributed 29Ajaykumar

C#

// C# program to count the number of
// subarrays with m odd numbers
using System;

class GFG {

    // function that returns the count of
    // subarrays with m odd numbers
    public static int countSubarrays(int[] a, int n, int m)
    {
        int count = 0;
        int[] prefix = new int[n + 1];
        int odd = 0;

        // traverse in the array
        for (int i = 0; i < n; i++) 
        {
            prefix[odd]++;

            // if array element is odd
            if ((a[i] & 1) == 1)
                odd++;

            // when number of odd
            // elements >= M
            if (odd >= m)
                count += prefix[odd - m];
        }

        return count;
    }

    // Driver code
    public static void Main()
    {
        int[] a = { 2, 2, 5, 6, 9, 2, 11 };
        int n = a.Length;
        int m = 2;

        Console.WriteLine(countSubarrays(a, n, m));
    }
}

// This code is contributed by anuj_67.

PHP

=M 
        if ($odd >= $m) 
            $count += $prefix[$odd - $m]; 
    } 

    return $count; 
} 

// Driver Code 
$a = array(2, 2, 5, 6, 9, 2, 11 ); 
$n = sizeof($a); 
$m = 2; 

echo countSubarrays($a, $n, $m); 

// This code is contributed 
// by Shivi_Aggarwal
?>

Javascript

输出 :

8

时间复杂度: O(n)