Leetcode Problem 2495. Number of Subarrays Having Even Product

2495. Number of Subarrays Having Even Product

Leetcode Solutions

Counting Subarrays with Even Product

  1. Initialize a variable lastEven to -1, which will hold the index of the last even number encountered.
  2. Initialize a variable count to 0, which will hold the count of subarrays with an even product.
  3. Iterate through the array using an index i. a. If nums[i] is even, update lastEven to i. b. Add lastEven + 1 to count, as all subarrays ending at i that start before or at lastEven will have an even product.
  4. Return the value of count.
UML Thumbnail

Subtracting Odd Subarray Counts from Total Subarrays

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...