question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count-number-of-nice-subarrays | [C++][O(N) Prefix Sum] | con-prefix-sum-by-emli-asgz | \nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> prefixSum(nums.size());\n \n for(int i = 0 | emli | NORMAL | 2019-11-03T04:09:13.103668+00:00 | 2019-11-03T04:10:11.114978+00:00 | 1,681 | false | ```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> prefixSum(nums.size());\n \n for(int i = 0; i < nums.size(); i++){\n if (nums[i] % 2 == 1){\n prefixSum[i] = 1;\n }\n }\n \n for(int i = 1; i... | 12 | 1 | [] | 2 |
count-number-of-nice-subarrays | Python - 2 Sliding Window Solutions | python-2-sliding-window-solutions-by-its-pd9z | \n# APPROACH 1- NOT O(1) SPACE\n\nThere are some things to take care of in this problem. We cannot simply consider one window to be valid and just increment cou | itsarvindhere | NORMAL | 2022-09-14T15:35:31.467039+00:00 | 2022-09-16T06:09:39.908099+00:00 | 1,526 | false | \n# **APPROACH 1- NOT O(1) SPACE**\n\nThere are some things to take care of in this problem. We cannot simply consider one window to be valid and just increment count by 1.\n\nConsider this ->\n\n\t\tnums = [2,2,2,1,2,2,1,2,2,2] and k = 2\n\t\t\t\t0 1 2 3 4 5 6 7 8 9\n\t\t\nSo, if we use the sliding window approach, th... | 11 | 0 | ['Sliding Window', 'Python'] | 3 |
count-number-of-nice-subarrays | 🔥2 Approaches || ⚡Easiest & Best Code in C++ with explanation ✅ | 2-approaches-easiest-best-code-in-c-with-jxz6 | Please Upvote if u liked my Solution\uD83D\uDE42\n# Using PrefixSum with HashMap(O(n) SC):-\n\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& | aDish_21 | NORMAL | 2022-09-12T16:32:22.742499+00:00 | 2024-06-25T14:32:20.952741+00:00 | 1,169 | false | ## **Please Upvote if u liked my Solution**\uD83D\uDE42\n# Using PrefixSum with HashMap(O(n) SC):-\n```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int size=nums.size(),i=0,j=0;\n for(auto &it:nums){\n if((it & 1)==0)\n it=0;\n e... | 11 | 0 | ['Array', 'Hash Table', 'Math', 'C', 'Sliding Window', 'Prefix Sum'] | 0 |
count-number-of-nice-subarrays | JAVA solution | Current problem transformed to Problem 560 | HashMap | java-solution-current-problem-transforme-hjv3 | Please Upvote !!! (\u25E0\u203F\u25E0)\nWe traverse the whole array and put 0 in place of even elements and 1 in place of odd elements. \nSo instead of finding | sourin_bruh | NORMAL | 2022-09-24T18:09:51.437156+00:00 | 2022-09-24T18:09:51.437198+00:00 | 1,084 | false | #### *Please Upvote !!!* **(\u25E0\u203F\u25E0)**\nWe traverse the whole array and put 0 in place of even elements and 1 in place of odd elements. \nSo instead of finding k odd numbers, we now find subarrays whose sum will be equal to k (Because the odd numbers are all 1 and k odd numbers will give a sum of k now).\n\n... | 10 | 0 | ['Java'] | 1 |
count-number-of-nice-subarrays | beats 100% | beats-100-by-vigneshreddy06-okea | Intuition\nC++\n\nPython3\n\n\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nSimply Use the queue to store the indexes of odd el | vigneshreddy06 | NORMAL | 2024-06-22T11:25:26.046829+00:00 | 2024-06-22T11:25:26.046853+00:00 | 186 | false | # Intuition\nC++\n\nPython3\n\n\n\n<!-- Describe your first thoughts on how to solve this prob... | 9 | 0 | ['C++', 'Python3'] | 3 |
count-number-of-nice-subarrays | Prefix sum+Sliding window vs at most k odds||35ms Beats 99.99% | prefix-sumsliding-window-vs-at-most-k-od-38ou | Intuition\n Describe your first thoughts on how to solve this problem. \nThe concept is to use sliding window. With the help of prefix sum, it made an acceptibl | anwendeng | NORMAL | 2024-06-22T01:33:39.888292+00:00 | 2024-06-22T02:08:18.435189+00:00 | 618 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe concept is to use sliding window. With the help of prefix sum, it made an acceptible solution.\n\n2nd approach usse at most k odds argument which is applied to solve the hard question [992. Subarrays with K Different Integers](https:/... | 9 | 3 | ['Array', 'Sliding Window', 'Prefix Sum', 'C++'] | 2 |
count-number-of-nice-subarrays | ✅Beats 100% | Variable Size Sliding Window | Easy Explaination | beats-100-variable-size-sliding-window-e-ourv | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nThe task is to find the number of subarrays with exactly k odd numbers. To achieve | Fidio99 | NORMAL | 2024-06-22T00:06:38.952447+00:00 | 2024-06-22T00:11:16.283450+00:00 | 2,299 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThe task is to find the number of subarrays with exactly k odd numbers. To achieve this, we use a helper function to count the number of subarrays with at most k odd numbers. The difference between the number of subarrays with at most... | 9 | 0 | ['Array', 'Hash Table', 'Math', 'Sliding Window', 'Python', 'C++', 'Java'] | 5 |
count-number-of-nice-subarrays | Hash Maps to Find Nice Subarrays. | hash-maps-to-find-nice-subarrays-by-iama-vcgv | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves counting the number of subarrays that contain exactly \uD835\uDC58 | iamanrajput | NORMAL | 2024-06-22T14:22:28.321791+00:00 | 2024-06-22T14:22:28.321813+00:00 | 274 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves counting the number of subarrays that contain exactly \uD835\uDC58.\n\nk odd numbers. Initially, this may seem complex due to the need to evaluate every possible subarray. However, leveraging prefix sums and hash maps... | 8 | 0 | ['Array', 'Hash Table', 'Math', 'Sliding Window', 'C++', 'Java'] | 0 |
count-number-of-nice-subarrays | Python easy prefix sum solution | python-easy-prefix-sum-solution-by-berth-fy0y | Hi guys,\n\nHere is a simple solution using the prefix sum of an array where each element is equal to 1 if nums[i]%2==1 and 0 if nums[i]%2==0. \n\nWe initialize | Berthouille | NORMAL | 2022-09-20T10:10:19.569672+00:00 | 2022-09-20T10:10:19.569715+00:00 | 1,492 | false | Hi guys,\n\nHere is a simple solution using the prefix sum of an array where each element is equal to 1 if nums[i]%2==1 and 0 if nums[i]%2==0. \n\nWe initialize the dictionary so that we take into account the arrays with k odd numbers starting at index 0.\n\n\t\tpref=list(accumulate([0 if num%2==0 else 1 for num in num... | 8 | 0 | ['Prefix Sum', 'Python'] | 4 |
count-number-of-nice-subarrays | WELL-EXPLAINED🧠 EASIEST SOLUTION🧐 SLIDING WINDOW🪟TWO-POINTERS | well-explained-easiest-solution-sliding-q4pce | UPVOTE IF YOU FIND THE SOLUTION HELPFUL\uD83D\uDCA1\n\n# Intuition\nThe problem requires us to find the number of continuous subarrays with exactly k odd number | __aayush_01 | NORMAL | 2024-06-22T08:56:27.411969+00:00 | 2024-06-22T08:56:44.036557+00:00 | 610 | false | # ***UPVOTE IF YOU FIND THE SOLUTION HELPFUL\uD83D\uDCA1***\n\n# Intuition\nThe problem requires us to find the number of continuous subarrays with exactly k odd numbers. This can be efficiently solved using a `sliding window` with `two-pointer` approach.\n\n\n# Approach\n1. **Initialization:**\n - Initialize two poi... | 7 | 0 | ['Array', 'Hash Table', 'Math', 'Two Pointers', 'Sliding Window', 'Python', 'C++', 'Java', 'JavaScript'] | 0 |
count-number-of-nice-subarrays | 🗓️ Daily LeetCoding Challenge Day 185|| 🔥 JAVA SOL | daily-leetcoding-challenge-day-185-java-yli6e | \n# Code\n\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int n = nums.length;\n int[] count = new int[n + 1];\n | DoaaOsamaK | NORMAL | 2024-06-22T03:04:12.672063+00:00 | 2024-06-22T03:04:12.672092+00:00 | 1,344 | false | \n# Code\n```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int n = nums.length;\n int[] count = new int[n + 1];\n count[0] = 1;\n int result = 0, oddCount = 0;\n for (int num : nums) {\n oddCount += num & 1;\n if (oddCount - k >= 0) ... | 7 | 0 | ['Java'] | 1 |
count-number-of-nice-subarrays | Python3 Solution | python3-solution-by-motaharozzaman1996-dor5 | \n\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n n=len(nums)\n i=0\n count=0\n ans=0\n | Motaharozzaman1996 | NORMAL | 2024-06-22T00:46:48.612302+00:00 | 2024-06-22T00:46:48.612321+00:00 | 1,287 | false | \n```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n n=len(nums)\n i=0\n count=0\n ans=0\n for j in range(n):\n if nums[j] & 1:\n k-=1\n count=0\n while k==0:\n k+= nums[i] & 1\n ... | 7 | 0 | ['Python', 'Python3'] | 3 |
count-number-of-nice-subarrays | C solution using "at least k" approach | c-solution-using-at-least-k-approach-by-zen14 | Algorithm\nMaintain two pointers i and j. If the number of odd numbers between i and j inclusive (i >= j) is exactly k then the number of subarrays with "at lea | psionl0 | NORMAL | 2024-06-22T00:22:10.059238+00:00 | 2024-06-22T02:32:49.809123+00:00 | 260 | false | # Algorithm\nMaintain two pointers `i` and `j`. If the number of odd numbers between `i` and `j` inclusive (`i >= j`) is exactly `k` then the number of subarrays with "at least `k`" odd numbers from `j` onwards will be given as `numsSize - i`.\n\nReturn the difference between the number of subarrays with "at least `k`"... | 7 | 0 | ['C'] | 1 |
count-number-of-nice-subarrays | Sliding Window,PrefixSum Approach | sliding-windowprefixsum-approach-by-ganj-set4 | \nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n prefixsum=0\n dic=defaultdict(int)\n dic[0]=1\n | GANJINAVEEN | NORMAL | 2023-08-21T08:51:46.348874+00:00 | 2023-08-21T08:51:46.348907+00:00 | 1,115 | false | ```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n prefixsum=0\n dic=defaultdict(int)\n dic[0]=1\n ans=0\n for i in nums:\n if i%2==1:\n prefixsum+=1\n ans+=dic[prefixsum-k]\n dic[prefixsum]+=1\n ... | 7 | 0 | ['Python3'] | 2 |
count-number-of-nice-subarrays | Easy solution | easy-solution-by-wtfcoder-4djp | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nConsider the window with k prime numbers, the number of evens on either side are va | wtfcoder | NORMAL | 2023-07-02T10:14:27.922307+00:00 | 2023-07-02T10:14:27.922328+00:00 | 2,152 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nConsider the window with k prime numbers, the number of evens on either side are variables, Maintain evens on left and right of every odd numbers, ans is sum of (evens on left of first number + 1 * evens on right of last number + 1). ... | 7 | 0 | ['Sliding Window', 'C++'] | 1 |
count-number-of-nice-subarrays | Easy sliding window solution with O(N) time complexity | easy-sliding-window-solution-with-on-tim-9tz6 | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nThis Problem is talking about subarrays so the first thought that should come in yo | shivansh_p7 | NORMAL | 2023-03-08T03:39:16.505195+00:00 | 2023-03-08T03:39:16.505241+00:00 | 1,732 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThis Problem is talking about subarrays so the first thought that should come in your mind -> sliding window\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n1. first take two pointer on index 0\r\n2. now th... | 7 | 0 | ['JavaScript'] | 0 |
count-number-of-nice-subarrays | JAVA Easy Consise O(n) | java-easy-consise-on-by-bharat194-z0ji | \nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n return count(nums,k) - count(nums,k-1); \n\t\t// number of subarrays with k o | bharat194 | NORMAL | 2022-04-05T06:18:26.887907+00:00 | 2022-04-06T04:56:53.048073+00:00 | 671 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n return count(nums,k) - count(nums,k-1); \n\t\t// number of subarrays with k odd integers = number of subarrays with atmost k odd integer - number of subarrays with atmost k-1 odd integers\n }\n public int count(int[] nums,int ... | 7 | 0 | ['Java'] | 2 |
count-number-of-nice-subarrays | Dont fall into the Two pointer/Sliding Window trap | dont-fall-into-the-two-pointersliding-wi-oesk | This question is a direct variation of running subarray sum. This approach is simple and highly intuitive compared to sliding window and 2 pointers.\n\nThe map | siddhantchimankar | NORMAL | 2021-10-22T07:56:02.502794+00:00 | 2021-11-30T05:35:42.419402+00:00 | 561 | false | This question is a direct variation of running subarray sum. This approach is simple and highly intuitive compared to sliding window and 2 pointers.\n\nThe map is storing {key, val} == {num of odds in subarray, num of subarray with these many odds that have been encountered till now}\n\nKey insight --> The map only sto... | 7 | 0 | [] | 1 |
count-number-of-nice-subarrays | [C++] Sliding Window | c-sliding-window-by-thegateway-328v | Exactly = atMostK(k) - atMostK(k-1)\n\nint atMostK(vector<int>& a, int k) {\n int i=0,count = 0,res =0;\n for(int j=0;j<a.size();j++){\n | theGateway | NORMAL | 2021-09-26T17:23:21.648287+00:00 | 2021-09-26T17:23:53.419324+00:00 | 1,645 | false | **Exactly = atMostK(k) - atMostK(k-1)**\n```\nint atMostK(vector<int>& a, int k) {\n int i=0,count = 0,res =0;\n for(int j=0;j<a.size();j++){\n if(a[j]%2==1) count++;\n if(count > k){\n while(count>k){\n if(a[i]%2==1) count--;\n i+... | 7 | 0 | ['C', 'Sliding Window', 'C++'] | 4 |
count-number-of-nice-subarrays | [C++] Two Pointer O(n) Solution | c-two-pointer-on-solution-by-khushboogup-ia88 | For finding a subarray with exactly k odd elements = count(subarray with at most k odd elements) - count(subarray with at most k-1 odd elements). This solution | khushboogupta13 | NORMAL | 2021-07-20T11:47:18.754762+00:00 | 2021-07-20T11:47:18.754801+00:00 | 495 | false | For finding a subarray with **exactly k odd elements = count(subarray with at most k odd elements) - count(subarray with at most k-1 odd elements)**. This solution consists of passing through the arrays twice (once for k odd elements and once for k-1 odd elements)\n\n```\nclass Solution {\n \n int atMostK(vector<... | 7 | 0 | [] | 3 |
count-number-of-nice-subarrays | Solution using hashmap and some math | solution-using-hashmap-and-some-math-by-uqnft | Intuition\n Describe your first thoughts on how to solve this problem. \nInitially thought sliding window but it is a complex one. After a bit of thinking I cam | mahdir246 | NORMAL | 2024-06-22T02:10:35.209132+00:00 | 2024-06-22T02:10:35.209159+00:00 | 402 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInitially thought sliding window but it is a complex one. After a bit of thinking I came up with this mathy approach which is hopefully easier to understand.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nConver... | 6 | 0 | ['Python3'] | 0 |
count-number-of-nice-subarrays | (C++) A Different Way To Solve This Problem. | c-a-different-way-to-solve-this-problem-x2kty | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | cookie_33 | NORMAL | 2024-06-22T01:36:46.182819+00:00 | 2024-06-22T01:43:48.441473+00:00 | 2,208 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(K)\n<!-- Add your space complexity here, e.g. $... | 6 | 0 | ['Queue', 'Sliding Window', 'C++'] | 3 |
count-number-of-nice-subarrays | Editorial solution... #Perfectly Beats all 100% | editorial-solution-perfectly-beats-all-1-h0di | \n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) - | Aim_High_212 | NORMAL | 2024-06-22T01:28:39.100394+00:00 | 2024-06-22T01:28:39.100419+00:00 | 103 | false | \n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n curr_sum = 0\n subarrays = 0\n prefix_sum = {curr_sum: 1}\n\n for i in range(len(nums)):\n curr_sum += nums[i... | 6 | 0 | ['Array', 'Hash Table', 'Math', 'C', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3'] | 0 |
count-number-of-nice-subarrays | C++ O(N) Solution (Beats 100% of Users) | c-on-solution-beats-100-of-users-by-cook-szlu | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | cookie_33 | NORMAL | 2024-06-22T01:24:31.579665+00:00 | 2024-06-22T01:24:31.579685+00:00 | 1,440 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $... | 6 | 0 | ['Sliding Window', 'C++'] | 7 |
count-number-of-nice-subarrays | ✅ Easy C++ Solution | easy-c-solution-by-moheat-af98 | Code\n\nclass Solution {\npublic:\n int subarray(vector<int> nums, int k)\n {\n if(k < 0)\n {\n return 0;\n }\n int | moheat | NORMAL | 2024-06-22T00:16:17.141171+00:00 | 2024-06-22T00:16:17.141194+00:00 | 2,112 | false | # Code\n```\nclass Solution {\npublic:\n int subarray(vector<int> nums, int k)\n {\n if(k < 0)\n {\n return 0;\n }\n int n = nums.size();\n\n int l = 0;\n int r = 0;\n int odd = 0;\n int count = 0;\n\n while(r < n)\n {\n i... | 6 | 0 | ['C++'] | 4 |
count-number-of-nice-subarrays | Very Easy C++ Solution in O(n) - beats 99.48% submissions | very-easy-c-solution-in-on-beats-9948-su-zo94 | To achieve good time complexity we need to use bit munpulation for checking odd number\n\nTime Complexity: O(n)\nSpace Complexity: O(1)\n\n#pragma GCC optimize( | vatsalkesarwani | NORMAL | 2021-06-19T22:11:35.952424+00:00 | 2021-06-19T22:11:35.952454+00:00 | 583 | false | To achieve good time complexity we need to use bit munpulation for checking odd number\n\nTime Complexity: O(n)\nSpace Complexity: O(1)\n```\n#pragma GCC optimize("Ofast") \n#pragma GCC target("avx,avx2,fma") \nstatic auto _ = [] ()\n{ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();\n\... | 6 | 2 | ['Two Pointers', 'C++'] | 0 |
count-number-of-nice-subarrays | Easy || Commented || O(N) Time and O(1) Space || Window Sliding | easy-commented-on-time-and-o1-space-wind-0yyd | \nclass Solution {\n public:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int l = 0, r = 0, n = nums.size(), kCount = 0, count = 0, prefix | faltu_admi | NORMAL | 2021-05-26T16:16:41.446848+00:00 | 2021-05-26T16:16:41.446890+00:00 | 528 | false | ```\nclass Solution {\n public:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int l = 0, r = 0, n = nums.size(), kCount = 0, count = 0, prefix = 0;\n while (r < n) {\n // increase odd count\n kCount += (nums[r++] & 1);\n if (kCount > k) {\n l++;... | 6 | 0 | [] | 2 |
count-number-of-nice-subarrays | JavaScript sliding window | javascript-sliding-window-by-zckpp1992-z27u | Move right pointer until odd number reach k, then count even number towards right until reach next odd number or the end, count even number towards left untill | zckpp1992 | NORMAL | 2020-08-07T02:10:19.118364+00:00 | 2020-08-07T02:10:19.118400+00:00 | 344 | false | Move right pointer until odd number reach k, then count even number towards right until reach next odd number or the end, count even number towards left untill left pointer is sitting on an odd number, the product of left and right is the answer for this window, at last move left pointer and reduce odd number for 1 for... | 6 | 0 | [] | 0 |
count-number-of-nice-subarrays | Easy Solution Using Sliding Window! | easy-solution-using-sliding-window-by-sa-yngz | \nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int odd=0,l=0,res=0;\n for(int r= | sanjaygarg | NORMAL | 2019-11-11T07:11:31.848956+00:00 | 2019-11-11T07:11:31.849003+00:00 | 1,180 | false | ```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int odd=0,l=0,res=0;\n for(int r=0;r<n;r++)\n {\n if(nums[r]%2==1)\n odd++;\n while(l<r && odd>k)\n {\n if(nums[l]%2==1)... | 6 | 1 | [] | 2 |
count-number-of-nice-subarrays | Easy C++ solution | no sliding window | O(N) | easy-c-solution-no-sliding-window-on-by-prd1e | IntuitionWe store the indices of odd numbers and use basic math to calculate nice subarrays.Approach1]Store Indices of Odd numbers:
We traverse the array and st | mithilesh_19 | NORMAL | 2025-02-19T12:03:57.189128+00:00 | 2025-03-19T20:13:20.635674+00:00 | 278 | false | # Intuition
We store the indices of odd numbers and use basic math to calculate nice subarrays.
# Approach
***1]Store Indices of Odd numbers:***
- We traverse the array and store the positions (indices) of all odd numbers.
- To handle boundaries, we add ```-1``` at the start and ```n``` (array size) at the end of this... | 5 | 0 | ['Math', 'C++'] | 0 |
count-number-of-nice-subarrays | Java Solution, Beats 100.00% | java-solution-beats-10000-by-mohit-005-q6er | Intuition\nThe problem requires us to find the number of continuous subarrays that contain exactly k odd numbers. The approach used in the given code leverages | Mohit-005 | NORMAL | 2024-06-22T14:05:01.658170+00:00 | 2024-06-22T14:05:01.658192+00:00 | 1,481 | false | # Intuition\nThe problem requires us to find the number of continuous subarrays that contain exactly `k` odd numbers. The approach used in the given code leverages prefix sums and a hashmap to efficiently count the number of valid subarrays.\n\n# Approach\n1. **Prefix Sum with HashMap**: The idea is to use a prefix sum... | 5 | 0 | ['Java'] | 0 |
count-number-of-nice-subarrays | JAVA Solution Explained in HINDI(2 Approaches) | java-solution-explained-in-hindi2-approa-ck8q | https://youtu.be/xufUS2MRAZw\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote | The_elite | NORMAL | 2024-06-22T09:49:40.390947+00:00 | 2024-06-22T13:09:36.402213+00:00 | 638 | false | ERROR: type should be string, got "https://youtu.be/xufUS2MRAZw\\n\\nFor explanation, please watch the above video and do like, share and subscribe the channel. \\u2764\\uFE0F Also, please do upvote the solution if you liked it.\\n\\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\\n\\nSubscribe Goal:- 500\\nCurrent Subscriber:- 453\\n# Complexity\\n- Time complexity: O(n)\\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\\n\\n- Space complexity: O(1)\\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\\n\\n# Code\\n```\\nclass Solution {\\n private int numSubarraysLessThanOrEqualToGoal(int[] nums, int goal) {\\n int i = 0, j = 0, n = nums.length, count = 0, ans = 0;\\n\\n while (j < n) {\\n if (nums[j] % 2 == 1) count++;\\n while (i <= j && count > goal) {\\n if (nums[i] % 2 == 1) count--;\\n i++;\\n }\\n ans += j - i + 1;\\n j++;\\n }\\n return ans;\\n }\\n\\n public int numberOfSubarrays(int[] nums, int k) {\\n return numSubarraysLessThanOrEqualToGoal(nums, k) - \\n numSubarraysLessThanOrEqualToGoal(nums, k - 1);\\n }\\n}\\n```\\n\\n# Complexity\\n- Time complexity: O(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# Code\\n```\\nclass Solution {\\n public int numberOfSubarrays(int[] nums, int k) {\\n int n = nums.length;\\n int ans = 0;\\n int prefSum = 0;\\n Map<Integer, Integer> freq = new HashMap<>();\\n freq.put(0, 1);\\n\\n for (int i = 0; i < n; i++) {\\n prefSum += (nums[i] % 2 == 1) ? 1 : 0;\\n ans += freq.getOrDefault(prefSum - k, 0);\\n freq.put(prefSum, freq.getOrDefault(prefSum, 0) + 1);\\n }\\n return ans;\\n }\\n}\\n```" | 5 | 0 | ['Java'] | 0 |
count-number-of-nice-subarrays | O(n) - 8 line of code - short explanation | on-8-line-of-code-short-explanation-by-a-hio4 | Intuition\nChange this question to: count of sub-arrays with sum of elements equal to k\n Describe your first thoughts on how to solve this problem. \n\n# Appro | Assault_Rifle | NORMAL | 2024-06-22T01:32:48.449954+00:00 | 2024-06-22T01:32:48.449979+00:00 | 341 | false | # Intuition\nChange this question to: count of sub-arrays with sum of elements equal to k\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIf we treat all odd numbers as 1 and all even numbers as 0. This problem changes to count of total number of sub arrays with sum of its elements e... | 5 | 0 | ['C++'] | 1 |
count-number-of-nice-subarrays | beats 99 % - Using Queue | beats-99-using-queue-by-youknowwho3737-30ou | Approach\nSimply Use the queue to store the indexes of odd elements.\nStart counting the subarrays when the Queue is of K size.\nMaintain two variables end and | youknowwho3737 | NORMAL | 2024-06-22T01:29:13.571571+00:00 | 2024-06-22T01:29:13.571601+00:00 | 939 | false | # Approach\nSimply Use the queue to store the indexes of odd elements.\nStart counting the subarrays when the Queue is of K size.\nMaintain two variables end and last to count the values that can be added to next index.\n\n# Complexity\n- Time complexity:\nOnly one iterations, e.g. $$O(n)$$\n\n- Space complexity:\nA Qu... | 5 | 0 | ['Greedy', 'Queue', 'C++'] | 1 |
count-number-of-nice-subarrays | Sliding window + deque. One pass | sliding-window-deque-one-pass-by-xxxxkav-kbdw | \nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n ans, left, que = 0, -1, deque()\n for i in range(len(nums)): | xxxxkav | NORMAL | 2024-06-22T00:35:02.517316+00:00 | 2024-06-22T09:02:13.366990+00:00 | 336 | false | ```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n ans, left, que = 0, -1, deque()\n for i in range(len(nums)):\n if nums[i]%2:\n que.append(i)\n if len(que) > k:\n left = que.popleft()\n if len(qu... | 5 | 0 | ['Sliding Window', 'Python3'] | 2 |
count-number-of-nice-subarrays | Sliding Window | C++ | sliding-window-c-by-tusharbhart-1rz2 | \r\nclass Solution {\r\npublic:\r\n int numberOfSubarrays(vector<int>& nums, int k) {\r\n int n = nums.size(), dis = 1, i = 0, cnt = 0, ans = 0;\r\n | TusharBhart | NORMAL | 2023-01-16T13:38:33.745731+00:00 | 2023-01-16T13:38:33.745767+00:00 | 5,234 | false | ```\r\nclass Solution {\r\npublic:\r\n int numberOfSubarrays(vector<int>& nums, int k) {\r\n int n = nums.size(), dis = 1, i = 0, cnt = 0, ans = 0;\r\n vector<int> d(n + 1, 1);\r\n\r\n for(int i=n-1; i>=0; i--) {\r\n nums[i] % 2 ? dis = 1 : dis++;\r\n d[i] = dis;\r\n ... | 5 | 0 | ['Sliding Window', 'C++'] | 1 |
count-number-of-nice-subarrays | [C++] O(1) Space | c-o1-space-by-simplekind-hofa | \nclass Solution {\npublic:\n \n int atmost (vector<int>& arr, int k){\n if(arr.size()==0)\n return 0 ;\n int ans = 0;\n i | simplekind | NORMAL | 2022-03-24T04:58:00.777505+00:00 | 2022-03-24T04:58:00.777540+00:00 | 919 | false | ```\nclass Solution {\npublic:\n \n int atmost (vector<int>& arr, int k){\n if(arr.size()==0)\n return 0 ;\n int ans = 0;\n int j= 0;\n int count = 0;\n for ( int i =0;i<arr.size();i++){\n if(arr[i]&1){\n count++;\n }\n ... | 5 | 0 | ['C', 'Sliding Window', 'C++'] | 1 |
count-number-of-nice-subarrays | Java | Two-Pointer & Sliding window | Time: O(N) & Space: O(1) | java-two-pointer-sliding-window-time-on-kq336 | \nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n \n int odds = 0;\n int i = 0, j = 0, flag = 0;\n int n = nu | harshitcode13 | NORMAL | 2021-12-24T07:17:30.720758+00:00 | 2021-12-24T07:17:48.579389+00:00 | 1,169 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n \n int odds = 0;\n int i = 0, j = 0, flag = 0;\n int n = nums.length;\n int ans = 0;\n while(j < n){\n if(nums[j]%2!=0){\n odds++;\n }\n while(odds > k){\... | 5 | 0 | ['Java'] | 0 |
count-number-of-nice-subarrays | Count Number of Nice Subarray | count-number-of-nice-subarray-by-abhishe-ux6i | \n\n\n\n\n\n\nDiscuss\n\n1. In this problem , First we will change the odd digit from 1 and even with zero in given array.\n2. calculating the sum from start to | abhishekjha786 | NORMAL | 2021-12-16T19:57:17.210511+00:00 | 2021-12-31T05:33:46.880826+00:00 | 286 | false | \n\n\n\n\n\n\n**Discuss**\n\n1. In this problem , First we will change the odd digit from 1 and even with zero in given array.\n2. calculating the sum from start to end index with comparison k value\ni.e. sum - ... | 5 | 0 | ['Sliding Window'] | 2 |
count-number-of-nice-subarrays | Python solution beats 99% | python-solution-beats-99-by-ruizhang84-ko12 | There is a simple solution, by enumerate list and count the combinations of numbers.\n\n\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: i | ruizhang84 | NORMAL | 2019-11-08T03:23:24.095734+00:00 | 2019-11-08T03:23:24.095778+00:00 | 493 | false | There is a simple solution, by enumerate list and count the combinations of numbers.\n\n```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n # count\n arr = []\n for i, x in enumerate(nums):\n if x % 2 != 0:\n arr.append(i) \n a... | 5 | 1 | [] | 0 |
count-number-of-nice-subarrays | [JAVA] simple O(n) solution | java-simple-on-solution-by-nandathantsin-mwh3 | \nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n ArrayList<Integer> pos = new ArrayList<>();\n pos.add(0);\n for | nandathantsin | NORMAL | 2019-11-03T04:03:07.289666+00:00 | 2019-11-03T04:03:07.289724+00:00 | 1,061 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n ArrayList<Integer> pos = new ArrayList<>();\n pos.add(0);\n for(int i=0;i<nums.length;i++){\n if(nums[i]%2==1){\n pos.add(i+1);\n }\n }\n pos.add(nums.length+1);\n ... | 5 | 0 | ['Java'] | 1 |
count-number-of-nice-subarrays | Simple Java HashMap Solution O(n) by keeping track of count of odd numbers | simple-java-hashmap-solution-on-by-keepi-8xu7 | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n \n int n = nums.length;\n \n HashMap map = new HashMa | manrajsingh007 | NORMAL | 2019-11-03T04:02:15.192334+00:00 | 2019-11-03T04:03:38.733562+00:00 | 868 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n \n int n = nums.length;\n \n HashMap<Integer, Integer> map = new HashMap<>();\n \n int ans = 0;\n int currCount = 0;\n \n map.put(0, 1);\n \n for(int i = 0; i < n; ... | 5 | 0 | [] | 0 |
count-number-of-nice-subarrays | [Java] Sliding window / Two Pointers O(n) | java-sliding-window-two-pointers-on-by-p-yx3l | \nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int start = 0, end = 0, len = nums.length;\n int count = 0, oddCount = | pnimit | NORMAL | 2019-11-03T04:00:59.954306+00:00 | 2019-11-03T04:06:13.465562+00:00 | 2,095 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int start = 0, end = 0, len = nums.length;\n int count = 0, oddCount = 0, evenCount = 0;\n \n while(end < len){\n \n while(end < len && oddCount < k){\n if(nums[end++] % 2 != 0... | 5 | 0 | ['Two Pointers', 'Sliding Window', 'Java'] | 0 |
count-number-of-nice-subarrays | [C++] ✅ 💯 |Three Approaches | Sliding Window along with Advanced Approach | Prefix Sum | Time: O(n) | c-three-approaches-sliding-window-along-97hy6 | Method 1 : Sliding Window Approach\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\nTo solve the problem of counting the number of | sidharthjain321 | NORMAL | 2024-06-22T10:54:19.532590+00:00 | 2024-06-23T05:35:54.065741+00:00 | 106 | false | # Method 1 : Sliding Window Approach\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nTo solve the problem of counting the number of nice subarrays (subarrays with exactly k odd numbers), we can use a sliding window approach. This technique helps in efficiently counting subarrays by ... | 4 | 0 | ['Array', 'Hash Table', 'Math', 'Sliding Window', 'C++'] | 1 |
count-number-of-nice-subarrays | simple and easy || C++ and Python || solution 😍❤️🔥 | simple-and-easy-c-and-python-solution-by-3nbn | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Python Code\n\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n | shishirRsiam | NORMAL | 2024-06-22T08:31:39.898084+00:00 | 2024-06-22T08:31:39.898109+00:00 | 484 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Python Code\n```\nclass Solution:\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n pref_count = defaultdict(int)\n pref_count[0] = 1\n odd_count, ans = 0, 0\n for num in nums:\n odd_count += num%2\n ... | 4 | 0 | ['Array', 'Hash Table', 'Math', 'Sliding Window', 'C++', 'Python3'] | 3 |
count-number-of-nice-subarrays | Easy C++ Solution with video Explanation | easy-c-solution-with-video-explanation-b-770c | Video Explanation\nhttps://youtu.be/P9YW8n1GrlI?si=pAfMnzN4w9D7aL7d\n\n# Complexity\n- Time complexity: O(n) \n Add your time complexity here, e.g. O(n) \n\n- S | prajaktakap00r | NORMAL | 2024-06-22T04:31:12.982540+00:00 | 2024-06-22T04:31:12.982567+00:00 | 483 | false | # Video Explanation\nhttps://youtu.be/P9YW8n1GrlI?si=pAfMnzN4w9D7aL7d\n\n# Complexity\n- Time complexity: $$O(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# Code\n```\nclass Solution {\npublic:\n int numberOfS... | 4 | 0 | ['C++'] | 0 |
count-number-of-nice-subarrays | Number of Nice Subarray✨|| Easiest 🤩|| Fastest🚀🚀||Beats 99% 😤😤||Prefix Count🪛 | number-of-nice-subarray-easiest-fastestb-ttqr | \n\n\n# Intuition\nThe problem revolves around identifying subarrays that contain exactly k odd numbers. The intuition is to maintain a running count of odd num | AtrijPaul | NORMAL | 2024-06-22T02:53:57.950658+00:00 | 2024-06-22T02:53:57.950690+00:00 | 286 | false | \n\n\n# Intuition\nThe problem revolves around identifying subarrays that contain exactly k odd numbers. The intuition is to maintain a running count of odd numbers encountered as we i... | 4 | 0 | ['Array', 'Hash Table', 'Math', 'Prefix Sum', 'Java'] | 1 |
count-number-of-nice-subarrays | Easy o(n) solution | easy-on-solution-by-vinuaqua4u-hadv | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | vinuaqua4u | NORMAL | 2024-06-22T02:09:30.124433+00:00 | 2024-06-22T02:09:30.124465+00:00 | 483 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: o(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: o(1)\n<!-- Add your space complexity here, e.g. $... | 4 | 1 | ['Python3'] | 1 |
count-number-of-nice-subarrays | 💯✅🔥Easy Java ,Python3 ,C++ Solution|| 15 ms ||≧◠‿◠≦✌ | easy-java-python3-c-solution-15-ms-_-by-jzi27 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is to count the number of subarrays with exactly k odd numbers. This is done b | suyalneeraj09 | NORMAL | 2024-06-22T00:43:33.631488+00:00 | 2024-06-22T00:43:33.631522+00:00 | 53 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to count the number of subarrays with exactly k odd numbers. This is done by maintaining a sliding window of odd numbers and incrementing the count whenever the window size is equal to k. The subarray function calculates the t... | 4 | 4 | ['Array', 'C++', 'Java', 'Python3'] | 0 |
count-number-of-nice-subarrays | Dig Deep into Sliding window pattern & Two pointers | dig-deep-into-sliding-window-pattern-two-d9eq | 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 | Dixon_N | NORMAL | 2024-05-18T16:55:15.595017+00:00 | 2024-05-19T17:17:24.170589+00:00 | 415 | false | 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-understan... | 4 | 0 | ['Java'] | 2 |
count-number-of-nice-subarrays | 1248. Count Number of Nice Subarrays - Brute Force & Sliding Window Approaches | 1248-count-number-of-nice-subarrays-brut-5zuh | Brute Force - Memory Limit Exceeded\r\n\r\nclass Solution:\r\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n def countOdds(arr): # | TrGanesh | NORMAL | 2024-05-11T11:18:07.123689+00:00 | 2024-05-11T11:18:07.123713+00:00 | 332 | false | ## Brute Force - Memory Limit Exceeded\r\n```\r\nclass Solution:\r\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n def countOdds(arr): # Function to Count Odd Values in Arr.\r\n odds = 0\r\n for i in range(len(arr)):\r\n if arr[i] % 2 != 0:\r\n ... | 4 | 0 | ['Array', 'Sliding Window', 'Python3'] | 0 |
count-number-of-nice-subarrays | The solution you are looking for. | the-solution-you-are-looking-for-by-abhi-6buw | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n# HEY GUYS UPVOTE \r\nOverall Logic:\r\n\r\n1. The main function (numberOfSubarrays | Abhishekkant135 | NORMAL | 2024-04-23T23:42:35.607043+00:00 | 2024-04-23T23:42:35.607063+00:00 | 568 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n# HEY GUYS UPVOTE \r\n**Overall Logic:**\r\n\r\n1. The main function (`numberOfSubarrays`) finds the difference in the number of subarrays allowed with `k` odd numbers and `k-1` odd numbers.\r\n2. The `atmost` function efficiently cou... | 4 | 0 | ['Sliding Window', 'Java'] | 0 |
count-number-of-nice-subarrays | Beginner-Friendly Solution || Easy Video Explanation in Hindi | beginner-friendly-solution-easy-video-ex-n9xj | Intuition\r\nHashing + PrefixSum\r\n\r\n# Approach\r\n\r\nKindly check the video links given below for understanding both the prefix sum concept + actual soluti | nayankumarjha2 | NORMAL | 2024-02-21T16:44:47.342601+00:00 | 2024-02-21T16:44:47.342647+00:00 | 785 | false | # Intuition\r\nHashing + PrefixSum\r\n\r\n# Approach\r\n\r\nKindly check the video links given below for understanding both the prefix sum concept + actual solution. Detailed explanations are provided in Hindi.\r\n\r\nPrefix-Sum Concept(Pre-requisite) : www.youtube.com/watch?v=PX5uIJC4sXQ\r\n\r\nActual Solution : www.y... | 4 | 0 | ['Array', 'Hash Table', 'Prefix Sum', 'C++', 'Java', 'Python3'] | 0 |
count-number-of-nice-subarrays | Modifying the question (Making it much more easier) || Sliding window || Easy to Understand | modifying-the-question-making-it-much-mo-4ou6 | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nTreat odd numbers as 1 and even numbers as 0, then solve as if you want sum of suba | BihaniManan | NORMAL | 2023-07-05T12:00:27.683940+00:00 | 2023-07-05T12:00:27.683962+00:00 | 2,140 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nTreat odd numbers as 1 and even numbers as 0, then solve as if you want sum of subarray having sum = k.\r\n\r\nSame as this question: [930. Binary Subarrays With Sum\r\n](https://leetcode.com/problems/binary-subarrays-with-sum/)\r\n\r... | 4 | 0 | ['Sliding Window', 'Python', 'C++', 'Java'] | 5 |
count-number-of-nice-subarrays | Java simple with complete explanation | java-simple-with-complete-explanation-by-icot | Approach\r\nThe provided solution employs a sliding window technique to solve the problem. Let\'s break down the approach step by step:\r\n\r\n- Initialize vari | S_SAYUJ | NORMAL | 2023-05-20T08:13:43.266989+00:00 | 2023-05-22T15:16:13.064805+00:00 | 386 | false | # Approach\r\nThe provided solution employs a sliding window technique to solve the problem. Let\'s break down the approach step by step:\r\n\r\n- Initialize variables left, right, count, max, and temp. Set len as the length of the input array nums. Here, left and right represent the left and right pointers of the slid... | 4 | 0 | ['Array', 'Two Pointers', 'Sliding Window', 'Java'] | 1 |
count-number-of-nice-subarrays | 2 POINTER SIMPLE APPROACH || PYTHON 3 | 2-pointer-simple-approach-python-3-by-an-ogyx | \r\n# Code\r\n\r\nclass Solution:\r\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n count = 0\r\n i = 0\r\n j = 0\r\n | ankitbisht981 | NORMAL | 2023-05-18T17:46:08.100351+00:00 | 2023-05-18T17:46:08.100390+00:00 | 270 | false | \r\n# Code\r\n```\r\nclass Solution:\r\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n count = 0\r\n i = 0\r\n j = 0\r\n temp = 0\r\n ans = 0\r\n while(j<len(nums)):\r\n\r\n if nums[j]%2!=0:\r\n temp+=1\r\n count = ... | 4 | 0 | ['Array', 'Two Pointers', 'Sliding Window', 'Python3'] | 0 |
count-number-of-nice-subarrays | 🤗👀JAVA | VERY EASY | java-very-easy-by-dipesh_12-51nr | Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n\r\n# Approach\r\n Describe your approach to solving the problem. \r\n\r\n# Complex | dipesh_12 | NORMAL | 2023-05-05T07:40:06.048246+00:00 | 2023-05-05T07:40:06.048286+00:00 | 1,579 | false | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:\r\n<!-- Add your space complexity ... | 4 | 0 | ['Java'] | 2 |
count-number-of-nice-subarrays | Two Pointer Solution easy | two-pointer-solution-easy-by-tejesh_jain-p7hg | \r\n\r\n# Approach\r\n Describe your approach to solving the problem. \r\nIn this solution we basically take 2 pointers (i and j). If the nums[j] is odd we incr | Tejesh_Jain_0912 | NORMAL | 2023-03-04T03:55:37.575851+00:00 | 2023-03-04T03:55:37.575881+00:00 | 1,117 | false | \r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\nIn this solution we basically take 2 pointers (i and j). If the nums[j] is odd we increase the odd counter. If off is equal to k we count all the subarrays in between i and j by increasing i position till odd == k. We add the result in res... | 4 | 1 | ['Two Pointers', 'Java'] | 0 |
count-number-of-nice-subarrays | C++ easy 6 lines of code 🔥 🔥 | c-easy-6-lines-of-code-by-sakettiwari-b9ug | Intuition\nIf you remember two sum question you can have refrence \uD83D\uDD25 \uD83D\uDD25\nEnjoy !\n\n# UPVOTE IF IT HELPED \n# Code\n\nclass Solution {\npubl | Sakettiwari | NORMAL | 2023-02-09T17:35:33.273164+00:00 | 2023-02-09T17:35:33.273209+00:00 | 1,160 | false | # Intuition\nIf you remember two sum question you can have refrence \uD83D\uDD25 \uD83D\uDD25\nEnjoy !\n\n# UPVOTE IF IT HELPED \n# Code\n```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int i=0 , sum=0 ,count=0;\n unordered_map<int,int>map;\n while(i<nums.siz... | 4 | 0 | ['C++'] | 1 |
count-number-of-nice-subarrays | java solution | java-solution-by-kjain6_11-bnhy | \nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int i = 0;\n int j = 0;\n int odd = 0;\n int result = 0; | kjain6_11 | NORMAL | 2022-07-26T02:57:38.269917+00:00 | 2022-07-26T02:57:38.269952+00:00 | 538 | false | ```\nclass Solution {\n public int numberOfSubarrays(int[] nums, int k) {\n int i = 0;\n int j = 0;\n int odd = 0;\n int result = 0;\n int temp = 0;\n \n \n /* \n Approach : two pointer + sliding window technique\n \n step 1 : w... | 4 | 0 | ['Sliding Window', 'Java'] | 2 |
count-number-of-nice-subarrays | [C++] 3 ideas, solution, and explanation for beginners | c-3-ideas-solution-and-explanation-for-b-0a5n | First, the naive solution, we check every possible subarray and increment the count everytime we encounter a nice subarray.\n\nclass Solution {\npublic:\n in | haoran_xu | NORMAL | 2021-07-28T14:12:26.566549+00:00 | 2021-07-28T14:12:26.566611+00:00 | 311 | false | First, the naive solution, we check every possible subarray and increment the count everytime we encounter a nice subarray.\n```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int sum = 0;\n for(int i = 0; i < n; i++) {\n int co... | 4 | 0 | [] | 0 |
count-number-of-nice-subarrays | Easy C++ solution | easy-c-solution-by-imran2018wahid-8mbu | \nclass Solution \n{\npublic:\n // Function to calculate the number of subarrays having count of odd numbers lees than or equals to k\n int helper(vector< | imran2018wahid | NORMAL | 2021-06-07T19:21:37.221774+00:00 | 2021-06-07T19:22:40.616497+00:00 | 503 | false | ```\nclass Solution \n{\npublic:\n // Function to calculate the number of subarrays having count of odd numbers lees than or equals to k\n int helper(vector<int>&nums,int k)\n {\n int ans=0,i=0,count=0;\n for(int j=0;j<nums.size();j++)\n {\n if(nums[j]%2!=0)\n {\n ... | 4 | 0 | ['C'] | 1 |
count-number-of-nice-subarrays | c++ O(1) space 97%faster 100 % space efficient | c-o1-space-97faster-100-space-efficient-ms4vg | \nclass Solution {\npublic:\n Solution()\n {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n int func(vector | naman238 | NORMAL | 2020-05-05T16:06:16.178028+00:00 | 2020-05-05T16:06:16.178079+00:00 | 509 | false | ```\nclass Solution {\npublic:\n Solution()\n {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n int func(vector<int> &nums,int k)\n {\n int j=0;\n int cnt=0;\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n if(nums[i]%... | 4 | 0 | ['C', 'Sliding Window'] | 1 |
count-number-of-nice-subarrays | [C++] Map Solution | Easy Implementation | c-map-solution-easy-implementation-by-ni-qscd | \nint numberOfSubarrays(vector<int>& nums, int k) {\n\tint res = 0, sum = 0, n = nums.size();\n\tunordered_map<int, int> mpp;\n\tfor (int i = 0; i < n; ++i) {\n | ninilo97 | NORMAL | 2020-04-04T05:30:29.698988+00:00 | 2020-04-04T05:30:29.699021+00:00 | 609 | false | ```\nint numberOfSubarrays(vector<int>& nums, int k) {\n\tint res = 0, sum = 0, n = nums.size();\n\tunordered_map<int, int> mpp;\n\tfor (int i = 0; i < n; ++i) {\n\t\tmpp[sum]++;\n\t\tsum += nums[i] & 1;\n\t\tres += mpp[sum - k];\n\t}\n\treturn res;\n}\n``` | 4 | 0 | ['C', 'C++'] | 1 |
count-number-of-nice-subarrays | C++ Sliding Window | c-sliding-window-by-esamokhov-yeuz | \nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int count = 0;\n int l = 0;\n int r = 0;\n int | esamokhov | NORMAL | 2020-03-27T09:27:25.948157+00:00 | 2020-03-27T09:27:25.948193+00:00 | 915 | false | ```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int count = 0;\n int l = 0;\n int r = 0;\n int oddCount = 0;\n int size = nums.size();\n while (r < size) {\n while (oddCount < k && r < size) {\n if (nums[r] % 2 !... | 4 | 1 | ['C', 'Sliding Window'] | 1 |
count-number-of-nice-subarrays | Python straightforward solution | python-straightforward-solution-by-otoc-mtt2 | Straightforward Solution: similar to the idea in the solution for 828. Unique Letter String\n\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\ | otoc | NORMAL | 2019-11-05T00:34:32.417291+00:00 | 2019-11-05T00:34:32.417329+00:00 | 640 | false | Straightforward Solution: similar to the idea in [the solution for 828. Unique Letter String](https://leetcode.com/problems/unique-letter-string/discuss/128952/One-pass-O(N)-Straight-Forward)\n```\n def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n lst = [-1]\n for i in range(len(nums)):\n... | 4 | 0 | [] | 0 |
count-number-of-nice-subarrays | [Python] Easy to understand, Sliding window (Left & Right evens), Faster than 100% | python-easy-to-understand-sliding-window-snie | \tclass Solution(object):\n\t\tdef numberOfSubarrays(self, nums, k):\n\t\t\t"""\n\t\t\te.g. k = 2\n\t\t\tnums = [2, 2, 1, 2, 1, 2, 2]\n\t\t\tindex= 0 1 2 3 | d-_-b | NORMAL | 2019-11-03T17:35:09.325171+00:00 | 2019-11-03T17:43:20.568949+00:00 | 798 | false | \tclass Solution(object):\n\t\tdef numberOfSubarrays(self, nums, k):\n\t\t\t"""\n\t\t\te.g. k = 2\n\t\t\tnums = [2, 2, 1, 2, 1, 2, 2]\n\t\t\tindex= 0 1 2 3 4 5 6\n\t\t\t2 even numbers to left of first 1\n\t\t\t2 even numbers to right of last 1\n\t\t\ttotal number of subarrays = pick between 0-2 numbers on left, ... | 4 | 0 | ['Python'] | 0 |
count-number-of-nice-subarrays | C++ Sliding window O(N) | c-sliding-window-on-by-orangezeit-vnkj | \nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int i(0), ans(0), c(0);\n for (int j = 0; j < nums.size(); ++ | orangezeit | NORMAL | 2019-11-03T04:10:32.971513+00:00 | 2019-11-03T04:10:32.971550+00:00 | 285 | false | ```\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int i(0), ans(0), c(0);\n for (int j = 0; j < nums.size(); ++j) {\n if (nums[j] % 2) c++;\n if (c == k) {\n int t1(i), t2(j);\n while (nums[i++] % 2 == 0) {};\n ... | 4 | 3 | [] | 0 |
count-number-of-nice-subarrays | Sliding window solution | sliding-window-solution-by-yushi_lu-0z2k | The idea is sliding window with minor modification. Here is an example:\n2, 2, 3, 2, 2\nWe want to find 1 odd number. Using sliding window, we can find 2, 2, 1 | yushi_lu | NORMAL | 2019-11-03T04:02:43.061688+00:00 | 2019-11-03T04:03:16.411229+00:00 | 1,105 | false | The idea is sliding window with minor modification. Here is an example:\n```2, 2, 3, 2, 2```\nWe want to find 1 odd number. Using sliding window, we can find `2, 2, 1`. And we delete the first two \'2\', which means that there are 3 sub-arrays that contain one 3. \n\nBut it\'s not over, there are still two 2 left in t... | 4 | 0 | ['Sliding Window', 'C++'] | 1 |
count-number-of-nice-subarrays | O(n) solution | on-solution-by-coder206-jyvl | In the example [2,2,2,1,2,2,1,2,2,2] for k = 2 the first element of a nice subarray can be the first 1 or any 2 before it and the last element can be the second | coder206 | NORMAL | 2019-11-03T04:01:13.281353+00:00 | 2019-11-03T04:01:13.281403+00:00 | 906 | false | In the example [2,2,2,1,2,2,1,2,2,2] for k = 2 the first element of a nice subarray can be the first 1 or any 2 before it and the last element can be the second 1 or any 2 after it. We transform this array to [4,3,4] (every number in the new array is the length of subarray of even numbers plus 1). The answer is 16 = 4 ... | 4 | 1 | [] | 0 |
flatten-deeply-nested-array | ✔️Easy Solution✔️2625. Flatten Deeply Nested Array✔️Level up🚀your JavaScript skills 🚀Day 22 | easy-solution2625-flatten-deeply-nested-lr0u9 | Intuition\n Describe your first thoughts on how to solve this problem. \n>The problem requires us to flatten a multi-dimensional array based on a given depth. W | Vikas-Pathak-123 | NORMAL | 2023-05-26T05:55:26.204234+00:00 | 2023-06-04T05:13:26.645405+00:00 | 13,426 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n>The problem requires us to flatten a multi-dimensional array based on a given depth. We need to maintain the structure of the array up to the given depth and flatten the remaining subarrays.\n\n# Approach\n<!-- Describe your approach to ... | 156 | 0 | ['JavaScript'] | 12 |
flatten-deeply-nested-array | 100% Simple and Easy explanation | Faster Recursive Solution | 100-simple-and-easy-explanation-faster-r-egci | Understanding the Problem Statement\n\nWe have given a multi-dimensional array, let\'s suppose,\n\narr = [[1, 2], [3, [4, 5]]].\n\nThis array has two elements, | kartikkukreja | NORMAL | 2023-04-14T06:55:13.608711+00:00 | 2023-04-14T06:55:59.729980+00:00 | 5,888 | false | **Understanding the Problem Statement**\n\nWe have given a multi-dimensional array, let\'s suppose,\n\n`arr = [[1, 2], [3, [4, 5]]]`.\n\nThis array has two elements, each of which is itself an array. The first element **[1, 2]** has a depth of **1**, while the second element **[3, [4, 5]]** has a depth of **2**.\n\nTo ... | 33 | 0 | ['Recursion', 'JavaScript'] | 6 |
flatten-deeply-nested-array | Recursive & Iterative Approach | Detailed Explanation | recursive-iterative-approach-detailed-ex-cv38 | \u2705\u2705 Please Upvote if you find it useful \u2705\u2705\n\n## Recursive Approach:\n\n1. The function flat takes an input array arr and a depth as param | sourasb | NORMAL | 2023-05-26T11:33:01.171367+00:00 | 2023-05-26T11:56:11.570883+00:00 | 2,812 | false | ## \u2705\u2705 Please Upvote if you find it useful \u2705\u2705\n\n## Recursive Approach:\n\n1. The function `flat` takes an input array `arr` and a depth as parameters.\n2. If the current `depth` equals `0`, the function reaches the base case where no further flattening is required. In this case, the function crea... | 21 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | ✔️ JavaScript solution using Recursion || Easiest Solution !!! | javascript-solution-using-recursion-easi-hfz5 | # Intuition \n\n\n# Approach\n\n1. Write a recursive function that keeps track of the current depth.\n2. If the current depth >= the maximum depth, always jus | ray_aadii | NORMAL | 2023-08-31T07:43:24.105537+00:00 | 2023-08-31T07:43:24.105553+00:00 | 2,109 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Write a recursive function that keeps track of the current depth.\n2. If the current depth >= the maximum depth, always just push the value to the returned... | 16 | 0 | ['Recursion', 'JavaScript'] | 4 |
flatten-deeply-nested-array | 🗓️ Daily LeetCoding Challenge Day 26|| 🔥 JS SOL | daily-leetcoding-challenge-day-26-js-sol-41s4 | \n# Code\n\nvar flat = function(arr, depth) {\n const stack = [...arr.map(item => [item, depth])];\n const result = [];\n\n while (stack.length > 0) {\n c | DoaaOsamaK | NORMAL | 2024-06-14T19:06:17.589407+00:00 | 2024-06-14T19:06:17.589428+00:00 | 1,505 | false | \n# Code\n```\nvar flat = function(arr, depth) {\n const stack = [...arr.map(item => [item, depth])];\n const result = [];\n\n while (stack.length > 0) {\n const [item, depth] = stack.pop();\n\n if (Array.isArray(item) && depth > 0) {\n stack.push(...item.map(subItem => [subItem, depth - 1]));\n } else... | 12 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | Recursive TypeScript solution | recursive-typescript-solution-by-joshua_-o9im | Approach\nAny time I need to traverse a nested data structure, I reach for recursion.\n\nIntuitively, we need to loop through the array and check if each elemen | Joshua_desjones | NORMAL | 2023-04-12T00:52:11.414925+00:00 | 2023-04-12T00:52:11.414968+00:00 | 1,366 | false | # Approach\nAny time I need to traverse a nested data structure, I reach for recursion.\n\nIntuitively, we need to loop through the array and check if each element is a number. If so, it is already flat, so it can be pushed into our result. If the element is also an array, we need to loop it as well and check each of i... | 11 | 0 | ['TypeScript'] | 1 |
flatten-deeply-nested-array | Easiest Solution Ever | easiest-solution-ever-by-includerajat-ghuf | Code\n\nvar flat = function (arr, n) {\n // if n = 0 then we return arr or if all the element of array is \n // integer (not an array) we return arr.\n | includerajat | NORMAL | 2023-05-26T04:50:27.055549+00:00 | 2023-05-26T04:50:27.055582+00:00 | 1,307 | false | # Code\n```\nvar flat = function (arr, n) {\n // if n = 0 then we return arr or if all the element of array is \n // integer (not an array) we return arr.\n if(n == 0 || arr.every((item) => !Array.isArray(item))) return arr;\n const result = [];\n for(let i=0;i<arr.length;i++)\n if(Array.isArray(arr... | 7 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | ❇ flatten-deeply-nested-array👌 🏆O(N)❤️ Javascript❤️ Memory👀95.45%🕕 Meaningful Vars✍️ 🔴🔥 ✅ 👉 | flatten-deeply-nested-array-on-javascrip-eqb0 | \nvar flat = function (arr, n, tempArray = [], currentCycle = 0) {\n for (let index = 0; index < arr.length; index++) {\n if (Array.isArray(arr[index] | anurag-sindhu | NORMAL | 2023-10-17T16:11:25.271069+00:00 | 2023-10-17T16:11:25.271087+00:00 | 1,172 | false | ```\nvar flat = function (arr, n, tempArray = [], currentCycle = 0) {\n for (let index = 0; index < arr.length; index++) {\n if (Array.isArray(arr[index]) && currentCycle < n) {\n flat(arr[index], n, tempArray, currentCycle + 1)\n } else {\n tempArray.push(arr[index])\n }\n... | 6 | 0 | ['JavaScript'] | 2 |
flatten-deeply-nested-array | 🔥 Fastest & Easiest Solution | Time O(n) > 99% & Extra Space O(n) > 90% ✅ | fastest-easiest-solution-time-on-99-extr-2lxb | Solution from Pro JS Developer\n\nPlease take a look at the code below.\n\n# Approach\n\nNo rocket sciense, no magic, nothing excess.\nresult - returning array, | sahaviev | NORMAL | 2023-06-03T20:24:02.972718+00:00 | 2023-06-04T01:00:09.365883+00:00 | 469 | false | # Solution from Pro JS Developer\n\nPlease take a look at the code below.\n\n# Approach\n\nNo rocket sciense, no magic, nothing excess.\n**result** - returning array, accesed from closure.\n**toFlat** - recursion function with same arguments. On each flattening level just passed **n - 1**.\n\n# Complexity\n- Time compl... | 6 | 0 | ['JavaScript'] | 2 |
flatten-deeply-nested-array | Constant-space, in-place, fast, iterative solution with early-termination optimization | constant-space-in-place-fast-iterative-s-4sd6 | Intuition\nThe problem statement doesn\'t state that the input array must remain untouched, so we can perform operations in-place on the array to save memory. A | brian717fr | NORMAL | 2023-04-13T18:43:40.514785+00:00 | 2023-04-13T18:43:40.514817+00:00 | 1,021 | false | # Intuition\nThe problem statement doesn\'t state that the input array must remain untouched, so we can perform operations in-place on the array to save memory. Additionally, solving things iteratively saves us from adding *n* recursive function calls to the stack, resulting in O(1) space.\n\n# Approach\nIterate over t... | 6 | 0 | ['JavaScript'] | 2 |
flatten-deeply-nested-array | 100% Simple and Easy Solution | Faster Recursive Solution | 100-simple-and-easy-solution-faster-recu-o0fy | Complexity
Time complexity: O(N * D)
Space complexity: O(N)
Code | ashwin_menghar | NORMAL | 2025-02-13T07:22:26.451922+00:00 | 2025-02-13T07:22:26.451922+00:00 | 660 | false | # Complexity
- Time complexity: O(N * D)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```javascript []
/**
* @param {Array} arr
* @param {number} depth
* @return {Array}
*/
var flat = function (arr, n, res = []) {
... | 5 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | DAY(20*O(1)+2*O(1)) | Commented 👍| Eassiest ✅ | day20o12o1-commented-eassiest-by-drontit-olnv | The given code defines a function called flat using arrow function syntax. This function takes in two parameters: arr, which is an array, and n, which represent | DronTitan | NORMAL | 2023-05-26T04:54:43.616091+00:00 | 2023-05-26T04:54:43.616131+00:00 | 590 | false | The given code defines a function called `flat` using arrow function syntax. This function takes in two parameters: `arr`, which is an array, and `n`, which represents the depth level until which the array should be flattened.\n\nThe code uses a ternary operator to check if the value of `n` is truthy. If `n` is truthy ... | 5 | 0 | ['Array', 'JavaScript'] | 4 |
flatten-deeply-nested-array | 🔥🔥 Detailed Solution Using Recursion In Javascript || Faster Than 80%✔✔ | detailed-solution-using-recursion-in-jav-0y91 | Intuition\n Describe your first thoughts on how to solve this problem. \n According to the Problem Statement:\n 1. If depth of the current array is less than n | Boolean_Autocrats | NORMAL | 2023-05-18T14:57:46.508634+00:00 | 2023-05-18T14:58:42.597698+00:00 | 843 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n According to the Problem Statement:\n 1. If depth of the current array is less than n then we will flatten it.\n 2. If depth of the current array is equal to n then we will flatten it.\n# Approach\n<!-- Describe your approach to solving ... | 5 | 1 | ['JavaScript'] | 3 |
flatten-deeply-nested-array | Recurcive method | recurcive-method-by-yogiv07-5msr | Code\n\n/**\n * @param {Array} arr\n * @param {number} depth\n * @return {Array}\n */\nvar flat = function (arr, n) {\n if(n==0) return arr;\n var ans = [ | yogiv07 | NORMAL | 2024-06-06T15:48:03.129891+00:00 | 2024-06-06T15:48:03.129933+00:00 | 667 | false | # Code\n```\n/**\n * @param {Array} arr\n * @param {number} depth\n * @return {Array}\n */\nvar flat = function (arr, n) {\n if(n==0) return arr;\n var ans = [];\n for(var i=0; i<arr.length; i++){\n if(Array.isArray(arr[i])){\n ans.push(...flat(arr[i], n-1));\n } else {\n an... | 4 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | So easy 92%-85%✅🤛🍻 | so-easy-92-85-by-itachi2003-ntte | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n\n# Code\n\n/**\n * @ | itachi2003 | NORMAL | 2024-02-29T08:02:21.214981+00:00 | 2024-03-08T13:38:29.411903+00:00 | 606 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Code\n```\n/**\n * @param {Array} arr - The input array to be flattened.\n * @param {number} depth - The depth to which the array should be flattened.\n * @retur... | 4 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | Java Script Solution for Flatten Deeply Nested Array Problem | java-script-solution-for-flatten-deeply-6yjvp | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to recursively flatten the given multi-dimensional | Aman_Raj_Sinha | NORMAL | 2023-05-26T03:56:06.884355+00:00 | 2023-05-26T03:56:06.884399+00:00 | 416 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to recursively flatten the given multi-dimensional array while considering the depth constraint.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach used in the code is... | 4 | 1 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | Best JavaScript and TypeScript Solution for Beginner 💛 | best-javascript-and-typescript-solution-0ty2t | Code\njavascript []\nvar flat = function (arr, n) {\n if (n === 0) {\n return arr;\n }\n \n let answer = [];\n \n arr.forEach(element = | Ghauoor | NORMAL | 2023-05-23T16:21:28.416936+00:00 | 2023-05-23T16:21:28.416979+00:00 | 1,461 | false | # Code\n```javascript []\nvar flat = function (arr, n) {\n if (n === 0) {\n return arr;\n }\n \n let answer = [];\n \n arr.forEach(element => {\n if (n > 0 && Array.isArray(element)) {\n answer.push(...flat(element, n - 1));\n } else {\n answer.push(element);... | 4 | 0 | ['TypeScript', 'JavaScript'] | 2 |
flatten-deeply-nested-array | Simple 1 line solution | simple-1-line-solution-by-arnavn101-b9ad | Approach\nRecursively reduce arr by recursing through non-numerical inputs until base case of n === 0. If e is an element, it\'s pushed into acc, otherwise, res | arnavn101 | NORMAL | 2023-04-12T01:24:49.470204+00:00 | 2023-04-12T01:25:22.577494+00:00 | 966 | false | # Approach\nRecursively reduce `arr` by recursing through non-numerical inputs until base case of `n === 0`. If `e` is an element, it\'s pushed into `acc`, otherwise, result of recursive call is unpacked before being pushed. For each case we return `acc` in the `reduce` HOF.\n\n# Complexity\n- Time complexity:\n$$O(n)$... | 4 | 1 | ['Recursion', 'JavaScript'] | 2 |
flatten-deeply-nested-array | JavaScript Solution | javascript-solution-by-motaharozzaman199-vej7 | \n\n\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n if (n==0){\n return arr;\n }\ | Motaharozzaman1996 | NORMAL | 2023-05-26T14:47:39.201449+00:00 | 2023-05-26T14:47:39.201481+00:00 | 1,353 | false | \n\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n if (n==0){\n return arr;\n }\n let ans=[];\n for (let i=0; i<arr.length; i++){\n if(n>0 && Array.isArray(arr[i])){\n ans.push(...flat(arr[i],n-1));\n }else... | 3 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | JavaScript Easy Solution | javascript-easy-solution-by-ankush-kashy-q2ll | \n\n# Code\n\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n let res = [];\n const flatte | Ankush-Kashyap | NORMAL | 2023-05-26T09:03:16.257701+00:00 | 2023-05-26T09:03:16.260438+00:00 | 502 | false | \n\n# Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n let res = [];\n const flattening = (nums, l) => {\n for (const num of nums) {\n if (Array.isArray(num) && l > 0 && l <= n) {\n flattening(num, l - 1);\n } el... | 3 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | "Inorder" DFS Traversal (ft. Generators) | inorder-dfs-traversal-ft-generators-by-j-lbmx | Intuition\nThe nested structure of the array suggests a recursive, tree-like structure. Thinking about how the array is flattened in this structure leads to the | jeffreyhu8 | NORMAL | 2023-04-12T14:01:37.234500+00:00 | 2023-04-12T14:01:37.234542+00:00 | 687 | false | # Intuition\nThe nested structure of the array suggests a recursive, tree-like structure. Thinking about how the array is flattened in this structure leads to the idea of a DFS traversal.\n\n# Approach\nFor some array, we scan the elements left to right.\n\nIf the element is a number, we add it to our answer array.\n\n... | 3 | 0 | ['Array', 'Depth-First Search', 'Recursion', 'TypeScript'] | 2 |
flatten-deeply-nested-array | [JavaScript] A simple recursive solution | javascript-a-simple-recursive-solution-b-wvpu | Overall Idea\n\nThis problem lends very well for a recursive solution! We iterate through the array, checking for nested arrays within.\n\n1) If the element is | tangj1905 | NORMAL | 2023-04-11T22:47:59.017471+00:00 | 2023-04-12T04:23:41.725424+00:00 | 1,380 | false | # Overall Idea\n\nThis problem lends very well for a recursive solution! We iterate through the array, checking for nested arrays within.\n\n1) If the element is another array, we call it recursively down one level. We can use the spread operator (`...`) to do the flattening for us when appending to our flattened array... | 3 | 0 | ['Recursion', 'JavaScript'] | 1 |
flatten-deeply-nested-array | using flat() | using-flat-by-joelll-km7i | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Joelll | NORMAL | 2024-03-18T08:44:15.442108+00:00 | 2024-03-18T08:44:15.442143+00:00 | 447 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['JavaScript'] | 3 |
flatten-deeply-nested-array | JavaScript | Flatten Deeply Nested Array | javascript-flatten-deeply-nested-array-b-g8qe | Intuition\n Describe your first thoughts on how to solve this problem. \nPre-defined terms:\n\n1. Multi-dimensional array \u2192 a recursive data structure that | samabdullaev | NORMAL | 2023-11-10T20:03:27.526486+00:00 | 2023-11-10T20:03:27.526506+00:00 | 36 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPre-defined terms:\n\n1. `Multi-dimensional array` \u2192 a recursive data structure that contains integers or other multi-dimensional arrays\n\n2. `Flattened array` \u2192 a version of that array with some or all of the sub-arrays remove... | 2 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | Elegant and readable solution | elegant-and-readable-solution-by-arvisix-nici | Code\n\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nconst flat = (arr, n) => {\n if (n === 0) return arr\n\n const flat | arvisix | NORMAL | 2023-08-11T11:44:59.460185+00:00 | 2023-08-11T11:44:59.460211+00:00 | 376 | false | # Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nconst flat = (arr, n) => {\n if (n === 0) return arr\n\n const flatArr = []\n\n arr.forEach(el => \n (Array.isArray(el) && n > 0) ? flatArr.push(...flat(el, n - 1)) : flatArr.push(el)\n )\n\n return flatArr... | 2 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | JavaScript || Easy to Understand ✅✅ | javascript-easy-to-understand-by-shubham-geo5 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Shubhamjain287 | NORMAL | 2023-06-17T05:24:07.096041+00:00 | 2023-06-17T05:24:07.096069+00:00 | 570 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | JavaScript | Recursive Solution | javascript-recursive-solution-by-foyez-4m3w | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | foyez | NORMAL | 2023-05-27T02:44:39.187743+00:00 | 2023-05-27T02:44:39.187780+00:00 | 157 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$... | 2 | 0 | ['JavaScript'] | 0 |
flatten-deeply-nested-array | JavaScript | Recursive Solution | javascript-recursive-solution-by-varshar-aym9 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | VarshaRani_9 | NORMAL | 2023-05-26T03:05:55.167369+00:00 | 2023-05-26T03:05:55.167410+00:00 | 717 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | Best JavaScript Solution for Beginner 💛 | best-javascript-solution-for-beginner-by-hlfd | Code\n\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n if (n === 0) {\n return arr;\n | Ghauoor | NORMAL | 2023-05-23T16:16:32.509163+00:00 | 2023-05-23T16:16:32.509201+00:00 | 678 | false | # Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n if (n === 0) {\n return arr;\n }\n \n let answer = [];\n \n arr.forEach(element => {\n if (n > 0 && Array.isArray(element)) {\n answer.push(...flat(elem... | 2 | 0 | ['JavaScript'] | 1 |
flatten-deeply-nested-array | ✅ JavaScript - recursive solution | javascript-recursive-solution-by-daria_a-i0ce | Approach\n Describe your approach to solving the problem. \nFlatten arrays recursively until n equals 0, then return array itself.\n\n# Code\n\n/**\n * @param { | daria_abdulnasyrova | NORMAL | 2023-04-17T14:37:08.781967+00:00 | 2023-04-17T14:37:08.781996+00:00 | 291 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nFlatten arrays recursively until n equals 0, then return array itself.\n\n# Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n if (n === 0) return arr;\n\n let result = [];\... | 2 | 0 | ['JavaScript'] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.