Check whether n is a bleak number (no x where x plus its set-bit count equals n)

Quick Overview

Decide whether a positive integer n is bleak, meaning no positive x makes x plus the number of set bits in x equal to n. Tests reasoning about how large a set-bit count can be and turning that bound into a short, exact search.

Check whether n is a bleak number (no x where x plus its set-bit count equals n)

Company: Sap

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a positive integer `n`, determine whether it is a bleak number. A number `n` is called bleak if there is no positive integer `x` such that `x + countSetBits(x) == n`, where `countSetBits(x)` is the number of set bits (1s) in the binary representation of `x`. Return `True` if `n` is bleak and `False` otherwise. ### Function Signature ```python def is_bleak(n: int) -> bool: ``` ### Constraints - `1 <= n <= 10^9` ### Examples ```text Input: n = 3 Output: False ``` `x = 2` gives `2 + countSetBits(2) = 2 + 1 = 3`. Since such an `x` exists, 3 is not bleak. ```text Input: n = 4 Output: True ``` Any valid `x` would be smaller than 4. `x = 1` gives 2, `x = 2` gives 3 and `x = 3` gives 5, so no `x` reaches 4 and 4 is bleak. ```text Input: n = 1 Output: True ``` The smallest positive `x` is 1, and `1 + 1 = 2` is already larger than 1.

Overview: Decide whether a positive integer n is bleak, meaning no positive x makes x plus the number of set bits in x equal to n. Tests reasoning about how large a set-bit count can be and turning that bound into a short, exact search.

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

Given a positive integer n, determine whether it is a bleak number.

A number n is called bleak if there is no positive integer x such that x + countSetBits(x) == n, where countSetBits(x) is the number of set bits (1s) in the binary representation of x.

Return True if n is bleak and False otherwise.

Function Signature

def is_bleak(n: int) -> bool:

Constraints

  • 1 <= n <= 10^9

Examples

Input: n = 3
Output: False

x = 2 gives 2 + countSetBits(2) = 2 + 1 = 3. Since such an x exists, 3 is not bleak.

Input: n = 4
Output: True

Any valid x would be smaller than 4. x = 1 gives 2, x = 2 gives 3 and x = 3 gives 5, so no x reaches 4 and 4 is bleak.

Input: n = 1
Output: True

The smallest positive x is 1, and 1 + 1 = 2 is already larger than 1.