📜  乘积相等的子阵列数

📅  最后修改于: 2021-04-17 17:13:10             🧑  作者: Mango

给定一个由N个整数组成的数组arr [],任务是计算具有偶积的子数组的总数。

例子 :

天真的方法:解决此问题的最简单方法是从给定数组生成所有可能的子数组,并针对每个子数组检查其乘积是否为偶数。如果发现对任何子数组都是正确的,则增加计数。最后,打印获得的计数。

下面是上述方法的实现:

C++
// CPP implementation of the above approach
#include
using namespace std;
 
// Function to count subarrays with even product
void evenproduct(int arr[], int length)
{
   
  // Stores count of subarrays
  // with even product
  int count = 0;
 
  // Traverse the array
  for (int i = 0; i < length+1; i++) {
 
    // Initialize product
    int product = 1;
 
    for (int j = i; j < length+1; j++) {
 
      // Update product of the subarray
      product *= arr[j];
 
      if (product % 2 == 0)
        ++count;
    }
  }
 
  // Print total count of subarrays
  cout<


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG {
 
    // Function to count subarrays with even product
    static void evenproduct(int arr[], int length)
    {
        // Stores count of subarrays
        // with even product
        int count = 0;
 
        // Traverse the array
        for (int i = 0; i < arr.length; i++) {
 
            // Initialize product
            int product = 1;
 
            for (int j = i; j < arr.length; j++) {
 
                // Update product of the subarray
                product *= arr[j];
 
                if (product % 2 == 0)
                    ++count;
            }
        }
 
        // Print total count of subarrays
        System.out.println(count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int arr[] = { 7, 5, 4, 9 };
 
        // Length of an array
        int length = arr.length - 1;
 
        // Function call to count
        // subarrays with even product
        evenproduct(arr, length);
    }
}


Python3
# Python3 implementation of the above approach
 
# Function to count subarrays with even product
def evenproduct(arr, length) :
   
  # Stores count of subarrays
  # with even product
  count = 0;
 
  # Traverse the array
  for i in range(length + 1) :
 
    # Initialize product
    product = 1;
    for j in range(i, length + 1) :
 
      # Update product of the subarray
      product *= arr[j];
 
      if (product % 2 == 0) :
          count += 1;
 
  # Print total count of subarrays
  print(count);
 
# Driver Code
if __name__ == "__main__" :
   
    # Input
    arr = [ 7, 5, 4, 9 ];
     
    # Length of an array
    length = len(arr) - 1;
     
    # Function call to count
    # subarrays with even product
    evenproduct(arr, length);
 
    # This code is contributed by AnkThon


C#
// C# implementation of the above approach
using System;
public class GFG
{
 
  // Function to count subarrays with even product
  static void evenproduct(int []arr, int length)
  {
 
    // Stores count of subarrays
    // with even product
    int count = 0;
 
    // Traverse the array
    for (int i = 0; i < arr.Length; i++) {
 
      // Initialize product
      int product = 1;
 
      for (int j = i; j < arr.Length; j++) {
 
        // Update product of the subarray
        product *= arr[j];
 
        if (product % 2 == 0)
          ++count;
      }
    }
 
    // Print total count of subarrays
    Console.WriteLine(count);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
     
    // Input
    int []arr = { 7, 5, 4, 9 };
 
    // Length of an array
    int length = arr.Length - 1;
 
    // Function call to count
    // subarrays with even product
    evenproduct(arr, length);
  }
}
 
// This code is contributed by AnkThon


C++
#include 
using namespace std;
 
// Function to count subarrays
// with even product
void evenproduct(int arr[],
                 int length)
{
   
  // Total number of subarrays
  int total_subarray
    = length * (length + 1) / 2;
 
  // Counter variables
  int total_odd = 0;
  int count_odd = 0;
 
  // Traverse the array
  for (int i = 0; i < length; ++i) {
 
    // If current element is odd
    if (arr[i] % 2 == 0) {
      count_odd = 0;
    }
    else {
 
      ++count_odd;
 
      // Update count of subarrays
      // with odd product up to index i
      total_odd += count_odd;
    }
  }
 
  // Print count of subarrays
  // with even product
  cout << (total_subarray
           - total_odd) << endl;
}
 
// Driver code
int main()
{
 
  // Input
  int arr[] = { 7, 5, 4, 9 };
 
  // Length of an array
  int length = sizeof(arr) / sizeof(arr[0]);
 
  // Function call to count
  // even product subarrays
  evenproduct(arr, length);
 
  return 0;
}
 
// This code is contributed by splevel62.


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG {
 
    // Function to count subarrays
    // with even product
    static void evenproduct(int arr[],
                            int length)
    {
        // Total number of subarrays
        int total_subarray
            = length * (length + 1) / 2;
 
        // Counter variables
        int total_odd = 0;
        int count_odd = 0;
 
        // Traverse the array
        for (int i = 0; i < arr.length; ++i) {
 
            // If current element is odd
            if (arr[i] % 2 == 0) {
                count_odd = 0;
            }
            else {
 
                ++count_odd;
 
                // Update count of subarrays
                // with odd product up to index i
                total_odd += count_odd;
            }
        }
 
        // Print count of subarrays
        // with even product
        System.out.println(total_subarray
                           - total_odd);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int arr[] = { 7, 5, 4, 9 };
 
        // Length of an array
        int length = arr.length;
 
        // Function call to count
        // even product subarrays
        evenproduct(arr, length);
    }
}


Python3
# Function to count subarrays
# with even product
def evenproduct(arr, length):
 
    # Total number of subarrays
    total_subarray = length * (length + 1) // 2
 
    # Counter variables
    total_odd = 0
    count_odd = 0
 
    # Traverse the array
    for i in range(length):
 
        # If current element is odd
        if (arr[i] % 2 == 0):
            count_odd = 0
 
        else:
            count_odd += 1
 
            # Update count of subarrays
            # with odd product up to index i
            total_odd += count_odd
 
    # Print count of subarrays
    # with even product
    print(total_subarray
          - total_odd)
 
# Driver code
if __name__ == "__main__":
 
    # Input
    arr = [7, 5, 4, 9]
 
    # Length of an array
    length = len(arr)
 
    # Function call to count
    # even product subarrays
    evenproduct(arr, length)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to count subarrays
  // with even product
  static void evenproduct(int[] arr,
                          int length)
  {
 
    // Total number of subarrays
    int total_subarray
      = length * (length + 1) / 2;
 
    // Counter variables
    int total_odd = 0;
    int count_odd = 0;
 
    // Traverse the array
    for (int i = 0; i < arr.Length; ++i) {
 
      // If current element is odd
      if (arr[i] % 2 == 0) {
        count_odd = 0;
      }
      else {
 
        ++count_odd;
 
        // Update count of subarrays
        // with odd product up to index i
        total_odd += count_odd;
      }
    }
 
    // Print count of subarrays
    // with even product
    Console.WriteLine(total_subarray
                      - total_odd);
  }
 
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Input
    int[] arr = { 7, 5, 4, 9 };
 
    // Length of an array
    int length = arr.Length;
 
    // Function call to count
    // even product subarrays
    evenproduct(arr, length);
  }
}
 
