给定一个大小为N的整数数组arr[] ,任务是计算具有奇数乘积的子数组的数量。
例子:
Input : arr[] = {5, 1, 2, 3, 4}
Output : 4
Explanation: The sub-arrays with odd product are-
{5}, {1}, {3}, {5, 1}. Hence the count is 4.
Input : arr[] = {12, 15, 7, 3, 25, 6, 2, 1, 1, 7}
Output : 16
朴素的方法:一个简单的解决方案是计算每个子数组的乘积并检查它是否为奇数并相应地计算计数。
时间复杂度: O(N 2 )
有效方法:奇数乘积只有奇数乘积才有可能。因此,对于数组中的每K 个连续奇数,具有奇数乘积的子数组的数量增加K*(K+1)/2 。计算连续奇数的一种方法是计算每两个连续偶数的索引之间的差值并减去1 。对于计算, -1和N被视为偶数的索引。
下面是上述方法的实现:
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 Classes Live