Problem · Math

Count Fancy Numbers

Learn this problem
MediumHudson River Trading logoHudson River TradingINTERNOA

Problem statement

For the original problem description, pls checkout the image source at the very bottom of this web page 🐾🐾

Imagine you’re in a magical world where certain numbers are considered "fancy." A number is fancy if, when written in base-4, it only contains the digits 0 and 1—like secret codes known only to wizards. For example, 17 is fancy because its base-4 form, 101, uses only 0s and 1s, while 18 is not, as its base-4 form, 102, contains a forbidden 2. Now, your task is to discover how many of these special, fancy numbers exist below a given number, n. But beware! n can be as big as a billion, so you need a clever method—something faster than just checking every number individually.

Function

countFancyNumbers(n: int) → int

Examples

Example 1

n = 1return = 0

There are no positive integers less than 1.

Example 2

n = 10return = 3

The fancy numbers less than 10 are {1, 4, 5}.

More Hudson River Trading problems

drafts saved locally
public int countFancyNumbers(int n) {
  // write your code here
}
n1
expected0
checking account