Skip to the code
cf.
+25 XP

#677 Every Narrower Permission Set

medium

An admin panel lets a user share access with someone else, but only with permissions they have themselves. The user's permissions are a bit mask mask.

Write narrowerSets(mask) that returns every mask that only uses bits from mask (its submasks, including mask itself and 0), sorted from largest to smallest.

Example 1

Input: mask = 5
Output: [5,4,1,0]

5 is 101. Its submasks are 101, 100, 001, and 000.

Example 2

Input: mask = 0
Output: [0]

With no permissions, the only option is none.

Constraints

  • 0 <= mask <= 2^30
  • mask has at most 10 bits set.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).