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-substrings-with-k-frequency-characters-i | Easy to understand solution in Java using sliding window. | easy-to-understand-solution-in-java-usin-1c6t | ApproachWe maintain two pointers, left and right, to represent the sliding window's boundaries. left starts at 0, and right moves from 0 to the end of the strin | Khamdam | NORMAL | 2025-02-14T16:43:42.334586+00:00 | 2025-02-14T16:43:42.334586+00:00 | 1 | false | # Approach
We maintain two pointers, left and right, to represent the sliding window's boundaries. left starts at 0, and right moves from 0 to the end of the string.
We use a HashMap freq to track the frequency of characters in the current window. As right moves to the next character, we update the frequency of that c... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Python3 solution | explained | sliding window | python3-solution-explained-sliding-windo-mez4 | IntuitionSliding windowApproachConstruct a frequency hashmap to maintain the numbers for each letter. The number of elements in the dictionary will always be 26 | FlorinnC1 | NORMAL | 2025-02-13T20:42:20.584202+00:00 | 2025-02-13T20:42:20.584202+00:00 | 6 | false | # Intuition
Sliding window
# Approach
Construct a frequency hashmap to maintain the numbers for each letter. The number of elements in the dictionary will always be 26 at maximum. We will get the number of numbers wich have freq >= k at every iteration and based on that add it to the count. We always only need 1 eleme... | 0 | 0 | ['Sliding Window', 'Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple sliding window solution 😊 | simple-sliding-window-solution-by-harshl-1hpg | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | harshlage | NORMAL | 2025-01-25T11:16:45.380304+00:00 | 2025-01-25T11:16:45.380304+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Count Substrings in one pass. | count-substrings-in-one-pass-by-rishiins-wy4f | IntuitionApproachOur aim is to count the number of substrings where no character appears at least (k) times. It starts by calculating the total number of substr | RishiINSANE | NORMAL | 2025-01-25T08:29:54.752737+00:00 | 2025-01-25T08:29:54.752737+00:00 | 2 | false | # Intuition
# Approach
Our aim is to count the number of substrings where no character appears at least \(k\) times. It starts by calculating the total number of substrings in the string using the formula `(n * (n + 1)) / 2`, where \(n\) is the string length. Then, it uses a sliding window technique with two pointers,... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple Greedy & Sliding Window | simple-greedy-sliding-window-by-eskandar-rfxx | Complexity
Time complexity:
O(n)
Space complexity:
O(1)Code | Eskandar1 | NORMAL | 2025-01-23T20:55:44.773103+00:00 | 2025-01-23T21:10:17.542648+00:00 | 6 | false |
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
---
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
---
# Code
```cpp []
class Solution {
public:
int numberOfSubstrings(string s, int k) {
int frq[26]={0};
int l=0, r=0, n=s.s... | 0 | 0 | ['Hash Table', 'String', 'Greedy', 'Sliding Window', 'C++', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | 📌📌Easy to understand solution📌📌 | Beginner-friendly✅✅ | easy-to-understand-solution-beginner-fri-2mke | Complexity
Time complexity: O(n)
Space complexity: O(26)
Code | Sankar_Madhavan | NORMAL | 2025-01-20T16:53:08.986371+00:00 | 2025-01-20T16:53:08.986371+00:00 | 4 | false | # Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(26)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int numberOfSubstrings(string s, int k) {
int n = s.length();
unordered_map<char, in... | 0 | 0 | ['Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple Brute Force Solution | simple-brute-force-solution-by-shaik_far-xklm | IntuitionIf any character frequency is equal to K then increment the count.Twist if as soon as we find character frequency equal to k then if any other characte | shaik_farhaan1 | NORMAL | 2025-01-19T11:48:56.613066+00:00 | 2025-01-19T11:48:56.613066+00:00 | 5 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
If any character frequency is equal to K then increment the count.Twist if as soon as we find character frequency equal to k then if any other characters are left then that cnt of the substring including that characters must also be added.... | 0 | 0 | ['Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | C++ Sliding Window, ( 100.00% | 87.57%) | c-sliding-window-10000-8757-by-lckharry-sc5l | IntuitionApproachComplexity
Time complexity:O(n)
Space complexity:O(1)
Code | LCKharry | NORMAL | 2025-01-15T08:32:23.294641+00:00 | 2025-01-15T08:32:23.294641+00:00 | 5 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:$$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:$$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)... | 0 | 0 | ['Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Slide Window | slide-window-by-linda2024-bpn2 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | linda2024 | NORMAL | 2025-01-13T21:18:38.347194+00:00 | 2025-01-13T21:18:38.347194+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C#'] | 0 |
count-substrings-with-k-frequency-characters-i | C++, sliding window, O(n) && O(1) | c-sliding-window-on-o1-by-viktor_komarov-2vmr | Complexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | viktor_komarov | NORMAL | 2025-01-12T09:25:00.423369+00:00 | 2025-01-12T09:25:00.423369+00:00 | 4 | false | # Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```cpp []
class Solution {
public:
int numberOfSubstrings(string s, int k) {
array<int, 26> counts = {0};
int total = 0;
size_t left = 0;
for (size_t right = 0; right < s.size(); ++right) {
++counts[s[... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple | Intuitive | simple-intuitive-by-richardleee-46br | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | RichardLeee | NORMAL | 2025-01-11T11:25:43.509411+00:00 | 2025-01-11T11:25:43.509411+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | o[n] solution in c++ | on-solution-in-c-by-ansh0111-gnv6 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ansh0111 | NORMAL | 2025-01-04T07:34:52.267905+00:00 | 2025-01-04T07:34:52.267905+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | 3325. Count Substrings With K-Frequency Characters I | 3325-count-substrings-with-k-frequency-c-zusl | IntuitionOnce we are confirmed that a character repeats k times, we no longer need to keep traversing through the array.Code | priyam_saha17 | NORMAL | 2024-12-29T01:17:27.767671+00:00 | 2024-12-29T01:17:27.767671+00:00 | 7 | false | # Intuition
Once we are confirmed that a character repeats k times, we no longer need to keep traversing through the array.
# Code
```python3 []
class Solution:
def numberOfSubstrings(self, s: str, k: int) -> int:
ans = 0
for i in range(0, len(s)):
freq = [0]*26
for j in ran... | 0 | 0 | ['Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding window| Optimal Approach| Beginner Friendly | sliding-window-optimal-approach-beginner-5wxg | \n\n# Code\ncpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n //abcde k=1\n int i,j=0;\n int n=s.size();\n | ashishabhishek2019 | NORMAL | 2024-12-08T16:15:48.232179+00:00 | 2024-12-08T16:15:48.232203+00:00 | 6 | false | \n\n# Code\n```cpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n //abcde k=1\n int i,j=0;\n int n=s.size();\n \n map<char,int>mp;\n int cnt=0;\n // just as soon as a frequency gets to k you can count all substring by simple math because ... | 0 | 0 | ['Hash Table', 'String', 'Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | python Solution || Sliding window | python-solution-sliding-window-by-nitish-6b53 | 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 | Nitishk25 | NORMAL | 2024-12-02T09:56:38.139415+00:00 | 2024-12-02T09:56:38.139439+00:00 | 5 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding window || C++ Solution | sliding-window-c-solution-by-nitishk25-e8b8 | 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 | Nitishk25 | NORMAL | 2024-12-02T08:15:47.481758+00:00 | 2024-12-02T08:15:47.481798+00:00 | 1 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Copied solution | copied-solution-by-sachinab-pphx | \n# Code\njava []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int n = s.length(), res = n*(n+1)/2;\n int[] count = n | sachinab | NORMAL | 2024-11-29T13:43:49.773966+00:00 | 2024-11-29T13:43:49.774000+00:00 | 2 | false | \n# Code\n```java []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int n = s.length(), res = n*(n+1)/2;\n int[] count = new int[26];\n\n for(int i=0, j=0; j<n; j++){\n char c = s.charAt(j);\n count[c-\'a\']++;\n\n while(count[c-\'a\']>=k)... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Count Substrings with K-Frequency | count-substrings-with-k-frequency-by-suy-veba | Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly I thought of using the sliding window approach as there is k.\n# Approach\n Des | ranges | NORMAL | 2024-11-27T16:18:03.949652+00:00 | 2024-11-27T16:18:03.949681+00:00 | 2 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly I thought of using the sliding window approach as there is k.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSliding Window:\n\nWe use two pointers, i and j, to represent the sliding window. i is the start o... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Easy C++ solution -- O(n) -- Sliding window | easy-c-solution-on-sliding-window-by-shu-f4v9 | 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 | shubhamkmt | NORMAL | 2024-11-27T15:44:49.852820+00:00 | 2024-11-27T15:44:49.852863+00:00 | 2 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Easy solution | easy-solution-by-_jyoti_geek-7wwv | Code\njava []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int n = s.length();\n HashMap<Character, Integer> map = ne | _jyoti_geek | NORMAL | 2024-11-22T15:40:54.526684+00:00 | 2024-11-22T15:40:54.526715+00:00 | 2 | false | # Code\n```java []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int n = s.length();\n HashMap<Character, Integer> map = new HashMap<>();\n\n int i = 0, j = 0;\n int ct = 0;\n while (i < n) {\n char ch = s.charAt(i);\n map.put(ch, map.... | 0 | 0 | ['Hash Table', 'String', 'Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Count Substrings With K-Frequency Characters I | Java | 100% | count-substrings-with-k-frequency-charac-vrtn | 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 | Ajay-Ganapathy | NORMAL | 2024-11-21T14:41:14.251015+00:00 | 2024-11-21T14:41:14.251041+00:00 | 3 | 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)$$ --... | 0 | 0 | ['Hash Table', 'Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | C++ solution | c-solution-by-nguyenchiemminhvu-smis | \nstatic bool fast = []()\n{\n std::cin.tie(0)->sync_with_stdio(false);\n return true;\n}();\n\nclass Solution\n{\npublic:\n int numberOfSubstrings(con | nguyenchiemminhvu | NORMAL | 2024-11-18T06:13:03.527528+00:00 | 2024-11-18T06:13:03.527553+00:00 | 0 | false | ```\nstatic bool fast = []()\n{\n std::cin.tie(0)->sync_with_stdio(false);\n return true;\n}();\n\nclass Solution\n{\npublic:\n int numberOfSubstrings(const string& s, int k)\n {\n int res = 0;\n\n int left = 0;\n int right = 0;\n std::unordered_map<char, int> freq;\n int ... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple Sliding Window C++ | simple-sliding-window-c-by-ken_14-2pk0 | 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 | ken_14 | NORMAL | 2024-11-17T14:28:58.136777+00:00 | 2024-11-17T14:28:58.136802+00:00 | 4 | 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:\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:... | 0 | 0 | ['Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Slide Window | slide-window-by-hityxh2018-rv1m | 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 | hityxh2018 | NORMAL | 2024-11-13T13:18:10.100613+00:00 | 2024-11-13T13:18:10.100649+00:00 | 0 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding Window 100 beats | sliding-window-100-beats-by-pk1698629-p2hd | Intuition\nSo we will bw doing this problem bu using sliding window approach\n\n\n# Approach\n\nwe have to run a for loop by expanding the right \nnow\nwe just | pk1698629 | NORMAL | 2024-11-12T12:54:51.779274+00:00 | 2024-11-12T12:54:51.779298+00:00 | 1 | false | # Intuition\nSo we will bw doing this problem bu using sliding window approach\n\n\n# Approach\n\nwe have to run a for loop by expanding the right \nnow\nwe just have to see the point where we are having an alphabet with frequency of k and then we have to count the possibilities of substrings by subtracting the index(a... | 0 | 0 | ['Two Pointers', 'Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Two pointers easy c++ code | two-pointers-easy-c-code-by-ziang142019-mk2b | \n# Code\ncpp []\n#define FOR(i,a,b) for(int i = a; i < b.size(); i++)\n#define F0(i,b) for(int i = 0; i< b.size(); i++)\n#define BE(a) a.begin(), | ziang142019 | NORMAL | 2024-11-11T19:11:25.538211+00:00 | 2024-11-11T19:11:25.538253+00:00 | 1 | false | \n# Code\n```cpp []\n#define FOR(i,a,b) for(int i = a; i < b.size(); i++)\n#define F0(i,b) for(int i = 0; i< b.size(); i++)\n#define BE(a) a.begin(), a.end()\n#define __S(a) a.size()\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vector<int>> vvi;\ntypedef vector <long long> vl;\n... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding window in js, Beats 73.31% in runtime | sliding-window-in-js-beats-7331-in-runti-qdy1 | Intuition\n Describe your first thoughts on how to solve this problem. \nsliding window\n\n# Approach\n Describe your approach to solving the problem. \nif we a | masha-nv | NORMAL | 2024-11-08T13:11:48.217761+00:00 | 2024-11-08T13:13:29.952841+00:00 | 4 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nsliding window\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nif we are on our left and right pointers such that number of any char is equal to k, we need to add to our final count that substring and all left and ... | 0 | 0 | ['JavaScript'] | 0 |
count-substrings-with-k-frequency-characters-i | Aditya Verma | Sliding Window Pattern | C++ | aditya-verma-sliding-window-pattern-c-by-6cw3 | \n\n\n\n\n## Aditya Verma - Variable Sized Sliding Window Approach\n\n## Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n\n- | konarksharmaa | NORMAL | 2024-11-05T12:35:36.737926+00:00 | 2024-11-05T12:35:36.737963+00:00 | 1 | false | \n\n\n\n\n## Aditya Verma - Variable Sized Sliding Window Approach\n\n## Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n## Code\n```cpp []\nclass Solution {\npublic:\n int numberOfSubs... | 0 | 0 | ['Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Super Simple || C++ | super-simple-c-by-lotus18-syim | Code\ncpp []\nclass Solution \n{\npublic:\n int numberOfSubstrings(string s, int k) \n {\n int n=s.size(), cnt=0;\n for(int x=0; x<n; x++)\n | lotus18 | NORMAL | 2024-11-05T07:27:22.548086+00:00 | 2024-11-05T07:27:22.548118+00:00 | 1 | false | # Code\n```cpp []\nclass Solution \n{\npublic:\n int numberOfSubstrings(string s, int k) \n {\n int n=s.size(), cnt=0;\n for(int x=0; x<n; x++)\n {\n map<char,int> m;\n int flag=0;\n for(int y=x; y<n; y++)\n {\n m[s[y]]++;\n ... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Intuitive Sliding Window Solution in C++ | intuitive-sliding-window-solution-in-c-b-j3wo | ---\n\n# Solution Explanation\n\n## Problem\nGiven a string s and an integer k, we need to count the number of substrings where any character appears at least k | _jayesh | NORMAL | 2024-11-02T17:52:24.044328+00:00 | 2024-11-02T17:52:24.044365+00:00 | 4 | false | ---\n\n# Solution Explanation\n\n## Problem\nGiven a string `s` and an integer `k`, we need to count the number of substrings where any character appears at least `k` times. This problem is solved by iterating over possible substrings and using a sliding window approach to efficiently count the substrings that satisfy ... | 0 | 0 | ['Two Pointers', 'String', 'Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding Window | Beats 100% | Frequency Array | sliding-window-beats-100-frequency-array-muky | Intuition\nWe can see from the problem that if we find a string that satisfies the condition, we can keep adding each character left in the whole string and it | nuggetcrab | NORMAL | 2024-10-30T17:05:59.094302+00:00 | 2024-10-30T17:05:59.094335+00:00 | 3 | false | # Intuition\nWe can see from the problem that if we find a string that satisfies the condition, we can keep adding each character left in the whole string and it would still satisfy this condition. We know that we should then find every first occurence that satisfies the condition, and then add the amount of characters... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | My Solution Java Simple | my-solution-java-simple-by-user5475a-hmld | 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 | user5475a | NORMAL | 2024-10-29T17:26:25.837905+00:00 | 2024-10-29T17:26:25.837935+00:00 | 1 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | sliding window technique | sliding-window-technique-by-owenwu4-c8f3 | 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 | owenwu4 | NORMAL | 2024-10-28T00:18:23.840238+00:00 | 2024-10-28T00:18:23.840265+00:00 | 9 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple C++ Approach and Solution | Weekly Contest 420 | Question 2 | simple-c-approach-and-solution-weekly-co-xuaz | Intuition\n Describe your first thoughts on how to solve this problem. \n1. Fix the left index of the substring.\n2. For the fixed left index, find the first ri | ak0012916 | NORMAL | 2024-10-27T04:26:12.317859+00:00 | 2024-10-27T04:26:12.317880+00:00 | 5 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Fix the left index of the substring.\n2. For the fixed left index, find the first right index for which substring `s[left..right]` satisfies the condition.\n3. Every substring that starts at left and ends after right satisfies the cond... | 0 | 0 | ['Hash Table', 'String', 'Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | C++ solution using sliding window way | c-solution-using-sliding-window-way-by-l-kc7y | \n\n# Code\ncpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n\n int res = 0;\n int count_larger_k = 0;\n u | lcq_dev | NORMAL | 2024-10-26T06:55:21.228925+00:00 | 2024-10-26T06:55:21.228962+00:00 | 1 | false | \n\n# Code\n```cpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n\n int res = 0;\n int count_larger_k = 0;\n unordered_map<char, int> char2Int;\n int i = 0;\n \n\n for(int j = 0; j < s.size();j ++) {\n char cur_char = s[j];\n ... | 0 | 0 | ['C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple and Easy JAVA Solution , Beats 100% using Sliding Window approach | simple-and-easy-java-solution-beats-100-al097 | Intuition\n\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- Tim | Triyaambak | NORMAL | 2024-10-25T19:53:39.172121+00:00 | 2024-10-25T19:53:39.172155+00:00 | 2 | false | # Intuition\n\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... | 0 | 0 | ['Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | scala sliding window | scala-sliding-window-by-vititov-lvkg | scala []\nobject Solution {\n def numberOfSubstrings(s: String, k: Int): Int = {\n def f(l0: Int, l1: Int, r: Int, aMap: Map[Char,Int], acc: Int): Int =\n | vititov | NORMAL | 2024-10-25T19:41:00.363907+00:00 | 2024-10-25T19:41:00.363931+00:00 | 1 | false | ```scala []\nobject Solution {\n def numberOfSubstrings(s: String, k: Int): Int = {\n def f(l0: Int, l1: Int, r: Int, aMap: Map[Char,Int], acc: Int): Int =\n if(aMap.values.maxOption.getOrElse(0)>=k) g(l0,l1,r,aMap,acc)\n else if(r >= s.length) acc\n else f(l0,l1,r+1,aMap+(s(r) -> (aMap(s(r))+1)),acc... | 0 | 0 | ['Hash Table', 'String', 'Sliding Window', 'Scala'] | 0 |
count-substrings-with-k-frequency-characters-i | Easy solution using hashtable || GOLANG || CPP | easy-solution-using-hashtable-golang-cpp-3oun | Intuition\nThe problem requires counting the number of substrings where each character appears at least k times. We iterate over each starting index of the subs | sonuola | NORMAL | 2024-10-25T16:18:51.612084+00:00 | 2024-10-25T16:18:51.612110+00:00 | 7 | false | # Intuition\nThe problem requires counting the number of substrings where each character appears at least k times. We iterate over each starting index of the substring, expanding from that point and using an array to keep track of character counts. Once a substring meets the condition, all following substrings starting... | 0 | 0 | ['Hash Table', 'C++', 'Go'] | 0 |
count-substrings-with-k-frequency-characters-i | MAP || AVS | map-avs-by-vishal1431-ex0e | \n# Code\ncpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n | Vishal1431 | NORMAL | 2024-10-24T19:02:12.631917+00:00 | 2024-10-24T19:02:12.631962+00:00 | 0 | false | \n# Code\n```cpp []\nclass Solution {\npublic:\n int numberOfSubstrings(string s, int k) {\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n map<char, int> m;\n int check = 0;\n for (int j = i; j < s.size(); j++) {\n m[s[j]]++;\n if (... | 0 | 0 | ['Hash Table', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | Simple Sliding Window solution | simple-sliding-window-solution-by-vikash-sgmn | 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 | vikash_kumar_dsa2 | NORMAL | 2024-10-24T18:00:19.932594+00:00 | 2024-10-24T18:00:19.932620+00:00 | 13 | 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)$$ --... | 0 | 0 | ['Hash Table', 'Sliding Window', 'C++'] | 0 |
count-substrings-with-k-frequency-characters-i | [scala] - sliding window, no index, foldLeft, recursion | scala-sliding-window-no-index-foldleft-r-p71r | I am deeply concerned by the amount of attention given to Scala. Please upvote and submit your solutions.\n\n# Code\nscala\nobject Solution {\n import scala.co | nikiforo | NORMAL | 2024-10-24T16:14:17.070122+00:00 | 2024-10-24T16:15:47.235261+00:00 | 2 | false | I am deeply concerned by the amount of attention given to Scala. Please upvote and submit your solutions.\n\n# Code\n```scala\nobject Solution {\n import scala.collection.immutable.Queue\n\n def numberOfSubstrings(s: String, k: Int): Int =\n s.foldLeft(Queue.empty[Char], Map.empty[Char, Int], 0) { case ((queue, ma... | 0 | 0 | ['Scala'] | 0 |
count-substrings-with-k-frequency-characters-i | Variable Sized Sliding Window Solution | variable-sized-sliding-window-solution-b-ljjh | Intuition: Find total of all substring and subtract it with total of unfit Substrings\n Describe your first thoughts on how to solve this problem. \n\n# Approac | ALOK_SRIVASTAVA | NORMAL | 2024-10-24T15:31:12.697760+00:00 | 2024-10-24T15:31:12.697793+00:00 | 5 | false | # Intuition: Find total of all substring and subtract it with total of unfit Substrings\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach: Approached the Problem using Sliding Window\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: 0(n)\n<!-- ... | 0 | 0 | ['Hash Table', 'Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Sliding Window | sliding-window-by-a90100-luju | 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 | a90100 | NORMAL | 2024-10-24T13:20:21.147022+00:00 | 2024-10-24T13:25:55.871235+00:00 | 4 | 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(26 * n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(c)\uFF0Cc is the types of English letters ... | 0 | 0 | ['JavaScript'] | 0 |
count-substrings-with-k-frequency-characters-i | Intuition from brute force to O(N) for all kinds of arrays | intuition-from-brute-force-to-on-for-all-6coj | Brute Force\nTime = O(N^2)\nSpace = O(26) for chars, O(N) for ints.\n\n##### Two Pointer 1\n\n1. Iterate over string while storing counts.\n2. While count of ch | antrixm | NORMAL | 2024-10-24T13:06:03.798300+00:00 | 2024-10-24T13:09:13.961445+00:00 | 17 | false | ##### Brute Force\nTime = O(N^2)\nSpace = O(26) for chars, O(N) for ints.\n\n##### Two Pointer 1\n\n1. Iterate over string while storing counts.\n2. While count of char == k, iterate while removing elements from left.\n3. For each such iteration, ans += n - r, since if for an index r the count is >=k, the condition wil... | 0 | 0 | ['Array', 'Two Pointers', 'Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | java solution using sliding window and hashmap | java-solution-using-sliding-window-and-h-7dzj | 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 | TanmayYadav | NORMAL | 2024-10-24T12:32:05.951432+00:00 | 2024-10-24T12:32:05.951464+00:00 | 1 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Java beats 100% | java-beats-100-by-lee216-2jbj | \n\n# Complexity\n- Time complexity:\nO(n)\n\n\n# Code\njava []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int l = 0, r = | lee216 | NORMAL | 2024-10-24T02:13:36.626675+00:00 | 2024-10-24T02:13:36.626735+00:00 | 6 | false | \n\n# Complexity\n- Time complexity:\nO(n)\n\n\n# Code\n```java []\nclass Solution {\n public int numberOfSubstrings(String s, int k) {\n int l = 0, r = 0, len = s.length(), res = 0;\n int freq[] = new int[26];\n while(r != len && l != len) {\n freq[s.charAt(r) - \'a\']++;\n ... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | [Accepted] Swift | accepted-swift-by-vasilisiniak-pjnu | \nclass Solution {\n func numberOfSubstrings(_ s: String, _ k: Int) -> Int {\n \n let s = Array(s)\n var ch = [s[0]: 1]\n var r = | vasilisiniak | NORMAL | 2024-10-23T20:28:57.287710+00:00 | 2024-10-23T20:28:57.287738+00:00 | 1 | false | ```\nclass Solution {\n func numberOfSubstrings(_ s: String, _ k: Int) -> Int {\n \n let s = Array(s)\n var ch = [s[0]: 1]\n var r = 0\n var res = 0\n\n for l in 0...s.count - k {\n if l > 0 {\n ch[s[l - 1]] = (ch[s[l - 1]]! > 0) ? (ch[s[l - 1]]! - ... | 0 | 0 | ['Swift'] | 0 |
count-substrings-with-k-frequency-characters-i | Java contest SOULTION beats 73% | java-contest-soultion-beats-73-by-qlob-h37t | 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 | Qlob | NORMAL | 2024-10-23T17:34:11.958347+00:00 | 2024-10-23T17:34:11.958384+00:00 | 4 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | That's Easy | thats-easy-by-y_v_singh-9d38 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nJust follow the provide | Y_V_Singh | NORMAL | 2024-10-23T17:26:59.541600+00:00 | 2024-10-23T17:26:59.541633+00:00 | 4 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nJust follow the provided hint\nThen hit the brute force\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:... | 0 | 0 | ['Hash Table', 'String', 'Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Intuitive Optimised Brute Force | intuitive-optimised-brute-force-by-vatad-yeyc | Intuition\n- Optimised Brute Force.\n\n# Approach\n- Started generating substring from index 0.\n- Stopped generating substring oncce any char freq = k\n- Added | vatadya | NORMAL | 2024-10-23T12:53:49.480101+00:00 | 2024-10-23T12:53:49.480138+00:00 | 2 | false | # Intuition\n- Optimised Brute Force.\n\n# Approach\n- Started generating substring from index 0.\n- Stopped generating substring oncce any char freq = k\n- Added remaing length of substring from that char to final result\n\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n... | 0 | 0 | ['Java'] | 0 |
count-substrings-with-k-frequency-characters-i | Python Solution (7 ms) Beats 95%🔥🔥🔥 | python-solution-7-ms-beats-95-by-abhigun-hsyo | Time Complexity: O(n)\n\n class Solution:\n def numberOfSubstrings(self, s: str, k: int) -> int:\n n = len(s)\n char_count = [0] | abhigunjal | NORMAL | 2024-10-23T08:50:17.335839+00:00 | 2024-10-23T08:50:17.335877+00:00 | 15 | false | **Time Complexity:** O(n)\n\n class Solution:\n def numberOfSubstrings(self, s: str, k: int) -> int:\n n = len(s)\n char_count = [0] * 26\n left = 0\n result = 0\n valid = 0 # Number of characters that have frequency >= k\n\n for right in rang... | 0 | 0 | ['Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | count substrings with k frequency characters using javascript | count-substrings-with-k-frequency-charac-13ky | Intuition\nGiven a string s and an integer k, return the total number of \nsubstrings of s where at least one character appears at least k times.\n\n# Approach\ | lucifer_300 | NORMAL | 2024-10-23T03:57:08.229470+00:00 | 2024-10-23T03:57:08.229504+00:00 | 4 | false | # Intuition\nGiven a string s and an integer k, return the total number of \nsubstrings of s where at least one character appears at least k times.\n\n# Approach\nrun nested loop to generate all possible substrings and then store them in hashmap on every iteration.\n\nif the count of the any character in substring equa... | 0 | 0 | ['JavaScript'] | 0 |
count-substrings-with-k-frequency-characters-i | Python (Simple Sliding Window) | python-simple-sliding-window-by-rnotappl-h25f | 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 | rnotappl | NORMAL | 2024-10-22T19:21:58.988625+00:00 | 2024-10-22T19:21:58.988657+00:00 | 2 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-substrings-with-k-frequency-characters-i | two pointers | two-pointers-by-user5285zn-0ei8 | We just keep a tight sliding window that contains a string that has just one character $k$ times.\n\nrust []\nimpl Solution {\n pub fn number_of_substrings(s | user5285Zn | NORMAL | 2024-10-22T15:31:26.096511+00:00 | 2024-10-22T15:31:26.096536+00:00 | 6 | false | We just keep a tight sliding window that contains a string that has just one character $k$ times.\n\n```rust []\nimpl Solution {\n pub fn number_of_substrings(s: String, k: i32) -> i32 {\n let s = s.chars().collect::<Vec<char>>();\n let mut total = 0;\n let mut i = 0;\n let mut a = vec![0... | 0 | 0 | ['Rust'] | 0 |
count-substrings-with-k-frequency-characters-i | Fast, Readable Solution - Sliding window | fast-readable-solution-sliding-window-by-oi36 | Intuition\nThe intuition behind the solution to count substrings that contain at least one character appearing at least k times revolves around the concept of m | chandroos | NORMAL | 2024-10-22T14:22:24.228779+00:00 | 2024-10-22T14:31:50.346346+00:00 | 3 | false | # Intuition\nThe intuition behind the solution to count substrings that contain at least one character appearing at least `k` times revolves around the concept of maintaining a dynamic window (or substring) and efficiently checking conditions as we expand and contract this window. Here\u2019s a breakdown of the key int... | 0 | 0 | ['Sliding Window', 'Java'] | 0 |
count-substrings-with-k-frequency-characters-i | C++ Sliding Window Solution | c-sliding-window-solution-by-baxi_darsh-jfzh | 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 | Baxi_Darsh | NORMAL | 2024-10-22T13:11:50.981695+00:00 | 2024-10-22T13:11:50.981736+00:00 | 16 | 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(26)\n<!-- Add your space complexity here, e.g. $$... | 0 | 0 | ['Sliding Window', 'C++'] | 0 |
count-subarrays-with-median-k | Balance | balance-by-votrubac-5j4w | A subarray has a median k if:\n- It includes k\n- Count n[i] < k is equal to count n[i] > k (odd-size subarrays).\n- Count n[i] < k is one less than count n[i] | votrubac | NORMAL | 2022-11-27T04:00:48.933669+00:00 | 2022-11-27T05:23:16.133064+00:00 | 7,858 | false | A subarray has a median `k` if:\n- It includes `k`\n- Count `n[i] < k` is equal to count `n[i] > k` (odd-size subarrays).\n- Count `n[i] < k` is one less than count `n[i] > k` (even-size subarrays).\n \nOr, in other words, the balance between the count of smaller and larger elements is zero or one.\n \nSince intege... | 195 | 1 | ['C'] | 31 |
count-subarrays-with-median-k | 🔥Python3🔥 Hashmap counting Explained O(n) | python3-hashmap-counting-explained-on-by-68tc | Intuition\n - We are looking for subarrays that contain k.\n - Elements are unique (from 1 to n).\n - Since the subarray has to contain k, and there is only one | MeidaChen | NORMAL | 2022-11-27T04:40:54.205153+00:00 | 2024-01-04T19:17:20.515255+00:00 | 2,812 | false | **Intuition**\n - We are looking for subarrays that contain ```k```.\n - Elements are unique (from 1 to n).\n - Since the subarray has to contain ```k```, and there is only one ```k```, we can start from ```k``` to build the subarrays.\n\nWhat makes a valid subarray (i.e., k as the median)?\n - the number of elements l... | 64 | 0 | [] | 6 |
count-subarrays-with-median-k | [Java/Python 3/C++] 1 pass O(n) codes: Count the prefix sum of the balance of (greater - samller). | javapython-3c-1-pass-on-codes-count-the-8g756 | Key observation: Within any subarray, the # of elements greater than median - the # of those less than median = 0 or 1.In order to guarantee the median k to pre | rock | NORMAL | 2022-11-27T04:00:51.228678+00:00 | 2025-04-05T17:08:02.373342+00:00 | 4,447 | false | **Key observation:** Within any subarray, the # of elements greater than median - the # of those less than median `= 0` or `1`.
In order to guarantee the median `k` to present in subarrays, we ONLY save into HashMap/dict the frequencies of the running balances BEFORE finding the median `k`. e.g.,
`nums = [7, 1, 3, 4... | 55 | 0 | ['Array', 'Hash Table', 'C', 'Prefix Sum', 'C++', 'Java', 'Python3'] | 7 |
count-subarrays-with-median-k | Simple Map !! | simple-map-by-megamind-2wwf | \n# Approach\n Describe your approach to solving the problem. \nSince we need to find the median and it depends only on the number of numbers less than k or gre | megamind_ | NORMAL | 2022-11-27T04:01:42.024671+00:00 | 2022-11-27T06:27:06.286612+00:00 | 2,543 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nSince we need to find the median and it depends only on the number of numbers less than k or greater than k. So we can change the array to 1 and -1 where nums[i]>k and nums[i]<k respectively. Now ,in order to get median ,we must include k in our sub... | 31 | 0 | ['C++'] | 5 |
count-subarrays-with-median-k | Python 3 ||| 7 lines, w/ example || T/S: 62% / 99% | python-3-7-lines-w-example-ts-62-99-by-s-7g3p | https://leetcode.com/problems/count-subarrays-with-median-k/submissions/1277527921/I could be wrong, but I think that time complexity is O(N) and space complexi | Spaulding_ | NORMAL | 2022-11-27T18:37:44.719704+00:00 | 2025-04-02T17:45:38.147863+00:00 | 542 | false | ```
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
d, iMed = defaultdict(int), nums.index(k) # Ex: [3, 2, 1, 4, 5] k = 4
ans, diff, d[0] = 0, 0, 1
# i n diff ans d
for i, n in enumerate(nums... | 17 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | ✅ C++ || Using Map || Easy Solution | c-using-map-easy-solution-by-indresh149-ncvc | \n\nclass Solution {\npublic:\n int segments(int n, vector<int> p, int m)\n {\n map<int, int> c;\n c[0] = 1;\n bool has = false;\n int sum = 0 | indresh149 | NORMAL | 2022-11-27T04:08:21.602213+00:00 | 2022-11-27T04:08:21.602258+00:00 | 2,094 | false | \n```\nclass Solution {\npublic:\n int segments(int n, vector<int> p, int m)\n {\n map<int, int> c;\n c[0] = 1;\n bool has = false;\n int sum = 0;\n long long ans = 0;\n for (int r = 0; r < n; r++) {\n \n // If element is less than m\n if (p[r] < m)\n sum--;\n \n ... | 15 | 1 | ['C'] | 2 |
count-subarrays-with-median-k | Easy map solution | easy-map-solution-by-sumitk7970-q4xy | Approach:\nFor a number to be median of an array, it must be included in the array and the count of numbers greater than it should be either equal to or 1 more | Sumitk7970 | NORMAL | 2022-11-27T04:03:21.354108+00:00 | 2022-11-27T18:33:34.449449+00:00 | 1,820 | false | **Approach:**\nFor a number to be median of an array, it must be included in the array and the count of numbers greater than it should be either equal to or 1 more than the count of numbers less than it.\n\n**C++ Code:**\n```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unorde... | 11 | 0 | ['C', 'Java'] | 1 |
count-subarrays-with-median-k | C++ | Counting | O(n) | Detailed Explaination | c-counting-on-detailed-explaination-by-u-79gx | Approach\n\nThe goal is to find the number of subarrays that have a median equal to $k$.\n\nLet center be the index of value $k$ (nums[center] == k), obviously | uier890305 | NORMAL | 2022-11-27T04:02:58.298473+00:00 | 2022-11-27T19:22:54.344982+00:00 | 1,079 | false | # Approach\n\nThe goal is to find the number of subarrays that have a median equal to $k$.\n\nLet `center` be the index of value $k$ (`nums[center] == k`), obviously `nums[center]` itself is a valid subarray, and let\'s expand the subarray to the left and right.\n\n- The key to have a median $= k$ is balancing the numb... | 11 | 0 | ['C++'] | 2 |
count-subarrays-with-median-k | Java Solution With Detailed Explanation | java-solution-with-detailed-explanation-ysgrb | \n# Background:\nIf K is the median of a sub array, then the subarray must contain K \n\nNow let\u2019s compute the SUM OF SUBARRAY as follows, for each element | profchi | NORMAL | 2022-11-27T04:02:02.271053+00:00 | 2022-11-27T04:02:02.271095+00:00 | 865 | false | \n# Background:\nIf K is the median of a sub array, then the subarray must contain K \n\nNow let\u2019s compute the SUM OF SUBARRAY as follows, for each element in the subarray greater than K, add one to the sum. For each element in the subarray less than K, subtract one from the sum.\nFor example suppose K = 4 and we ... | 9 | 0 | ['Java'] | 1 |
count-subarrays-with-median-k | [Python3] freq table | python3-freq-table-by-ye15-89zy | Please pull this commit for solutions of weekly 321. \n\nIntuition\nThe subarray whose median is k has to include k itself. So we construct our subarrays starti | ye15 | NORMAL | 2022-11-27T04:04:15.979910+00:00 | 2022-11-27T05:09:36.803205+00:00 | 1,223 | false | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/e728a9f475e5742bea7cf67ac2d1a98ab99fb206) for solutions of weekly 321. \n\n**Intuition**\nThe subarray whose median is `k` has to include `k` itself. So we construct our subarrays starting from where `k` is located. \nWe find the index of `k` (say ... | 8 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | ✅C++ || Hash Table | c-hash-table-by-chiikuu-ywn8 | 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# Co | CHIIKUU | NORMAL | 2023-03-18T11:16:02.241074+00:00 | 2023-03-18T11:16:02.241120+00:00 | 580 | false | # 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 long long help(int n,vector<int> v,int k){\n long long ans=0;\n long long crr=0;... | 6 | 0 | ['Array', 'Hash Table', 'C++'] | 0 |
count-subarrays-with-median-k | Short & Concise | C++ | short-concise-c-by-tusharbhart-phxt | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int, int> m;\n m[0] = 1;\n int found = 0, a | TusharBhart | NORMAL | 2022-11-27T12:58:27.443983+00:00 | 2022-11-27T12:58:27.444021+00:00 | 613 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int, int> m;\n m[0] = 1;\n int found = 0, ans = 0, s = 0;\n \n for(int i : nums) {\n if(i == k) found = 1;\n else s += i < k ? -1 : 1;\n\n if(found) ans ... | 5 | 0 | ['Hash Table', 'Prefix Sum', 'C++'] | 3 |
count-subarrays-with-median-k | C++ ✅✅| Easy Solution | Using Unordered Map 🔥| | c-easy-solution-using-unordered-map-by-k-bc5e | Code\n\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int, int> mp;\n | kunal0612 | NORMAL | 2022-11-27T05:19:12.557483+00:00 | 2022-11-27T05:19:12.557526+00:00 | 768 | false | # Code\n```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int, int> mp;\n mp[0] = 1;\n bool flag = false;\n int sum = 0;\n long long ans = 0;\n for (int i = 0; i < n; i++){\n if (nums[i] < k... | 5 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Number of subarrays with sum k | Breaks to standard Problem | number-of-subarrays-with-sum-k-breaks-to-pvtr | if median is k then u need equal number of elements on both sides ? \nthen ->\nfor every index i count the numbers greater than and less than k then store the d | njcoder | NORMAL | 2022-11-27T04:18:38.123449+00:00 | 2022-11-27T04:26:30.732826+00:00 | 830 | false | **if median is k then u** need equal number of elements on both sides ? \nthen ->\n**for every index i cou**nt the numbers greater than and less than k then store the difference (diff)\nthen search for diff and diff-1 why ?\ndiff case : suppose \ncntl = number of elements less than k \ncntr = number of elements greate... | 5 | 1 | [] | 2 |
count-subarrays-with-median-k | C++ || sub array sum || easy || trick | c-sub-array-sum-easy-trick-by-nishu_1234-gjv8 | Trick - turn every larger element to -1\nevery smaller element to 1\nevery equal element to some larger value (I have used 1e5 * 2)\n\nthen answer will be subse | nishu_1234567 | NORMAL | 2022-11-27T04:03:52.591217+00:00 | 2022-11-27T04:04:54.562670+00:00 | 909 | false | Trick - turn every larger element to -1\nevery smaller element to 1\nevery equal element to some larger value (I have used 1e5 * 2)\n\nthen answer will be subset sum (1e5 * 2 ) + subset sum(1e5 * 2 - 1) [ for even sized sub array]\n```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n ... | 5 | 0 | ['C'] | 2 |
count-subarrays-with-median-k | Similar to Subarray sum equal to k || Simple Solution || O(N) | similar-to-subarray-sum-equal-to-k-simpl-2rtd | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThis question is similar to Subarray sum equal to k.\n\nHere what we want | Krishna_Morker | NORMAL | 2024-08-26T13:21:20.521077+00:00 | 2024-08-26T13:21:20.521098+00:00 | 245 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis question is similar to Subarray sum equal to k.\n\nHere what we want is that k should be median. so for odd number of length of array we want that same number of eleement less then k and more then k should be present. s... | 4 | 0 | ['C++'] | 2 |
count-subarrays-with-median-k | [Python] O(n) Picture / Video Solution | python-on-picture-video-solution-by-chea-fgms | You can watch the video solution.\n\n# Conditions for valid subarray\n\n\n\n\nThe condition for a valid subarray:\n Must include K\n Difference in counts:\n\t F | cheatcode-ninja | NORMAL | 2022-12-01T17:09:43.537379+00:00 | 2022-12-01T17:26:54.963299+00:00 | 499 | false | You can watch the [video](https://youtu.be/oLEKpPXUgm4) solution.\n\n# Conditions for valid subarray\n\n\n\n\nThe condition for a valid subarray:\n* Must include `K`\n* Difference in counts:\n\t* For **Odd leng... | 4 | 0 | ['Python', 'Python3'] | 0 |
count-subarrays-with-median-k | Very easy✅ || Beginner friendly C++ solution using MAP || beats 💯 | very-easy-beginner-friendly-c-solution-u-lgpg | ```\nint countSubarrays(vector& p, int m) {\n map c;\n c[0] = 1;\n bool flag = false;\n int sum = 0;\n int n=p.size();\n long long ans = 0;\n | SuMiT_P13 | NORMAL | 2022-11-27T04:42:14.977395+00:00 | 2022-11-27T04:43:51.897440+00:00 | 736 | false | ```\nint countSubarrays(vector<int>& p, int m) {\n map<int, int> c;\n c[0] = 1;\n bool flag = false;\n int sum = 0;\n int n=p.size();\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n \n \n if (p[i] < m)\n sum--;\n else if (p[i] > m)\n sum++;\n if... | 4 | 0 | ['C'] | 3 |
count-subarrays-with-median-k | Python || prefix sum || O(n) | python-prefix-sum-on-by-hanna9221-cii2 | First we transform nums into an array of -1, 0, 1: \nn < k to -1, n == k to 0, n > k to 1.\nFor example, nums = [5,1,3,4,2], k = 4 -> arr = [1,-1,-1,0,-1]\n\nFo | hanna9221 | NORMAL | 2022-11-27T04:03:24.671301+00:00 | 2022-11-27T04:16:05.487002+00:00 | 1,991 | false | First we transform `nums` into an array of -1, 0, 1: \nn < k to -1, n == k to 0, n > k to 1.\nFor example, `nums` = [5,1,3,4,2], k = 4 -> arr = [1,-1,-1,0,-1]\n\nFor a subarray with sum equals to 0, number of {n: n < k} = number of {n: n > k}.\nFor a subarray with sum equals to 1, number of {n: n < k} = number of {n: n... | 4 | 0 | ['Python3'] | 1 |
count-subarrays-with-median-k | Java || PrefixSum of -1, 0,1 || Total - left - right || Commented | java-prefixsum-of-1-01-total-left-right-hu5nm | find index of k, and convert array into -1, 0, 1\n2. calculate the prefix Sum\n3. calculate three range: total [0, n - 1] - left[0, idxK - 1] - right[idxK + 1 | cooper-- | NORMAL | 2022-11-27T04:02:03.235197+00:00 | 2022-11-27T04:02:44.692847+00:00 | 1,644 | false | 1. find index of k, and convert array into -1, 0, 1\n2. calculate the prefix Sum\n3. calculate three range: total [0, n - 1] - left[0, idxK - 1] - right[idxK + 1]; ( exclude case without `k`)\n```\nclass Solution {\n public int countSubarrays(int[] nums, int k) {\n // 1. find index of k, and convert array i... | 4 | 1 | ['Prefix Sum', 'Java'] | 0 |
count-subarrays-with-median-k | C++ | Easy | c-easy-by-pradeep-hjhf | Intution : \n# First find the index of the element k \t\t \n# \nNow create left array to store the number which may be the part of the left and tells th | pradeep_ | NORMAL | 2022-11-27T04:01:33.484529+00:00 | 2022-11-27T04:16:15.536431+00:00 | 566 | false | # **Intution : **\n# First find the index of the element k \t\t \n# \nNow create left array to store the number which may be the part of the left and tells the position of number \'k\' from the increment for the number which are less than \'k\' and decrement for the number which are greater than \'k\' \... | 4 | 0 | ['Binary Search'] | 0 |
count-subarrays-with-median-k | C++ solution using HashMap | c-solution-using-hashmap-by-sachin_kumar-d17l | 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 | Sachin_Kumar_Sharma | NORMAL | 2024-04-14T06:49:17.998974+00:00 | 2024-04-14T06:49:17.999027+00:00 | 21 | 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(N)\n<!-- Add your space complexity here, e.g. $... | 3 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | C++ | prefix sum | O(N) | explanation | c-prefix-sum-on-explanation-by-milochen-igg1 | \nIn the following source code, \ncountSol(vk,q,0) will figure out all possible odd size subarray.\ncountSol(vk,q,-1) will fiture out all possible even size sub | milochen | NORMAL | 2022-11-27T14:47:55.526998+00:00 | 2022-11-27T14:54:40.729164+00:00 | 1,058 | false | \nIn the following source code, \ncountSol(vk,q,0) will figure out all possible odd size subarray.\ncountSol(vk,q,-1) will fiture out all possible even size subarray.\nFor example, \nInput: nums = [3,2,1,4,5], k = 4\nOutput: 3\nThe subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].\nThe cases [4] & [1... | 3 | 0 | ['C', 'Prefix Sum'] | 0 |
count-subarrays-with-median-k | ✅ [Python] Multiply diff counts around median | O(n) Solution w/ Explanation | python-multiply-diff-counts-around-media-ydat | Intuition\nBrute Force - O(n^3)\nYou should always start by coming up with the brute force solution. In this case, we can check every possible subarray, count t | takanuva15 | NORMAL | 2022-11-27T04:01:57.236402+00:00 | 2022-11-27T13:41:17.651601+00:00 | 401 | false | # Intuition\n**Brute Force - O(n^3)**\nYou should always start by coming up with the brute force solution. In this case, we can check every possible subarray, count the number of elements above/below the median, and check that the median exists in the subarray. As long as there are the same number of elements above/bel... | 3 | 0 | ['Python3'] | 1 |
count-subarrays-with-median-k | [Javascript] Count Left & Right Balance | javascript-count-left-right-balance-by-a-aaiz | Solution: Count Left & Right Balance\n\nFor a subarray to have median of k:\n 1. The subarray must contain k.\n 2. k must be the middle element (if odd) or lo | anna-hcj | NORMAL | 2022-11-27T04:00:53.965300+00:00 | 2022-11-27T04:00:53.965341+00:00 | 386 | false | **Solution: Count Left & Right Balance**\n\nFor a subarray to have median of `k`:\n 1. The subarray must contain `k`.\n 2. `k` must be the middle element (if odd) or lower mid element (if even).\n\nCreate subarrays revolving around `nums[i] = k`.\n\nHow do we find whether `k` is the mid/lower mid element?\n* Starti... | 3 | 0 | ['JavaScript'] | 2 |
count-subarrays-with-median-k | 2 Sum || Prefix & Suffix || O(N) | 2-sum-prefix-suffix-on-by-dragonzz-398v | Intuition\nWe can reduce this problem to a 2Sum after pre-processing \n\n# Approach\n\nlet idx be the index of k in nums\n1. find the prefix array where pre[i]= | dragonzz | NORMAL | 2022-11-27T04:00:52.305254+00:00 | 2022-11-27T04:06:52.795597+00:00 | 668 | false | # Intuition\nWe can reduce this problem to a 2Sum after pre-processing \n\n# Approach\n\nlet idx be the index of k in nums\n1. find the prefix array where pre[i]= # of element > k - # of element < k in num[i:idx]\n2. find the suffix array where suf[i]= # of element > k - # of element < k in num[idx:i]\n\nif pre[i] + su... | 3 | 0 | ['Python3'] | 1 |
count-subarrays-with-median-k | o(N) | on-by-12345556-5y2c | 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 | 12345556 | NORMAL | 2023-07-23T18:53:34.886916+00:00 | 2023-07-23T18:53:34.886969+00:00 | 83 | 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 | ['C++'] | 0 |
count-subarrays-with-median-k | C++|| Easy Map Solution Using Simple Balance | c-easy-map-solution-using-simple-balance-hmod | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n map<int,int> mp;\n int cnt=0;// no of elements\n int i=0,n=n | Arko-816 | NORMAL | 2022-12-29T05:43:46.559355+00:00 | 2022-12-29T06:24:14.413416+00:00 | 218 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n map<int,int> mp;\n int cnt=0;// no of elements\n int i=0,n=nums.size();\n bool found=false;\n mp[0]=1;\n int ans=0;\n while(i<n)\n {\n if(nums[i]<k)//elements left of k we... | 2 | 0 | [] | 0 |
count-subarrays-with-median-k | [C++] Transform the input array and use the approach of Target sum problem | c-transform-the-input-array-and-use-the-08n3y | Steps:\n1) First transform the input array -> set the nums[i] == k to 0; nums[i] < k to -1; nums[i] > k to 1\n So for example: nums = [3,2,1,4,5], k = 4; nu | samore65 | NORMAL | 2022-11-27T19:31:12.833632+00:00 | 2022-11-27T19:35:28.699376+00:00 | 276 | false | Steps:\n1) First transform the input array -> set the nums[i] == k to 0; nums[i] < k to -1; nums[i] > k to 1\n So for example: nums = [3,2,1,4,5], k = 4; nums -> [-1,-1,-1,0,1]\n2) Start a cummulative sum of the transformed input array, from the k value element and store the cummulative sum\'s freq in a map\n\t mp[... | 2 | 0 | ['C'] | 0 |
count-subarrays-with-median-k | Prefix for greater and lesser elements (Balancing) | prefix-for-greater-and-lesser-elements-b-8tcv | \n Find the index of k which is pivot\n compute the prefix sum for greater and lesser elements of k (count of greater and lesser elements than k)\n First check | NaveenprasanthSA | NORMAL | 2022-11-27T10:53:45.462070+00:00 | 2022-11-27T11:01:17.132045+00:00 | 443 | false | \n* Find the index of k which is pivot\n* compute the prefix sum for greater and lesser elements of k (count of greater and lesser elements than k)\n* First check the right side of the pivot\n* If the difference number of greater and lesser elements is equal or 1 , we can consider that subarray will have the median as ... | 2 | 0 | ['Python'] | 1 |
count-subarrays-with-median-k | Explanation | explanation-by-iyershridhar-5qh5 | Tricky Subarray counting !!!!!\n\n\n\n\n\n\nclass Solution {\npublic:\n int subarray(vector<int> &arr , int k , int index){\n map<int , int> mp;\n | iyershridhar | NORMAL | 2022-11-27T05:52:21.906825+00:00 | 2022-11-27T05:52:45.964381+00:00 | 180 | false | # Tricky Subarray counting !!!!!\n\n\n of both side.\nNow assume | Jason0704 | NORMAL | 2022-11-27T04:07:30.341643+00:00 | 2022-11-27T14:10:03.982928+00:00 | 391 | false | **Idea**\nLet `large` be the count of larger numbers and `small` be the count of smaller numbers. \nCalculate the `difference` (= large - small) of both side.\nNow assume at the index i, we have difference = 2, which means we have 2 more "largers" than "smallers".\nSo what we only need to do, is to find the count of di... | 2 | 0 | ['C'] | 0 |
count-subarrays-with-median-k | [Python3] Counting O(N) With Explanations | python3-counting-on-with-explanations-by-1vvd | Hint1: We record 1 if num > k; -1 if num < k, and 0 if num == k.\n\nHint2: Let pos denote the index of k in nums, where k is the median. Then the problem is equ | xil899 | NORMAL | 2022-11-27T04:01:16.531939+00:00 | 2022-11-27T21:12:44.825562+00:00 | 426 | false | **Hint1:** We record 1 if `num > k`; -1 if `num < k`, and 0 if `num == k`.\n\n**Hint2:** Let `pos` denote the index of `k` in `nums`, where `k` is the median. Then the problem is equivalent to:\n1. `pos` is in the indices of the subarray;\n2. the sum of the subarray is either `0` or `1`.\n\n**Hint3:** Let `c_i` be the ... | 2 | 0 | ['Python', 'Python3'] | 0 |
count-subarrays-with-median-k | [C++] Prefix Sum, Count, O(n), short | c-prefix-sum-count-on-short-by-kevin1010-0yfv | First, find the index of k. \n\nConstruct 2 prefix sum, the sum equals to the number of integers larger than k minus the number of integers less than k.\n\nThe | kevin1010607 | NORMAL | 2022-11-27T04:00:51.764581+00:00 | 2022-11-27T04:00:51.764628+00:00 | 593 | false | First, find the index of `k`. \n\nConstruct 2 prefix sum, the sum equals to the number of integers larger than `k` minus the number of integers less than `k`.\n\nThe first prefix sum `L` is from the index of `k` to `0`.\nThe second prefix sum `R` is from the index of `k` to `n-1`.\nFor example: `nums = [3,2,1,4,5]`, `... | 2 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | ✅Beginner Friendly 🔥|| Step By Steps Solution ✅ || HashMap Approach ✅🔥 | beginner-friendly-step-by-steps-solution-lt1t | Code | rajput11dynamic | NORMAL | 2025-01-16T18:43:09.295009+00:00 | 2025-01-16T18:43:09.295009+00:00 | 106 | false | 
# Code
```java []
class Solution {
public int countSubarrays(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int c = 0, i = 0;
boolea... | 1 | 1 | ['Java'] | 0 |
count-subarrays-with-median-k | clean code...using subarray sum equal k logic...without finding index of k | clean-codeusing-subarray-sum-equal-k-log-xcue | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | niteshsaxena03 | NORMAL | 2025-01-02T15:34:08.839953+00:00 | 2025-01-02T15:34:08.839953+00:00 | 66 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | [Java] Easy 100% solution | java-easy-100-solution-by-ytchouar-4x72 | java\nclass Solution {\n public int countSubarrays(final int[] nums, final int k) {\n final int n = nums.length;\n final int[] counts = new int | YTchouar | NORMAL | 2024-11-04T04:58:13.298727+00:00 | 2024-11-04T04:58:13.298764+00:00 | 78 | false | ```java\nclass Solution {\n public int countSubarrays(final int[] nums, final int k) {\n final int n = nums.length;\n final int[] counts = new int[n * 2 + 1];\n\n int count = 0, result = 1, pos = 0;\n\n for(int i = 0; i < n; ++i)\n if(nums[i] == k)\n pos = i;\n\n... | 1 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Easy java solution with intuition , approach and description | easy-java-solution-with-intuition-approa-6o8l | Intuition and Approach\n\nThe problem requires counting the number of subarrays where the median equals a given integer k. A median is the middle element in a s | workwithracian | NORMAL | 2024-07-01T09:14:54.339143+00:00 | 2024-07-01T09:14:54.339178+00:00 | 23 | false | ### Intuition and Approach\n\nThe problem requires counting the number of subarrays where the median equals a given integer `k`. A median is the middle element in a sorted array, so the goal is to identify subarrays where `k` is the middle element.\n\nThe approach is based on prefix sums and the idea of balancing count... | 1 | 0 | ['Python', 'C++', 'Java'] | 0 |
count-subarrays-with-median-k | C++ || Beginner Friendly || Full explanation || Easy | c-beginner-friendly-full-explanation-eas-i9us | \n class Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n //valid subarrays will be starting from 0 to i where i being the index | 1821_sank | NORMAL | 2023-09-21T09:05:49.937914+00:00 | 2023-09-21T09:05:49.937963+00:00 | 610 | false | ```\n class Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n //valid subarrays will be starting from 0 to i where i being the index of k and ending at i to n\n //suppose i foudn a valid subarray between s and e\n //numbers greater than k between s to i be g1\n //numb... | 1 | 0 | ['C', 'Prefix Sum'] | 0 |
count-subarrays-with-median-k | C++ | Hints Solution | With Comments | c-hints-solution-with-comments-by-ama29n-w8gy | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n for(auto& it : nums) {\n it = | ama29n | NORMAL | 2023-02-24T15:13:39.528442+00:00 | 2023-02-24T15:13:39.528473+00:00 | 107 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n for(auto& it : nums) {\n it = it == k ? 0 : it > k ? 1 : -1;\n }\n int idx;\n for(auto i = 0; i < n; i++) {\n if(nums[i] == 0) {\n idx = i; ... | 1 | 0 | ['Hash Table', 'Prefix Sum'] | 0 |
count-subarrays-with-median-k | Python | Counter | O(n) | python-counter-on-by-aryonbe-nk2f | Code\n\nfrom collections import Counter\nclass Solution:\n def countSubarrays(self, nums: List[int], k: int) -> int:\n n = len(nums)\n i = nums | aryonbe | NORMAL | 2022-12-19T08:45:06.085055+00:00 | 2022-12-19T08:45:06.085097+00:00 | 71 | false | # Code\n```\nfrom collections import Counter\nclass Solution:\n def countSubarrays(self, nums: List[int], k: int) -> int:\n n = len(nums)\n i = nums.index(k)\n lcounter = Counter()\n cursum = 0\n lcounter[0] = 1\n for j in range(i-1,-1,-1):\n cursum += -1 if nums[... | 1 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | Java || Easy Solution || Using HashMap | java-easy-solution-using-hashmap-by-maaw-2j30 | \nclass Solution {\n public int countSubarrays(int[] arr, int k) {\n HashMap<Integer , Integer> map = new HashMap<>();\n int index = -1;\n | MaawanAhmad | NORMAL | 2022-12-08T13:47:51.520292+00:00 | 2022-12-08T13:47:51.520325+00:00 | 60 | false | ```\nclass Solution {\n public int countSubarrays(int[] arr, int k) {\n HashMap<Integer , Integer> map = new HashMap<>();\n int index = -1;\n for(int i = 0 ; i < arr.length ; i++){\n if(arr[i] == k){\n index = i;\n break;\n }\n }\n ... | 1 | 0 | [] | 1 |
count-subarrays-with-median-k | C++| MAP | O(N) | c-map-on-by-kumarabhi98-vjnp | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int> mp; mp[0]++;\n int sum = 0, in = 0, re = | kumarabhi98 | NORMAL | 2022-11-27T09:54:38.419623+00:00 | 2022-11-27T09:54:38.419668+00:00 | 30 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int> mp; mp[0]++;\n int sum = 0, in = 0, re = 0;\n while(in<nums.size() && nums[in]!=k) in++;\n for(int i = in-1; i>=0;i--) {\n if(nums[i]>k) sum++; else sum--;\n mp[s... | 1 | 0 | ['C'] | 0 |
count-subarrays-with-median-k | ✅Using HashMap || Easy C++ Solution | using-hashmap-easy-c-solution-by-ramkish-l5j5 | Please upvote if you like.\nApproach\n\nLogic is that k would be median of a subarray if the count of smaller and greater value on the left side is 1 more than | ramkishanteli2 | NORMAL | 2022-11-27T07:00:22.450536+00:00 | 2022-11-27T07:00:22.450575+00:00 | 70 | false | **Please upvote if you like.**\n**Approach**\n\nLogic is that k would be median of a subarray if the count of smaller and greater value on the left side is 1 more than the count of smaller and greater value on the right side.\n\n\nWe find the count of the smaller and greater value then the median k on the right side an... | 1 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.