// This code is contributed by code_hunt.


输出:
6

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

高效方法:请按照以下步骤优化上述方法:

  1. 在大小为N的阵列的子阵列的总数是N *(N + 1)/ 2。
  2. 具有奇数乘积的子数组的数量等于数组中存在的连续奇数元素的总数。
  3. 因此,乘积为偶数的子阵列的数量= (子阵列的总数–乘积为奇数的子阵列)
  4. 打印获得的子数组计数值。

下面是上述方法的实现:

C++

#include 
using namespace std;
 
// Function to count subarrays
// with even product
void evenproduct(int arr[],
                 int length)
{
   
  // Total number of subarrays
  int total_subarray
    = length * (length + 1) / 2;
 
  // Counter variables
  int total_odd = 0;
  int count_odd = 0;
 
  // Traverse the array
  for (int i = 0; i < length; ++i) {
 
    // If current element is odd
    if (arr[i] % 2 == 0) {
      count_odd = 0;
    }
    else {
 
      ++count_odd;
 
      // Update count of subarrays
      // with odd product up to index i
      total_odd += count_odd;
    }
  }
 
  // Print count of subarrays
  // with even product
  cout << (total_subarray
           - total_odd) << endl;
}
 
// Driver code
int main()
{
 
  // Input
  int arr[] = { 7, 5, 4, 9 };
 
  // Length of an array
  int length = sizeof(arr) / sizeof(arr[0]);
 
  // Function call to count
  // even product subarrays
  evenproduct(arr, length);
 
  return 0;
}
 
