Dig Deep into Sliding window pattern & Two pointers
two pattern approach to solve these problems\r\n\r\n560. Subarray Sum Equals K\r\n930. Binary Subarrays With Sum\r\n1248. Count Number of Nice Subarrays\r\n\r\n
two pattern approach to solve these problems\r\n\r\n[560. Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/solutions/4416842/simple-as-that-important-pattern-must-read/)\r\n[930. Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/solutions/5180407/deep-understanding-of-two-pointer-and-intuitive-problems/)\r\n[1248. Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/solutions/5175648/dig-deep-into-sliding-window-pattern-two-pointers/)\r\n\r\n\r\nSliding window and two pointers pattern\r\n1. constant window\r\n2. Longest subarray/subString with condition (variable window)\r\n3. count (Longest) the subArray with given condition\r\n4. Minimum window\r\n\r\n### 2- pattern is mostly used\r\n```\r\n\r\n// Longest Subarray/substring pattern sliding window& two pointer pattern parblem.\r\n/*\r\nThere are 4 sliding window & two pointer pattern\r\n1. Constant window\r\n2. Longest Subarray/substring pattern\r\n3. Number of subArray/SubString pattern\r\n4.Find the minimum/shortest window pattern\r\n\r\nExplanation of 2. Longest Subarray/substring pattern\r\n\r\nThis can be again catagorised into 3\r\n1.Brute\r\n2.Optimal -> which is most used also used for printing the longest subarray/substring\r\n3.Optimsed version of 2 where \'while\' is replaced by \'if\'\r\n\r\n2 template --> 2.Optimal -> which is most used also used for printing the longest subarray/substring\r\n\r\nint left=0; int right=0, max=0,n=nums.length;\r\n\r\n        while(right<n){//This line is constant always the \'right\' starts from 0->n\r\n            \r\n            if(nums[right]==0) k--; //operation "on RIGHT" is based on problem\r\n\r\n            while(k<0){//while is constant and the condition inside is based on problem\r\n                if(nums[l]==0) k++;\r\n                left++;                \r\n            }\r\n            max = Math.max(max,right-left+1);//operation done for result\r\n            right++;// finally increment right this is constant\r\n        }\r\n\r\n        return max;\r\n\r\n*/\r\n\r\n```\r\n\r\n### 3 -Pattern\r\n```java []\r\n//Pattern 3 --> count the No of subArray/subString == (no of subArray with sum k) - (no of subArray with sum k-1)\r\n\r\n//This is the general rule but this can also be solved using the below pattern\r\n\r\npublic class Solution {\r\n    public int subarraySum(int[] a, int k) {\r\n        int n = a.length; // size of the array.\r\n\r\n        Map<Integer, Integer> preSumMap = new HashMap<>();\r\n        int count = 0;\r\n        int sum = 0;\r\n\r\n        for (int i = 0; i < n; i++) {\r\n            // Calculate the prefix sum till index i:\r\n            sum += a[i];\r\n\r\n            // If the sum equals k, update the count:\r\n            if (sum == k) {\r\n                count++;\r\n            }\r\n\r\n            // Calculate the sum of the remaining part, i.e., x - k:\r\n            int rem = sum - k;\r\n\r\n            // Increment count based on the occurrences in the map:\r\n            if (preSumMap.containsKey(rem)) {\r\n                count += preSumMap.get(rem);\r\n            }\r\n\r\n            // Update the map with the current prefix sum:\r\n            preSumMap.put(sum, preSumMap.getOrDefault(sum, 0) + 1);\r\n        }\r\n\r\n        return count;\r\n    }\r\n}\r\n\r\n```\r\n```java []\r\n// 930. Binary Subarrays With Sum\r\n\r\n//Pattern 3 --> count the No of subArray/subString == (no of subArray with sum k) - (no of subArray with sum k-1)\r\n\r\n\r\n\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\n\r\npublic class Solution {\r\n    public int numSubarraysWithSum(int[] nums, int goal) {\r\n        Map<Integer, Integer> preSumMap = new HashMap<>();\r\n        int count = 0;\r\n        int sum = 0;\r\n\r\n        // Initialize map with sum 0 having one occurrence to handle subarrays that start from index 0\r\n        preSumMap.put(0, 1);\r\n\r\n        for (int num : nums) {\r\n            sum += num;\r\n\r\n            // Calculate the sum of the remaining part needed to reach the goal\r\n            int rem = sum - goal;\r\n\r\n            // Increment count based on the occurrences in the map\r\n            count += preSumMap.getOrDefault(rem, 0);\r\n\r\n            // Update the map with the "current" prefix sum\r\n            preSumMap.put(sum, preSumMap.getOrDefault(sum, 0) + 1);\r\n        }\r\n\r\n        return count;\r\n    }\r\n}\r\n\r\n```\r\n```java []\r\n\r\n//pattern 3  Approach\r\n// 930. Binary Subarrays With Sum\r\nclass Solution {\r\n\r\n    public int numSubarraysWithSum(int[] nums, int goal) {\r\n        return atMost(nums, goal) - atMost(nums, goal - 1);\r\n    }\r\n\r\n    private int atMost(int[] nums, int goal) {\r\n        if (goal < 0) return 0;\r\n        int res = 0, left = 0, n = nums.length, right = 0;\r\n        int sum = 0;\r\n        while (right < n) {\r\n            sum += nums[right];\r\n            while (sum > goal) {\r\n                sum -= nums[left];\r\n                left++;\r\n            }\r\n            res += right - left + 1;\r\n            right++;\r\n        }\r\n        return res;\r\n    }\r\n}\r\n// Hope that is clear\r\n```\r\n\r\n# 1248. Count Number of Nice Subarrays\r\n\r\n```javascript []\r\n//Pattern 3 --> count the No of subArray/subString\r\n//Pattern 3 --> count the No of subArray/subString == (no of subArray with sum k) - (no of subArray with sum k-1)\r\n\r\nclass Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        return atMost(nums, k) - atMost(nums, k - 1);\r\n    }\r\n    private int atMost(int[] nums, int goal) {\r\n        if (goal < 0) return 0;\r\n        int res = 0, left = 0, n = nums.length, right = 0;\r\n        int sum = 0;\r\n        while (right < n) {\r\n            sum += nums[right]%2;\r\n            while (sum > goal) {\r\n                sum -= nums[left]%2;\r\n                left++;\r\n            }\r\n            res += right - left + 1;\r\n            right++;\r\n        }\r\n        return res;\r\n    }\r\n}\r\n```\r\n```java []\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\n\r\npublic class Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        Map<Integer, Integer> preSumMap = new HashMap<>();\r\n        int count = 0;\r\n        int sum = 0;\r\n\r\n        // Initialize map with sum 0 having one occurrence to handle subarrays that start from index 0\r\n        preSumMap.put(0, 1);\r\n\r\n        for (int num : nums) {\r\n            // Increment sum if the number is odd\r\n            sum += num % 2;\r\n\r\n            // Calculate the sum of the remaining part needed to reach the goal k\r\n            int rem = sum - k;\r\n\r\n            // Increment count based on the occurrences in the map\r\n            count += preSumMap.getOrDefault(rem, 0);\r\n\r\n            // Update the map with the "current" prefix sum\r\n            preSumMap.put(sum, preSumMap.getOrDefault(sum, 0) + 1);\r\n        }\r\n\r\n        return count;\r\n    }\r\n}\r\n\r\n```\r\n\r\n
['Java']