Remove the most range-decrement queries while still reducing an array to zero

Quick Overview

Given an array and a list of range queries that can each lower every index in their range by at most one, count how many queries can be dropped while the array can still be reduced to all zeros. It tests greedy choices over intervals and an efficient sweep for inputs of up to 100,000 elements.

Remove the most range-decrement queries while still reducing an array to zero

Company: Zynga

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `nums` of length `n` and a 2D array `queries` where `queries[i] = [l_i, r_i]`. Each query lets you decrement the value at every index in the range `[l_i, r_i]` of `nums` by at most 1. The amount (0 or 1) is chosen independently for each index in the range. A zero array is an array whose elements are all `0`. Return the maximum number of queries that can be removed from `queries` so that `nums` can still be converted to a zero array using the remaining queries. If `nums` cannot be converted to a zero array even with every query, return `-1`. ### Function Signature ```python def max_removal(nums: list[int], queries: list[list[int]]) -> int: ``` ### Rules - Each kept query is applied once. For every index in `[l, r]` (both ends inclusive) it decrements that index by either 0 or 1, chosen independently per index. - The order in which the kept queries are applied does not matter. - The function returns only the number of removed queries, so the expected output is a single integer. ### Constraints - `1 <= len(nums) <= 10^5` - `0 <= nums[i] <= 10^5` - `1 <= len(queries) <= 10^5` - `len(queries[i]) == 2` - `0 <= l_i <= r_i < len(nums)` ### Examples ```text Input: nums = [2, 0, 2], queries = [[0, 2], [0, 2], [1, 1]] Output: 1 ``` Keeping both `[0, 2]` queries decrements indices 0 and 2 twice each, and both queries skip index 1 because it is already 0. The query `[1, 1]` is not needed. Removing a second query would leave at most one `[0, 2]`, and index 0 needs two decrements. ```text Input: nums = [1, 1, 1, 1], queries = [[1, 3], [0, 2], [1, 3], [1, 2]] Output: 2 ``` Index 0 is covered only by `[0, 2]` and index 3 only by the two `[1, 3]` queries, so `[0, 2]` and one `[1, 3]` must stay. Together they cover all four indices, so the other two queries can be removed. ```text Input: nums = [1, 2, 3, 4], queries = [[0, 3]] Output: -1 ``` Index 1 needs two decrements, but only one query covers it, so no choice of queries can zero the array.

Overview: Given an array and a list of range queries that can each lower every index in their range by at most one, count how many queries can be dropped while the array can still be reduced to all zeros. It tests greedy choices over intervals and an efficient sweep for inputs of up to 100,000 elements.

Zynga logo
Zynga
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums of length n and a 2D array queries where queries[i] = [l_i, r_i].

Each query lets you decrement the value at every index in the range [l_i, r_i] of nums by at most 1. The amount (0 or 1) is chosen independently for each index in the range.

A zero array is an array whose elements are all 0.

Return the maximum number of queries that can be removed from queries so that nums can still be converted to a zero array using the remaining queries. If nums cannot be converted to a zero array even with every query, return -1.

Function Signature

def max_removal(nums: list[int], queries: list[list[int]]) -> int:

Rules

  • Each kept query is applied once. For every index in [l, r] (both ends inclusive) it decrements that index by either 0 or 1, chosen independently per index.
  • The order in which the kept queries are applied does not matter.
  • The function returns only the number of removed queries, so the expected output is a single integer.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= nums[i] <= 10^5
  • 1 <= len(queries) <= 10^5
  • len(queries[i]) == 2
  • 0 <= l_i <= r_i < len(nums)

Examples

Input: nums = [2, 0, 2], queries = [[0, 2], [0, 2], [1, 1]]
Output: 1

Keeping both [0, 2] queries decrements indices 0 and 2 twice each, and both queries skip index 1 because it is already 0. The query [1, 1] is not needed. Removing a second query would leave at most one [0, 2], and index 0 needs two decrements.

Input: nums = [1, 1, 1, 1], queries = [[1, 3], [0, 2], [1, 3], [1, 2]]
Output: 2

Index 0 is covered only by [0, 2] and index 3 only by the two [1, 3] queries, so [0, 2] and one [1, 3] must stay. Together they cover all four indices, so the other two queries can be removed.

Input: nums = [1, 2, 3, 4], queries = [[0, 3]]
Output: -1

Index 1 needs two decrements, but only one query covers it, so no choice of queries can zero the array.