FastPrepParity-Alternating Range Queries

Parity-Alternating Range Queries

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums and inclusive range queries [left, right], determine whether every adjacent pair inside each queried subarray has opposite parity.

Return one boolean per query in the original order. A one-element range is always valid.

Function

isAlternatingParity(nums: int[], queries: int[][]) → boolean[]

Examples

Example 1

nums = [3,4,1,2,6]queries = [[0,4],[1,3],[2,4]]return = [false,true,false]

The first and third ranges contain adjacent even values 2 and 6; range [1,3] alternates.

Example 2

nums = [2]queries = [[0,0]]return = [true]

A single value has no violating adjacent pair.

Example 3

nums = [1,1,2,3]queries = [[0,1],[1,3]]return = [false,true]

The first range starts with two odd values, while [1,3] is odd-even-odd.

Constraints

  • 1 <= nums.length <= 200000.
  • 1 <= queries.length <= 200000.
  • Each query satisfies 0 <= left <= right < nums.length.
  • -1000000000 <= nums[i] <= 1000000000.

More Google problems

See Google hiring insights
public boolean[] isAlternatingParity(int[] nums, int[][] queries) {
    // Write your code here.
}
nums[3,4,1,2,6]
queries[[0,4],[1,3],[2,4]]
expected[false,true,false]
Checking account…