📜  奇数乘积的子阵列数

📅  最后修改于: 2021-06-26 01:36:12             🧑  作者: Mango

给定大小为N的整数数组arr [] ,任务是计算具有奇积的子数组的数量。
例子:

天真的方法:一个简单的解决方案是计算每个子数组的乘积,并检查其是否为奇数,然后相应地计算计数。
时间复杂度: O(N 2 )
有效的方法:奇数乘积只能由奇数的乘积来实现。因此,对于数组中每K个连续的奇数,具有奇数乘积的子数组的数量增加K *(K + 1)/ 2 。计算连续奇数的一种方法是计算每两个连续偶数的索引之间的差,并将其相减1 。为了进行计算,将-1N视为偶数索引。
下面是上述方法的实现:

C++
// C++ program to find the count of
// sub-arrays with odd product
#include 
using namespace std;
 
// Function that returns the count of
// sub-arrays with odd product
int countSubArrayWithOddProduct(int* A, int N)
{
    // Initialize the count variable
    int count = 0;
 
    // Initialize variable to store the
    // last index with even number
    int last = -1;
 
    // Initialize variable to store
    // count of continuous odd numbers
    int K = 0;
 
    // Loop through the array
    for (int i = 0; i < N; i++) {
        // Check if the number
        // is even or not
        if (A[i] % 2 == 0) {
            // Calculate count of continuous
            // odd numbers
            K = (i - last - 1);
 
            // Increase the count of sub-arrays
            // with odd product
            count += (K * (K + 1) / 2);
 
            // Store the index of last
            // even number
            last = i;
        }
    }
 
    // N considered as index of
    // even number
    K = (N - last - 1);
 
    count += (K * (K + 1) / 2);
 
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 12, 15, 7, 3, 25,
                  6, 2, 1, 1, 7 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << countSubArrayWithOddProduct(arr, n);
 
    return 0;
}


Java
// Java program to find the count of
// sub-arrays with odd product
class GFG {
 
// Function that returns the count of
// sub-arrays with odd product
static int countSubArrayWithOddProduct(int A[],
                                       int N)
{
     
    // Initialize the count variable
    int count = 0;
 
    // Initialize variable to store the
    // last index with even number
    int last = -1;
 
    // Initialize variable to store
    // count of continuous odd numbers
    int K = 0;
 
    // Loop through the array
    for(int i = 0; i < N; i++)
    {
 
       // Check if the number
       // is even or not
       if (A[i] % 2 == 0)
       {
 
           // Calculate count of continuous
           // odd numbers
           K = (i - last - 1);
            
           // Increase the count of sub-arrays
           // with odd product
           count += (K * (K + 1) / 2);
            
           // Store the index of last
           // even number
           last = i;
       }
    }
 
    // N considered as index of
    // even number
    K = (N - last - 1);
    count += (K * (K + 1) / 2);
     
    return count;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 12, 15, 7, 3, 25, 6, 2, 1, 1, 7 };
    int n = arr.length;
 
    // Function call
    System.out.println(countSubArrayWithOddProduct(arr, n));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to find the count of
# sub-arrays with odd product
 
# Function that returns the count of
# sub-arrays with odd product
def countSubArrayWithOddProduct(A, N):
     
    # Initialize the count variable
    count = 0
 
    # Initialize variable to store the
    # last index with even number
    last = -1
 
    # Initialize variable to store
    # count of continuous odd numbers
    K = 0
 
    # Loop through the array
    for i in range(N):
         
        # Check if the number
        # is even or not
        if (A[i] % 2 == 0):
             
            # Calculate count of continuous
            # odd numbers
            K = (i - last - 1)
 
            # Increase the count of sub-arrays
            # with odd product
            count += (K * (K + 1) / 2)
 
            # Store the index of last
            # even number
            last = i
 
    # N considered as index of
    # even number
    K = (N - last - 1)
 
    count += (K * (K + 1) / 2)
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 12, 15, 7, 3, 25, 6, 2, 1, 1, 7 ]
    n = len(arr)
 
    # Function call
    print(int(countSubArrayWithOddProduct(arr, n)))
 
# This code is contributed by Bhupendra_Singh


C#
// C# program to find the count of
// sub-arrays with odd product
using System;
class GFG{
 
// Function that returns the count of
// sub-arrays with odd product    
static int countSubArrayWithOddProduct(int[] A,
                                       int N)
{
         
    // Initialize the count variable
    int count = 0;
     
    // Initialize variable to store the
    // last index with even number
    int last = -1;
     
    // Initialize variable to store
    // count of continuous odd numbers
    int K = 0;
     
    // Loop through the array
    for(int i = 0; i < N; i++)
    {
        
       // Check if the number
       // is even or not
       if (A[i] % 2 == 0)
       {
            
           // Calculate count of continuous
           // odd numbers
           K = (i - last - 1);
            
           // Increase the count of sub-arrays
           // with odd product
           count += (K * (K + 1) / 2);
            
           // Store the index of last
           // even number
           last = i;
       }
    }
     
    // N considered as index of
    // even number
    K = (N - last - 1);
    count += (K * (K + 1) / 2);
     
    return count;
}
 
// Driver code
static void Main()
{
    int[] arr = { 12, 15, 7, 3, 25,
                  6, 2, 1, 1, 7 };
    int n = arr.Length;
     
    // Function call
    Console.WriteLine(countSubArrayWithOddProduct(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
16

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

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