← All problemsSign in

3954. Sum of Compatible Numbers in Range I

LeetCodeDynamic ProgrammingBit ManipulationEnumerationOpen on judge ↗

You are given two integers n and k. A positive integer x is called compatible if it satisfies both of the following conditions: - abs(n - x) <= k - (n & x) == 0 Return the sum of all compatible integers x. Note: - Here, & denotes the bitwise AND operator. - The absolute difference between integers i and j is defined as abs(i - j). Example 1: Input: n = 2, k = 3 Output: 10 Explanation:

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start