DEV Community

Cover image for 2597. The Number of Beautiful Subsets
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

1

2597. The Number of Beautiful Subsets

2597. The Number of Beautiful Subsets

Medium

You are given an array nums of positive integers and a positive integer k.

A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.

Return the number of non-empty beautiful subsets of the array nums.

A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.

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].

Example 3:

  • Input: nums = [4,2,5,9,10,3], k = 1
  • Output: 23

Constraints:

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

Solution:

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer
     */
    function beautifulSubsets($nums, $k) {
        $countBeautifulSubsets = -1;
        $countNums = array_fill(0, 1010, 0);
        $size = count($nums);
        $dfs = function($index) use (&$dfs, &$countBeautifulSubsets, &$nums, &$countNums, $size, $k) {
            if ($index >= $size) {
                ++$countBeautifulSubsets;
                return;
            }
            $dfs($index + 1);
            $isBeautifulIncrement = $nums[$index] + $k >= 1010 || $countNums[$nums[$index] + $k] == 0;
            $isBeautifulDecrement = $nums[$index] - $k < 0 || $countNums[$nums[$index] - $k] == 0;
            if ($isBeautifulIncrement && $isBeautifulDecrement) {
                ++$countNums[$nums[$index]];
                $dfs($index + 1);
                --$countNums[$nums[$index]];
            }
        };

        $dfs(0);
        return $countBeautifulSubsets;
    }
}
Enter fullscreen mode Exit fullscreen mode

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay