Only Backtracking 🚀 || concise 🎗️|| newbie friendly 🔥|| 3 lang 🎉 || simple explanation 🧩
Screenshot \uD83C\uDF89\n\n\n\n\n---\n\n\n# Intuition \uD83E\uDD14\n Describe your first thoughts on how to solve this problem. \n    As we see this is the same
# Screenshot \uD83C\uDF89\n\n![2597 js.png](https://assets.leetcode.com/users/images/d318b96d-d489-46b6-81dd-cf2e6e7b9191_1716427969.21775.png)\n\n\n---\n\n\n# Intuition \uD83E\uDD14\n<!-- Describe your first thoughts on how to solve this problem. -->\n    As we see this is the same problem as we solved yesterday \n\n    and day before yesterday.\n\n    This perticular problem is saying --> \n\n    1. We need to give the count of subArray of a given array\n\n    2. by considering the condition that \n    \n    3. no such two element present in our sub array \n\n    4. whose absolute deffrence is equal to the given variable (k)\n\n-------`Considering you all now understand the problem `-------\n---\n\n# Approach \uD83D\uDD25  `Let\'s discuss the approach`\n<!-- Describe your approach to solving the problem. -->\n    We are going to use our basic fanda of backtracking only ...\n\n    Yes yes only basic fanda \uD83E\uDD73\n\n\n-----------------------` What is BackTracking  --> `----------------\n\n    Every element have 2 choice either it will come with us\n    Or it will go alone in the tree \n    Or you can say we have to choose one path if it gets wrong \n    we can backtrack to any where\n\n    - This contain only 4 steps ...\n\n    1. Add the value    (choose one path)\n    2. Go to next step  (go forword on same path)\n    3. remove the value (take a one step back)\n    4. Go to next step  (choose another path)\n\n![2597 que.png](https://assets.leetcode.com/users/images/93c750a2-5983-4d31-a25d-b8afc8c3fa5e_1716429592.4974186.png)\n\n\n`We will use only backtracking but with modification \uD83D\uDE80\uD83D\uDE80\uD83D\uDE80`\n\n    We will add our value only if it will follow our given condition\n\n    Else we will not gonna add that \n    \n\n# UnderStand with code \uD83D\uDCAF\n    We will use Only backtracking Approach\n\n    - In which we have 5 works to do\n\n    1. Write the base case and make count variable with part array\n    2. Add the value after checking the condition with our isSafe fun\n    3. if safe then take first step (add) then one step ahead\n    4. Then backtrack our self by one step ( remove the last added)\n    5. call for the next step because every single step (element)\n        will be our part except null array (Given)\n\n\n`Step 1.` Make **Global count variable** and **part** and make **our base case**\n\n    Base case will hit if we reach to the last of index from which\n\n    no forword step gonna remain ...\n\n    Base case -->\n        IF base case hit our count variable inc by one \n\n```javascript []\n\n// Global count varibale\nlet count = 0;\n\nvar beautifulSubsets = function (nums, k) {\n    count = 0;\n    // Part array\n    part = [];\n\n    var subSetArr = function (i) {\n        //base case\n        if (i >= nums.length) {\n            count++;\n            return;\n        }\n    }\n\n    subSetArr(0);\n    return count - 1;\n};\n```\n```JAVA []\n\n    // Global count varibale\n    public int count = 0;\n\n    public void subSetArr(int[] nums, int i, List<Integer> part, int k) {\n        if (i >= nums.length) {\n            //base case\n            count++;\n            return;\n        }\n    }\n\n    public int beautifulSubsets(int[] nums, int k) {\n        count = 0;\n        // Part array directly pass \n        subSetArr(nums, 0, new ArrayList<>(), k);\n        \n    }\n}\n```\n```python []\n    # Global count varibale\n    count = 0\n    def beautifulSubsets(self, nums: List[int], k: int) -> int:\n        \n        self.count = 0\n        # Part array\n        part = []\n\n        def subSetArr(i):\n            #base case\n            if i >= len(nums):\n                self.count = self.count + 1\n                return\n\n```\n\n---\n\n\n`Step 2 - 3 - 4` **Add if isSafe** and **implement isSafe** and **Go for the next step**\n\n    isSafe function EXPLAIN -->\n\n    1. This function will check is upcoming number from nums array\n    2. will be our part of our part array or not\n    3. By sub next number to existing element of part array\n    4. If it comes equal to GIVEN (k) it will return false els true\n\n```javascript []\nvar isSafe = function (nums, part, j, k) {\n    for (let i = 0; i < part.length; i++) {\n        if (Math.abs(nums[j] - part[i]) == k) {\n            return false;\n        }\n    }\n\n    return true;\n}\n\nvar beautifulSubsets = function (nums, k) {\n    // Backtracking if safe to add \n    if (isSafe(nums, part, i, k)) {\n        part.push(nums[i]);\n        subSetArr(i + 1);\n        part.pop();\n    }\n    subSetArr(i + 1);\n\n};\n```\n```JAVA []\n    public boolean isSafe(int nums[], List<Integer> part, int j, int k) {\n        for (int i = 0; i < part.size(); i++) {\n            if (Math.abs(nums[j] - part.get(i)) == k) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    public void subSetArr(int[] nums, int i, List<Integer> part, int k) {\n        // Backtracking if safe to add \n        if (isSafe(nums, part, i, k)) {\n            part.add(nums[i]);\n            subSetArr(nums, i + 1, part, k);\n            part.remove(part.size() - 1);\n        }\n        subSetArr(nums, i + 1, part, k);\n    }\n}\n```\n```python []\n    def is_safe(self, nums, part, j, k):\n    for n in part:\n        if abs(nums[j] - n) == k:\n            return False\n    return True\n\n\n    def beautifulSubsets(self, nums: List[int], k: int) -> int:\n\n        def subSetArr(i):\n            # Backtracking if safe to add \n            if self.is_safe(nums, part, i, k):\n                part.append(nums[i])\n                subSetArr(i + 1)\n                part.pop()\n\n            subSetArr(i + 1)\n            \n```\n\n---\n\n\n`Step 5.` **Call our function** and **return the count**\n \n```javascript []\n\n    subSetArr(0);\n    return count - 1;\n```\n```JAVA []\n    subSetArr(nums, 0, new ArrayList<>(), k);\n    return count - 1;\n```\n```python []\n    subSetArr(0)\n    return self.count - 1     \n```\n\n---\n\n# Complexity \uD83D\uDE80\n- Time complexity: $$O(n * 2^n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n# Code \uD83D\uDC96\n\n```javascript []\nvar isSafe = function (nums, part, j, k) {\n    for (let i = 0; i < part.length; i++) {\n        if (Math.abs(nums[j] - part[i]) == k) {\n            return false;\n        }\n    }\n\n    return true;\n}\nlet count = 0;\n\nvar beautifulSubsets = function (nums, k) {\n    count = 0;\n    part = [];\n\n    var subSetArr = function (i) {\n        if (i >= nums.length) {\n            count++;\n            return;\n        }\n        if (isSafe(nums, part, i, k)) {\n            part.push(nums[i]);\n            subSetArr(i + 1);\n            part.pop();\n        }\n        subSetArr(i + 1);\n    }\n\n    subSetArr(0);\n    return count - 1;\n};\n```\n```JAVA []\nclass Solution {\n\n    public boolean isSafe(int nums[], List<Integer> part, int j, int k) {\n        for (int i = 0; i < part.size(); i++) {\n            if (Math.abs(nums[j] - part.get(i)) == k) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    public int count = 0;\n\n    public void subSetArr(int[] nums, int i, List<Integer> part, int k) {\n        if (i >= nums.length) {\n            count++;\n            return;\n        }\n        if (isSafe(nums, part, i, k)) {\n            part.add(nums[i]);\n            subSetArr(nums, i + 1, part, k);\n            part.remove(part.size() - 1);\n        }\n        subSetArr(nums, i + 1, part, k);\n    }\n\n    public int beautifulSubsets(int[] nums, int k) {\n        count = 0;\n\n        subSetArr(nums, 0, new ArrayList<>(), k);\n        return count - 1;\n    }\n}\n```\n```python []\nclass Solution:\n\n    count = 0\n\n    def is_safe(self, nums, part, j, k):\n        for n in part:\n            if abs(nums[j] - n) == k:\n                return False\n        return True\n\n    def beautifulSubsets(self, nums: List[int], k: int) -> int:\n\n        self.count = 0\n        part = []\n\n        def subSetArr(i):\n            if i >= len(nums):\n                self.count = self.count + 1\n                return\n\n            if self.is_safe(nums, part, i, k):\n                part.append(nums[i])\n                subSetArr(i + 1)\n                part.pop()\n\n            subSetArr(i + 1)\n\n        subSetArr(0)\n        return self.count - 1\n\n```\n``` JAVA []\n// Solution with interface...\nclass Solution {\n\n    public interface Subsets {\n        void subSetArr(int i);\n    }\n\n    public boolean isSafe(int nums[], List<Integer> part, int j, int k){\n        for (int i = 0; i < part.size(); i++) {\n            if(Math.abs(nums[j] - part.get(i)) == k) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    public int count = 0;\n\n    public int beautifulSubsets(int[] nums, int k) {\n        count = 0;\n        List<Integer> part = new ArrayList<>();\n\n        Subsets sol = new Subsets() {\n            public void subSetArr(int i) {\n                if (i >= nums.length) {\n                    count++;\n                    return;\n                }\n                if(isSafe(nums, part, i, k)) {\n                    part.add(nums[i]);\n                    subSetArr(i + 1);\n                    part.remove(part.size() - 1);\n                }\n                subSetArr(i + 1);\n            }\n        };\n\n        sol.subSetArr(0);\n        return count - 1;\n    }\n}\n```\n\n
['Array', 'Dynamic Programming', 'Backtracking', 'Sorting', 'Java', 'Python3', 'JavaScript']