Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Hash Maps
Google
Count Subarrays With Even Odd Ratio I

Count the number of contiguous subarrays where the ratio of even numbers to odd numbers satisfies a given condition.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums, count how many contiguous subarrays satisfy the condition described by the problem’s even/odd ratio definition.

This problem is centered on turning a subarray condition into a running-state counting problem. You should look for a way to evaluate many subarrays efficiently instead of checking each one independently.

Notes

  • A subarray must be contiguous.
  • The condition depends only on the parity counts inside the subarray.
  • The array may contain positive, negative, or zero values; parity is determined in the usual way.

Goal

Return the total number of valid subarrays.

Input Format

  • An integer array nums.
  • The exact ratio threshold/condition is implied by the problem statement and the title.

Output Format

  • Return an integer representing the number of contiguous subarrays that satisfy the condition.

Constraints

  • 1 <= nums.length.
  • Values fit in standard 32-bit signed integers.
  • Expected solution should be better than O(n2)O(n^2) for typical interview constraints.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3, 4]

Output

4

Explanation

Illustrative example: count all contiguous subarrays that satisfy the even/odd ratio rule.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.