// This code is contributed by splevel62.

Java

// Java implementation of the above approach
import java.io.*;
 
class GFG {
 
    // Function to count subarrays
    // with even product
    static void evenproduct(int arr[],
                            int length)
    {
        // Total number of subarrays
        int total_subarray
            = length * (length + 1) / 2;
 
        // Counter variables
        int total_odd = 0;
        int count_odd = 0;
 
        // Traverse the array
        for (int i = 0; i < arr.length; ++i) {
 
            // If current element is odd
            if (arr[i] % 2 == 0) {
                count_odd = 0;
            }
            else {
 
                ++count_odd;
 
                // Update count of subarrays
                // with odd product up to index i
                total_odd += count_odd;
            }
        }
 
        // Print count of subarrays
        // with even product
        System.out.println(total_subarray
                           - total_odd);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int arr[] = { 7, 5, 4, 9 };
 
        // Length of an array
        int length = arr.length;
 
        // Function call to count
        // even product subarrays
        evenproduct(arr, length);
    }
}

Python3

# Function to count subarrays
# with even product
def evenproduct(arr, length):
 
    # Total number of subarrays
    total_subarray = length * (length + 1) // 2
 
    # Counter variables
    total_odd = 0
    count_odd = 0
 
    # Traverse the array
    for i in range(length):
 
        # If current element is odd
        if (arr[i] % 2 == 0):
            count_odd = 0
 
        else:
            count_odd += 1
 
            # Update count of subarrays
            # with odd product up to index i
            total_odd += count_odd
 
    # Print count of subarrays
    # with even product
    print(total_subarray
          - total_odd)
 
# Driver code
if __name__ == "__main__":
 
    # Input
    arr = [7, 5, 4, 9]
 
    # Length of an array
    length = len(arr)
 
    # Function call to count
    # even product subarrays
    evenproduct(arr, length)
 
    # This code is contributed by ukasp.

C#

// C# program for the above approach
using System;
class GFG
{
 
  // Function to count subarrays
  // with even product
  static void evenproduct(int[] arr,
                          int length)
  {
 
    // Total number of subarrays
    int total_subarray
      = length * (length + 1) / 2;
 
    // Counter variables
    int total_odd = 0;
    int count_odd = 0;
 
    // Traverse the array
    for (int i = 0; i < arr.Length; ++i) {
 
      // If current element is odd
      if (arr[i] % 2 == 0) {
        count_odd = 0;
      }
      else {
 
        ++count_odd;
 
        // Update count of subarrays
        // with odd product up to index i
        total_odd += count_odd;
      }
    }
 
    // Print count of subarrays
    // with even product
    Console.WriteLine(total_subarray
                      - total_odd);
  }
 
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Input
    int[] arr = { 7, 5, 4, 9 };
 
    // Length of an array
    int length = arr.Length;
 
    // Function call to count
    // even product subarrays
    evenproduct(arr, length);
  }
}
 
// This code is contributed by code_hunt.

输出:
6

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