#2597

The Number of Beautiful Subsets

specialist · 865 · lc medium +31 · failed · 50.9% accepted · 1,303 likes · top 39%

Description

You are given a positive integer array nums and a positive integer k. A subset is beautiful if it contains no two elements with an absolute difference of exactly k. Two subsets are distinct when formed from different indices. Return the count of non-empty beautiful subsets.

Example 1:

Input: nums = [2,4,6], k = 2
Output: 4
Explanation: The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].

Example 2:

Input: nums = [1], k = 1
Output: 1
Explanation: The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].

Code

1
2
3