605. Count the Number of K-free subsets

You are given an integer array called nums containing distinct elements and an integer k.

A subset is called k-free subset, If there are no two items in a subset with an absolute difference of k. The empty set is a k-Free subset.

Return the number of k-Free subsets present in nums array.

A subset of an array is a selection of elements (possibly none) of the array.

Example 1:

Input : nums = [1, 2, 3, 4] , k = 2

Output : 9

Explanation :

The valid subsets are : {} , {1}, {2}, {3}, {4}, {1, 2}, {2, 3}, {3, 4}, {1, 4}.

Example 2:

Input : nums = [1, 2, 3, 4] , k = 1

Output : 8

Explanation :

The valid subsets are : {} , {1}, {2}, {3}, {4}, {1, 3}, {1, 4}, {2, 4}.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [1, 2, 3, 4, 5] , k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= 1000
  • 1 <= k <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long countTheNumOfKFreeSubsets(vector<int>& nums, int k) {
//your code goes here
}
};
Test Case

Input:

K
Nums