The Number of Beautiful Subsets - Problem

Imagine you're organizing a special art exhibition where certain paintings cannot be displayed together due to their contrasting styles. You have an array nums of positive integers representing different artwork IDs, and a positive integer k representing the "conflict threshold".

A subset of artworks is considered "beautiful" if it doesn't contain any two pieces whose ID numbers have an absolute difference equal to k. For example, if k=3, then artworks with IDs 5 and 8 cannot be in the same beautiful subset since |5-8| = 3.

Your goal is to count all possible non-empty beautiful subsets that can be formed from the given array. Remember, each subset represents a different way to curate your exhibition!

Example: With nums = [2,4,6] and k = 2, the beautiful subsets are: [2], [4], [6], [2,6] (note that [2,4] and [4,6] are not beautiful since their differences equal k=2).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,4,6], k = 2
โ€บ Output: 4
๐Ÿ’ก Note: The beautiful subsets are: [2], [4], [6], [2,6]. Note that [2,4], [4,6], and [2,4,6] are not beautiful because they contain pairs with difference k=2.
example_2.py โ€” Single Element
$ Input: nums = [1], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one non-empty subset exists: [1], which is trivially beautiful since it contains no pairs.
example_3.py โ€” No Valid Pairs
$ Input: nums = [4,2,5,9,10,3], k = 1
โ€บ Output: 23
๐Ÿ’ก Note: Many consecutive numbers exist (2,3,4,5), creating conflicts. The algorithm must carefully count only non-conflicting combinations.

Visualization

Tap to expand
Art Gallery Curation: Beautiful SubsetsAvailable Paintings: [2, 4, 6] | Conflict Rule: k = 2Exhibition 1[2]โœ“ BeautifulExhibition 2[4]โœ“ BeautifulExhibition 3[6]โœ“ BeautifulExhibition 4[2, 6]โœ“ BeautifulInvalid[2, 4]โœ— |2-4| = 2Invalid[4, 6]โœ— |4-6| = 2Invalid[2, 4, 6]โœ— Multiple conflictsResult: 4 Beautiful Exhibitions
Understanding the Visualization
1
Initial Setup
You have a collection of paintings with catalog numbers [2,4,6] and conflict rule k=2
2
Check Conflicts
Paintings with numbers differing by exactly 2 cannot be displayed together (2 conflicts with 4, 4 conflicts with 6)
3
Valid Exhibitions
Count all possible non-empty exhibitions: [2], [4], [6], [2,6] = 4 total
Key Takeaway
๐ŸŽฏ Key Insight: Use backtracking with conflict checking to efficiently explore only valid combinations, avoiding the exponential cost of generating all possible subsets.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

In worst case still explores all subsets, but with early pruning it's much faster in practice

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for recursion stack and frequency map

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 20
  • 1 โ‰ค nums[i], k โ‰ค 1000
  • All elements in nums are positive integers
  • The array may contain duplicate values
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.5K Views
Medium Frequency
~25 min Avg. Time
867 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen