hardBacktrackingPure DSA~30 min
Count Subsets Avoiding a Fixed Difference
From a set of configuration values, count the non-empty subsets in which no two chosen values differ by exactly k — a constraint used to avoid conflicting paired settings.
Problem
Given an array nums of positive integers and a positive integer k, return the number of non-empty 'beautiful' subsets: subsets in which no two elements have an absolute difference equal to k.
Input
An array nums of positive integers and a positive integer k.
Output
An integer: the number of non-empty beautiful subsets.
Constraints
- 1 <= nums.length <= 20
- 1 <= nums[i], k <= 1000
Examples
Example 1
Input
nums = [2,4,6], k = 2
Output
4
{2},{4},{6},{2,6} are beautiful; {2,4},{4,6},{2,4,6} are not.
Example 2
Input
nums = [1], k = 1
Output
1
The single subset {1} is beautiful.