Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Sum Of Compatible Numbers In Range I

Count how many numbers in each query range are compatible with a given value under a bitwise condition.

Acceptance 0%
Problem Statement

You are given an array of integers and several queries. For each query, consider the subarray inside the given range and count how many numbers are compatible with a specified target value.

A number is considered compatible if it satisfies the bitwise rule described by the problem. For every query, return the number of compatible values in the requested range.

The goal is to answer all queries efficiently, rather than scanning the range from scratch every time.

Input Format

  • An integer array nums.
  • A list of queries, where each query describes a range and a target value.
  • For each query, determine how many elements in the range are compatible with the target.

Output Format

  • Return an array of integers.
  • The i-th value is the answer for the i-th query.

Constraints

  • The array may contain many elements and there may be many queries.
  • A naive per-query full scan may be too slow for the intended solution.
  • Use integer arithmetic and bitwise operations where appropriate.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3, 4, 5]
queries = [[1, 3, 2], [0, 4, 1]]

Output

[2, 3]

Explanation

This is an illustrative example. For each query, count the numbers in the given range that satisfy the compatibility rule with the target value.

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.