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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
shortest-distance-to-a-character | best approach JS | best-approach-js-by-javad_pk-yrvk | 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 | javad_pk | NORMAL | 2024-05-23T19:07:51.736847+00:00 | 2024-05-23T19:28:01.462363+00:00 | 407 | 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 |
shortest-distance-to-a-character | Easy java solution with explanation ✅🚀🎯 | easy-java-solution-with-explanation-by-v-b7o7 | Intuition\nThe problem requires finding the shortest distance from each character in the string to a given character \'c\'. We can observe that the shortest dis | vidojevica | NORMAL | 2024-04-19T21:35:55.397467+00:00 | 2024-04-19T21:35:55.397490+00:00 | 971 | false | # Intuition\nThe problem requires finding the shortest distance from each character in the string to a given character \'c\'. We can observe that the shortest distance to \'c\' for any character is the minimum of the distances to \'c\' on its left and right sides.\n\n# Approach\n1. First, we find all positions of char... | 2 | 0 | ['Array', 'String', 'Java'] | 0 |
shortest-distance-to-a-character | Go O(N) | go-on-by-aspic-xpyz | \n# Code\n\nfunc shortestToChar(s string, c byte) []int {\n\twalkLeftToRight, cPos := make([]int, len(s)), -len(s)\n\tfor i := range s {\n\t\tif s[i] == c {\n\t | aspic | NORMAL | 2023-12-19T08:16:00.212409+00:00 | 2023-12-19T08:16:00.212430+00:00 | 135 | false | \n# Code\n```\nfunc shortestToChar(s string, c byte) []int {\n\twalkLeftToRight, cPos := make([]int, len(s)), -len(s)\n\tfor i := range s {\n\t\tif s[i] == c {\n\t\t\tcPos = i\n\t\t} else {\n\t\t\twalkLeftToRight[i] = i - cPos\n\t\t}\n\t}\n\tfor r := len(s) - 1; r >= 0; r-- {\n\t\tif s[r] == c {\n\t\t\tcPos = r\n\t\t} ... | 2 | 0 | ['Go'] | 0 |
shortest-distance-to-a-character | explain with step wise dry run ... | explain-with-step-wise-dry-run-by-anshik-z81x | \n\n# Approach\nStep 1: Finding the positions of character \'e\' in the string \'loveleetcode\'\nThe positions of \'e\' are: [3, 5, 6, 11]\nStep 2: Calculating | Anshika_73_ | NORMAL | 2023-12-02T08:35:20.256869+00:00 | 2023-12-02T08:35:20.256896+00:00 | 417 | false | \n\n# Approach\nStep 1: Finding the positions of character \'e\' in the string \'loveleetcode\'\nThe positions of \'e\' are: [3, 5, 6, 11]\nStep 2: Calculating the distance from each character to the nearest occurrence of \'e\'\nFor each character in the string, calculate the minimum distance to any of the positions ob... | 2 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Python | Easy | Array | Shortest Distance to a Character | python-easy-array-shortest-distance-to-a-wcg4 | \nsee the Successfully Accepted Submission\nPython\nclass Solution(object):\n def shortestToChar(self, s, c):\n """\n :type s: str\n :ty | Khosiyat | NORMAL | 2023-10-05T14:55:06.195051+00:00 | 2023-10-05T14:55:06.195069+00:00 | 514 | false | \n[see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/940703040/)\n```Python\nclass Solution(object):\n def shortestToChar(self, s, c):\n """\n :type s: str\n :type c: str\n :rtype: List[int]\n """ \n lengthOf_string = len(s)\n result = ... | 2 | 0 | ['Array', 'Python'] | 0 |
shortest-distance-to-a-character | Best Java Solution || Beats 60% | best-java-solution-beats-60-by-ravikumar-e2vx | 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 | ravikumar50 | NORMAL | 2023-09-12T08:57:06.452796+00:00 | 2023-09-12T08:57:06.452825+00:00 | 1,548 | 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 | ['Java'] | 0 |
shortest-distance-to-a-character | Easy Javascript Solution | easy-javascript-solution-by-chetannada-5ttr | 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 | chetannada | NORMAL | 2023-07-14T16:48:52.094428+00:00 | 2023-07-14T16:48:52.094451+00:00 | 115 | 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 |
shortest-distance-to-a-character | Solution | solution-by-deleted_user-b7ts | C++ []\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int n = s.length();\n\tvector<int>v;\n\tfor (int i = 0;i < n;i++) | deleted_user | NORMAL | 2023-05-03T12:33:15.583720+00:00 | 2023-05-03T13:35:27.617836+00:00 | 1,354 | false | ```C++ []\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int n = s.length();\n\tvector<int>v;\n\tfor (int i = 0;i < n;i++) {\n\t\tint distance = 0;\n\t\tfor (int j = i, k = i;j < n || k >= 0;j++, k--) {\n\t\t\tif ((k >= 0 && s[k] == c) || (j < n && s[j] == c )) {\n\t\t\t\tv.push... | 2 | 0 | ['C++', 'Java', 'Python3'] | 1 |
shortest-distance-to-a-character | C++ Unique Solution (Beats 100%) | c-unique-solution-beats-100-by-daghansu-91mv | 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 | daghanSU | NORMAL | 2023-03-19T22:00:50.606446+00:00 | 2023-03-19T22:00:50.606488+00:00 | 519 | 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 |
shortest-distance-to-a-character | Easy C++ Solution | easy-c-solution-by-clary_shadowhunters-uoj1 | \n# Code\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) \n {\n vector<int>ans;\n vector<int>temp;\n for | Clary_ShadowHunters | NORMAL | 2023-03-12T09:37:56.753617+00:00 | 2023-03-12T09:37:56.753650+00:00 | 1,462 | false | \n# Code\n```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) \n {\n vector<int>ans;\n vector<int>temp;\n for (int i=0;i<s.size();i++)\n {\n if (s[i]==c)\n temp.push_back(i);\n }\n int ind1=0;\n int n=temp.size();\n ... | 2 | 0 | ['C++'] | 1 |
shortest-distance-to-a-character | Best Explained Solution | best-explained-solution-by-darian-catali-u69b | \n\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int len = s.length();\n int ans[] = new int[len];\n int prev = | darian-catalin-cucer | NORMAL | 2023-02-06T04:44:09.286577+00:00 | 2023-02-06T04:44:09.286623+00:00 | 817 | false | \n```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int len = s.length();\n int ans[] = new int[len];\n int prev = len;\n \n // forward\n for(int i = 0; i < len; i++){\n if(s.charAt(i) == c){\n prev = 0;\n ans[... | 2 | 1 | ['Go', 'Scala', 'Ruby', 'Kotlin', 'Bash'] | 0 |
shortest-distance-to-a-character | C++ solution for beginners | c-solution-for-beginners-by-shristha-yyhy | \n\n# Code\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>ispresent;\n for(int i=0;i<s.length();i++ | Shristha | NORMAL | 2023-01-23T17:13:50.714266+00:00 | 2023-01-23T17:13:50.714337+00:00 | 1,453 | false | \n\n# Code\n```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>ispresent;\n for(int i=0;i<s.length();i++){\n if(s[i]==c)ispresent.push_back(i);\n }\n vector<int>ans(s.length());\n for(int i=0;i<s.length();i++){\n if(s[... | 2 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Shortest distance to a Character | shortest-distance-to-a-character-by-bibe-yi5p | Intuition\n Describe your first thoughts on how to solve this problem. two traversals from each end..and then assigning the minimum of them both!\n\n# Approach\ | bibek_codes | NORMAL | 2022-12-06T07:20:58.384197+00:00 | 2022-12-06T07:20:58.384231+00:00 | 59 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->two traversals from each end..and then assigning the minimum of them both!\n\n# Approach\n<!-- Describe your approach to solving the problem. -->Two loops taken from left and right traversal assigning distance acc. to the closest occurence ... | 2 | 0 | ['Two Pointers', 'String', 'C++'] | 0 |
shortest-distance-to-a-character | Runs 100% Faster Cpp soln | O(N) | runs-100-faster-cpp-soln-on-by-mk28r-xn7t | ```cpp\nclass Solution {\npublic:\n vector shortestToChar(string s, char c) {\n int n = s.size();\n vector ans(s.size());\n int last = - | TIME2LIVE | NORMAL | 2022-10-26T19:35:46.709053+00:00 | 2022-10-26T19:35:46.709090+00:00 | 23 | false | ```cpp\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int n = s.size();\n vector<int> ans(s.size());\n int last = -n ;\n for(int i = 0 ; i < n ;i++){\n if(s[i]==c)\n last= i ; \n \n ans[i]=(i-last);\n }\n ... | 2 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | [Python] Simplest Solution | python-simplest-solution-by-darpangoel07-n5lf | \n\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n temp = []\n ind = s.index(c)\n for i in range(len(s)):\n | darpangoel07 | NORMAL | 2022-09-18T10:12:12.842093+00:00 | 2022-09-18T10:13:20.934598+00:00 | 987 | false | ```\n\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n temp = []\n ind = s.index(c)\n for i in range(len(s)):\n if abs(ind-i)>abs(s.find(c,i)-i):\n ind = s.index(c,i)\n if s[i]!=c:\n temp.append(abs(ind-i))\n ... | 2 | 0 | ['Python'] | 0 |
shortest-distance-to-a-character | [C++] Fast and Easy iterative solution | c-fast-and-easy-iterative-solution-by-cy-ipaz | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int fdist=INT_MAX,bdist=INT_MAX;\n vector<int>ans(s.size(),INT_M | cyberReckon | NORMAL | 2022-09-12T09:59:00.286802+00:00 | 2022-09-12T09:59:00.286834+00:00 | 389 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int fdist=INT_MAX,bdist=INT_MAX;\n vector<int>ans(s.size(),INT_MAX);\n for(int i=0,j=s.size()-1;i<s.size();++i,--j)\n {\n if(s[i]==c) fdist=0;\n if(s[j]==c) bdist=0;\n \n ... | 2 | 0 | ['C', 'Iterator'] | 0 |
shortest-distance-to-a-character | JS easy solution 100% | js-easy-solution-100-by-kunkka1996-s1hi | \nvar shortestToChar = function(s, c) {\n const indexOfC = [];\n const output = [];\n \n for (let i = 0; i < s.length; i++) {\n if (s[i] === | kunkka1996 | NORMAL | 2022-09-09T08:12:28.965274+00:00 | 2022-09-09T08:12:28.965313+00:00 | 909 | false | ```\nvar shortestToChar = function(s, c) {\n const indexOfC = [];\n const output = [];\n \n for (let i = 0; i < s.length; i++) {\n if (s[i] === c) {\n indexOfC.push(i)\n }\n }\n \n for (let i = 0; i < s.length; i++) {\n output[i] = +Infinity;\n for (let j = 0;... | 2 | 0 | ['JavaScript'] | 0 |
shortest-distance-to-a-character | O(1) Space Prefix / Suffix With Comments 🔥 | o1-space-prefix-suffix-with-comments-by-mpr7v | \n# Python\n def shortestToChar(self, s: str, c: str) -> List[int]:\n\t """\n Q. Min/Max distance to closest character [left or right]\n \n | xxvvpp | NORMAL | 2022-09-05T07:41:52.700716+00:00 | 2022-09-05T07:42:16.042674+00:00 | 337 | false | \n# Python\n def shortestToChar(self, s: str, c: str) -> List[int]:\n\t """\n Q. Min/Max distance to closest character [left or right]\n \n Character can be present to left or right both\n \n 1. Trace from left to right & count distance from last char found\n 2. Trace fr... | 2 | 0 | ['Python'] | 0 |
shortest-distance-to-a-character | Easiest Approach 5 ms || with Explanation | easiest-approach-5-ms-with-explanation-b-8bxb | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>sk;\n vector<int>res;\n vector<int>temp;\n | sagarkesharwnnni | NORMAL | 2022-08-07T18:27:29.175120+00:00 | 2022-08-07T18:28:09.504025+00:00 | 400 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>sk;\n vector<int>res;\n vector<int>temp;\n int ss=0;\n int mins;\n for(int i=0;i<s.length();i++){\n if(s[i]==c)\n\t\t\t//storing all the c character location i.e \'e\' in ... | 2 | 0 | ['C'] | 0 |
shortest-distance-to-a-character | C++ O(n) solution easy to understand | c-on-solution-easy-to-understand-by-ayum-iwpm | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c)\n {\n int a=s.length();\n int arr1[a];\n int arr2[a];\n | ayumsh | NORMAL | 2022-07-14T13:13:23.347182+00:00 | 2022-07-14T13:13:23.347234+00:00 | 60 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c)\n {\n int a=s.length();\n int arr1[a];\n int arr2[a];\n for(int i=0;i<s.length();i++)\n {\n arr1[i]=100000;\n arr2[i]=100000;\n }\n int occur1;\n int flag1=0;... | 2 | 0 | ['C'] | 0 |
shortest-distance-to-a-character | Python3 O(n) || O(n) # Runtime: 53ms 78.92% Memory: 13.9mb 91.60% | python3-on-on-runtime-53ms-7892-memory-1-1orf | \nclass Solution:\n def shortestToChar(self, string: str, char: str) -> List[int]:\n return self.optimalSolution(string, char)\n# O(n) || O(n)\n# | arshergon | NORMAL | 2022-06-22T16:16:20.076274+00:00 | 2022-06-22T16:16:20.076312+00:00 | 1,159 | false | ```\nclass Solution:\n def shortestToChar(self, string: str, char: str) -> List[int]:\n return self.optimalSolution(string, char)\n# O(n) || O(n)\n# Runtime: 53ms 78.92% Memory: 13.9mb 91.60%\n def optimalSolution(self, string, char):\n n = len(string)\n leftArray, rightArray, result = ([... | 2 | 0 | ['Two Pointers', 'Python', 'Python3'] | 0 |
shortest-distance-to-a-character | Nice Solution in C++ | nice-solution-in-c-by-dev-madhurendra-grbl | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int> ans;\n int n = s.size();\n for(int i=0;i<n;++ | dev-madhurendra | NORMAL | 2022-05-31T15:05:54.885080+00:00 | 2022-05-31T15:05:54.885126+00:00 | 93 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int> ans;\n int n = s.size();\n for(int i=0;i<n;++i){\n if(s[i]==c){\n ans.push_back(0);\n }\n else{\n int st = i;\n int en = i;\... | 2 | 0 | [] | 0 |
shortest-distance-to-a-character | java || too easy solution | java-too-easy-solution-by-pratham_18-lpnj | //do upvote :)\n\n\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int ans[]=new int[s.length()];\n ArrayList<Integer> li | Pratham_18 | NORMAL | 2022-05-10T11:51:50.451195+00:00 | 2022-05-10T11:51:50.451219+00:00 | 153 | false | //do upvote :)\n\n```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int ans[]=new int[s.length()];\n ArrayList<Integer> li=new ArrayList<>();\n //Store the index of the character\n for(int i=0;i<s.length();i++){\n if(s.charAt(i)==c){\n li.... | 2 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | Shortest Distance to a Character | shortest-distance-to-a-character-by-sbar-h6om | \nvector<int> shortestToChar(string s, char c) {\n vector<int> index;\n vector<int> ans;\n int k1 = 0, k2 = 1;\n for(int i = 0; i < | sbaraniya01 | NORMAL | 2022-04-26T15:39:33.940854+00:00 | 2022-04-26T15:39:33.940881+00:00 | 209 | false | ```\nvector<int> shortestToChar(string s, char c) {\n vector<int> index;\n vector<int> ans;\n int k1 = 0, k2 = 1;\n for(int i = 0; i < s.length(); i++)\n {\n if(s[i] == c)\n {\n index.push_back(i);\n }\n }\n index.push_back... | 2 | 0 | ['C'] | 0 |
shortest-distance-to-a-character | Easy To Understand Java Code | easy-to-understand-java-code-by-riteshbh-mpx4 | \nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int min=Integer.MAX_VALUE;\n int[] array=new int[s.length()];\n f | riteshbhardwaj0001 | NORMAL | 2022-02-02T09:30:51.242691+00:00 | 2022-02-02T09:30:51.242721+00:00 | 89 | false | ```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int min=Integer.MAX_VALUE;\n int[] array=new int[s.length()];\n for(int i=0;i<s.length();i++){\n for(int j=0;j<s.length();j++){\n if(s.charAt(j)==c){\n min=Math.min(min,Math.abs... | 2 | 0 | [] | 1 |
shortest-distance-to-a-character | (Beats 100% Runtime) Optimal 2-Pass in Python | beats-100-runtime-optimal-2-pass-in-pyth-ec1j | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n #Time: O(n)\n #Space: O(n)\n \n #Represents how far | surin_lovejoy | NORMAL | 2022-01-25T09:43:36.336980+00:00 | 2022-01-25T09:46:37.152252+00:00 | 190 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n #Time: O(n)\n #Space: O(n)\n \n #Represents how far a character is away from c (x = inf)\n #loveleetcode\n #xxx010012340 - first pass\n #321010043210 - second pass\n #321010012210 -... | 2 | 0 | ['Python'] | 0 |
shortest-distance-to-a-character | Easy solution Using C++ | easy-solution-using-c-by-cryp2coder420-amvu | vector shortestToChar(string s, char c) {\n vector v;\n for (int i = 0; i < s.size(); i++)\n {\n if (s[i] == c)\n | cryp2coder420 | NORMAL | 2021-10-06T03:14:38.167443+00:00 | 2021-10-06T03:14:38.167494+00:00 | 91 | false | # vector<int> shortestToChar(string s, char c) {\n vector<int> v;\n for (int i = 0; i < s.size(); i++)\n {\n if (s[i] == c)\n v.push_back(i);\n }\n\n vector<int> result;\n int index, diff;\n for (int i = 0; i < s.size(); i++)\n {\n ... | 2 | 0 | [] | 0 |
shortest-distance-to-a-character | Easy to understand, O(N), [C++, Java] | easy-to-understand-on-c-java-by-akashsah-cg55 | Implementation\n\n1st Approach in C++\nTime Complexity = O(N), Space Complexity = O(N)\n\n\nvector<int> shortestToChar(string s, char c) {\n vector<int> vec, | akashsahuji | NORMAL | 2021-06-10T05:25:54.479871+00:00 | 2021-06-10T05:25:54.479919+00:00 | 387 | false | Implementation\n\n**1st Approach in C++**\nTime Complexity = O(N), Space Complexity = O(N)\n\n```\nvector<int> shortestToChar(string s, char c) {\n vector<int> vec, res;\n for(int itr = 0; itr < s.size(); itr++){\n if(s[itr] == c) vec.push_back(itr);\n }\n \n int dif1 = 0, dif2 = 0; \n for(i... | 2 | 0 | ['Array', 'String', 'C', 'Java'] | 0 |
shortest-distance-to-a-character | C++ Simplest Solution | c-simplest-solution-by-nandini_77-gknc | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int n = s.size();\n vector<int> ans(n,0);\n int prev = -1 | nandini_77 | NORMAL | 2021-02-13T09:48:25.776946+00:00 | 2021-02-13T09:48:25.776978+00:00 | 193 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int n = s.size();\n vector<int> ans(n,0);\n int prev = -1;\n for(int i = 0; i<n; i++){\n if(s[i] == c)\n prev = i;\n if(prev!=-1)\n ans[i] = i - prev; \n ... | 2 | 0 | ['C'] | 0 |
shortest-distance-to-a-character | [Python/Python3] Shortest Distance to a Character | pythonpython3-shortest-distance-to-a-cha-p8uc | ShamelessSelfPromotion: My other leetcode solutions to various questions can be found here\n\nThis algorithm is called 2-Pass as we iterate through array twice: | newborncoder | NORMAL | 2021-02-09T03:28:24.378117+00:00 | 2021-05-10T09:15:24.688803+00:00 | 306 | false | ***ShamelessSelfPromotion***: My other leetcode [solutions](https://leetcode.com/discuss/general-discussion/1112952/Collection-of-my-leetcode-solution-posts) to various questions can be found [here](https://leetcode.com/discuss/general-discussion/1112952/Collection-of-my-leetcode-solution-posts)\n\nThis algorithm is ca... | 2 | 1 | ['Python', 'Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | 665772 | 665772-by-stefanpochmann-sli1 | Ruby:\n\ndef count_prime_set_bits(l, r)\n (l..r).sum { |i| 665772 >> i.digits(2).sum & 1 }\nend\n\nPython:\n\n def countPrimeSetBits(self, L, R):\n r | stefanpochmann | NORMAL | 2018-01-14T07:03:16.079000+00:00 | 2018-09-06T03:28:50.057218+00:00 | 9,426 | false | Ruby:\n```\ndef count_prime_set_bits(l, r)\n (l..r).sum { |i| 665772 >> i.digits(2).sum & 1 }\nend\n```\nPython:\n\n def countPrimeSetBits(self, L, R):\n return sum(665772 >> bin(i).count('1') & 1 for i in range(L, R+1))\n\nJava stream:\n\n public int countPrimeSetBits(int L, int R) {\n return IntS... | 211 | 10 | [] | 24 |
prime-number-of-set-bits-in-binary-representation | [Java/C++] Clean Code | javac-clean-code-by-alexander-b156 | Java\n\nclass Solution {\n public int countPrimeSetBits(int l, int r) {\n Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19 | alexander | NORMAL | 2018-01-14T04:02:46.739000+00:00 | 2018-10-24T14:51:54.154335+00:00 | 13,805 | false | **Java**\n```\nclass Solution {\n public int countPrimeSetBits(int l, int r) {\n Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19 /*, 23, 29 */ ));\n int cnt = 0;\n for (int i = l; i <= r; i++) {\n int bits = 0;\n for (int n = i; n > 0; n >>= 1)\... | 70 | 2 | [] | 14 |
prime-number-of-set-bits-in-binary-representation | Sort Easy Python | sort-easy-python-by-localhostghost-02yb | \nclass Solution:\n def countPrimeSetBits(self, L, R):\n """\n :type L: int\n :type R: int\n :rtype: int\n """\n pr | localhostghost | NORMAL | 2018-02-12T04:56:46.093000+00:00 | 2018-10-25T14:23:48.715185+00:00 | 3,632 | false | ```\nclass Solution:\n def countPrimeSetBits(self, L, R):\n """\n :type L: int\n :type R: int\n :rtype: int\n """\n primes = {2, 3, 5, 7, 11, 13, 17, 19}\n return sum(map(lambda x: bin(x).count('1') in primes, range(L, R+1)))\n``` | 38 | 0 | [] | 4 |
prime-number-of-set-bits-in-binary-representation | Easy O(n) Java solution using DP | easy-on-java-solution-using-dp-by-ashish-dsj5 | \nclass Solution {\n public int countPrimeSetBits(int L, int R) {\n int cnt = 0;\n Set<Integer> listPrimes = new HashSet<>(Arrays.asList(2, 3, | ashish53v | NORMAL | 2018-01-14T04:09:44.167000+00:00 | 2018-08-11T16:08:26.213838+00:00 | 4,480 | false | ```\nclass Solution {\n public int countPrimeSetBits(int L, int R) {\n int cnt = 0;\n Set<Integer> listPrimes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ));\n int[] res = countBits(R);\n for(int i=L;i<=R;i++){\n if(listPrimes.contains(res[i])){\n ... | 25 | 2 | [] | 4 |
prime-number-of-set-bits-in-binary-representation | A fast algorithm utilizing Pascal's Triangle | a-fast-algorithm-utilizing-pascals-trian-a0on | Simple scenarios\nLet\'s observe first, how we may count the total number of set bits from (L,0]. We start with "simple numbers", e.g. 0b10000, with a 1 at posi | zhoulytwin | NORMAL | 2018-08-09T04:45:21.740163+00:00 | 2018-08-31T22:41:40.417266+00:00 | 1,430 | false | ## Simple scenarios\nLet\'s observe first, how we may count the total number of set bits from `(L,0]`. We start with "simple numbers", e.g. `0b10000`, with a `1` at position `n` followed by `n-1` `0`s:\n\n|n| L | Possible bit combos within (L,0] | 0 set bits | 1 set bits | 2 set bits | 3 set bits |\n|-|------... | 16 | 0 | [] | 3 |
prime-number-of-set-bits-in-binary-representation | Python 1 line | python-1-line-by-guofei9987-hxy7 | \nclass Solution:\n def countPrimeSetBits(self, L, R):\n """\n :type L: int\n :type R: int\n :rtype: int\n """\n re | guofei9987 | NORMAL | 2018-08-15T07:54:11.924189+00:00 | 2018-09-03T16:35:02.240974+00:00 | 1,632 | false | ```\nclass Solution:\n def countPrimeSetBits(self, L, R):\n """\n :type L: int\n :type R: int\n :rtype: int\n """\n return len([1 for i in range(L,R+1) if bin(i).count(\'1\') in [2, 3, 5, 7, 11, 13, 17, 19]])\n``` | 14 | 0 | [] | 3 |
prime-number-of-set-bits-in-binary-representation | c++ implementation for every function | c-implementation-for-every-function-by-m-8ygo | \nclass Solution {\npublic:\n \n int count_of_bits(int num){ //function to return number of bits\n int count = 0;\n while (num > 0){\n | minhphanhvu | NORMAL | 2020-07-23T20:25:24.123359+00:00 | 2020-07-23T20:25:24.123392+00:00 | 1,546 | false | ```\nclass Solution {\npublic:\n \n int count_of_bits(int num){ //function to return number of bits\n int count = 0;\n while (num > 0){\n if (num % 2 == 1){\n count +=1;\n num = num/2;\n }\n else{\n num = num/2;\n ... | 10 | 1 | ['C', 'C++'] | 1 |
prime-number-of-set-bits-in-binary-representation | Fast Python beats 100% (40ms, uses binomial coefficients) | fast-python-beats-100-40ms-uses-binomial-ttsl | Using this formula\n\nwe can obtain some fast code. There are O(log^2 N / log log N) binomial coefficients in this sum, I think. Note that the binomial coeffici | gd303 | NORMAL | 2018-08-08T12:38:45.886161+00:00 | 2018-08-08T12:38:45.886161+00:00 | 1,600 | false | Using this formula\n\nwe can obtain some fast code. There are O(log^2 N / log log N) binomial coefficients in this sum, I think. Note that the binomial coefficient cache is global across test cases.\n```\ndef binomial(n, k, cache={}):\n ... | 10 | 0 | [] | 5 |
prime-number-of-set-bits-in-binary-representation | Easy Python solution | easy-python-solution-by-vistrit-jgut | \ndef countPrimeSetBits(self, left: int, right: int) -> int:\n count=0\n for i in range(left,right+1):\n c=0\n n=bin(i).coun | vistrit | NORMAL | 2021-12-05T06:02:12.398473+00:00 | 2021-12-05T06:02:12.398505+00:00 | 987 | false | ```\ndef countPrimeSetBits(self, left: int, right: int) -> int:\n count=0\n for i in range(left,right+1):\n c=0\n n=bin(i).count("1")\n for j in range(1,n+1):\n if n%j==0:\n c+=1\n if c==2:\n count+=1\n ret... | 7 | 0 | ['Python', 'Python3'] | 1 |
prime-number-of-set-bits-in-binary-representation | Application of Wilson's theorem | application-of-wilsons-theorem-by-treema-wohi | It follows Wilson\'s theorem to perform one-liner test for prime. But to create the hash table is still quicker.\npython\nclass Solution(object):\n def count | treemantan | NORMAL | 2019-12-02T09:37:11.312320+00:00 | 2019-12-02T09:37:11.312368+00:00 | 651 | false | It follows [Wilson\'s theorem](https://en.wikipedia.org/wiki/Wilson%27s_theorem) to perform one-liner test for prime. But to create the hash table is still quicker.\n```python\nclass Solution(object):\n def countPrimeSetBits(self, L, R):\n """\n :type L: int\n :type R: int\n :rtype: int\n... | 7 | 0 | ['Python'] | 2 |
prime-number-of-set-bits-in-binary-representation | Python simple solution w/ explanation | python-simple-solution-w-explanation-by-y0pbp | The most intuitive way to count for 1\'s in the binary expression of a number, is counting the 1\'s. Luckily, python can do that using .count() method. \n\nSo a | jxw7410 | NORMAL | 2018-12-09T07:35:54.940371+00:00 | 2018-12-09T07:35:54.940426+00:00 | 754 | false | The most intuitive way to count for 1\'s in the binary expression of a number, is counting the 1\'s. Luckily, python can do that using .count() method. \n\nSo all we have to do is convert the number to binary, and do a count(). However, how do we know if a count is prime? \n\nWe could solve for the prime, but that woul... | 7 | 0 | [] | 3 |
prime-number-of-set-bits-in-binary-representation | C++ Simple Solution || 100% faster in runtime | c-simple-solution-100-faster-in-runtime-12cmy | \nclass Solution {\npublic:\n int countPrimeSetBits(int L, int R) {\n int count=0;\n for(int i=L;i<=R;i++){\n if(checkPrime(findSetB | rahuls321 | NORMAL | 2021-03-24T12:56:31.221842+00:00 | 2021-03-24T12:56:31.221887+00:00 | 1,213 | false | ```\nclass Solution {\npublic:\n int countPrimeSetBits(int L, int R) {\n int count=0;\n for(int i=L;i<=R;i++){\n if(checkPrime(findSetBits(i))) count++;\n }\n return count;\n }\n int findSetBits(int n){\n int count=0;\n while(n){\n n=n&(n-1);\n ... | 6 | 0 | ['C', 'C++'] | 2 |
prime-number-of-set-bits-in-binary-representation | 5 lines hashset regexp javascript solution | 5-lines-hashset-regexp-javascript-soluti-zqmy | \n/**\n * @param {number} L\n * @param {number} R\n * @return {number}\n */\nvar countPrimeSetBits = function(L, R) {\n let set = new Set([2, 3, 5, 7, 11, 13, | daimant | NORMAL | 2020-03-06T12:07:46.447562+00:00 | 2020-03-06T12:07:46.447609+00:00 | 271 | false | ```\n/**\n * @param {number} L\n * @param {number} R\n * @return {number}\n */\nvar countPrimeSetBits = function(L, R) {\n let set = new Set([2, 3, 5, 7, 11, 13, 17, 19]);\n let countPrime = 0;\n \n for (let i = L; i <= R; i++) {\n if (set.has(i.toString(2).replace(/0/g, \'\').length)) countPrime++;\n }\n\n re... | 6 | 0 | ['JavaScript'] | 0 |
prime-number-of-set-bits-in-binary-representation | Rust 1 liner (0ms, 1.9mb) | rust-1-liner-0ms-19mb-by-rcode0-kgm8 | \npub fn count_prime_set_bits(l: i32, r: i32) -> i32 {\n (l..=r).filter(|x|[2,3,5,7,11,13,17,19].contains(&x.count_ones())).count() as i32\n}\n// 2021 De | rcode0 | NORMAL | 2019-08-04T08:54:43.365401+00:00 | 2021-12-03T12:50:53.346746+00:00 | 289 | false | ```\npub fn count_prime_set_bits(l: i32, r: i32) -> i32 {\n (l..=r).filter(|x|[2,3,5,7,11,13,17,19].contains(&x.count_ones())).count() as i32\n}\n// 2021 Dec: Updated from 2.3->1.9mb and to not check >19 as per BigMih\'s comment\n```\n\nFun fact:\nThere\'s an instruction in recent Intel & AMD processors for coun... | 6 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | C++ 9 ms | c-9-ms-by-huahualeetcode-1c52 | \nclass Solution {\npublic:\n int countPrimeSetBits(int L, int R) {\n int ans = 0;\n unsigned magic = 2693408941;\n for (int n = L; n <= R; ++n)\n | huahualeetcode | NORMAL | 2018-01-18T06:48:01.131000+00:00 | 2018-10-09T13:05:07.937941+00:00 | 1,470 | false | ```\nclass Solution {\npublic:\n int countPrimeSetBits(int L, int R) {\n int ans = 0;\n unsigned magic = 2693408941;\n for (int n = L; n <= R; ++n)\n if (magic & (1 << __builtin_popcountll(n))) ++ans;\n return ans;\n }\n};\n``` | 6 | 1 | [] | 3 |
prime-number-of-set-bits-in-binary-representation | DP and Combinatorics | Fastest | O(log2(R)) | Complete math explanation | dp-and-combinatorics-fastest-olog2r-comp-mx6n | Dont forget to upvote if you like the content below. \uD83D\uDE43\n\n# TL;DR\nTake as an example the range [6, 10].\n1. Examine the most significant bit of the | wallandteen | NORMAL | 2023-02-04T22:21:17.515184+00:00 | 2023-05-07T22:10:43.087817+00:00 | 789 | false | Dont forget to upvote if you like the content below. \uD83D\uDE43\n\n# TL;DR\nTake as an example the range `[6, 10]`.\n1. Examine the most significant bit of the binary representation of $$10$$: `1010`.\n2. For numbers less than `1000` (i.e., 8 in decimal), i.e., in the range of `[0, 111]`, we can fill the $$3$$ bits w... | 5 | 0 | ['Math', 'Dynamic Programming', 'Brainteaser', 'Combinatorics', 'Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | java FOR LOOP easy SOLUTION 👌 | java-for-loop-easy-solution-by-sulaymon-fgorf | \n public int countPrimeSetBits(int left, int right) {\n int res = 0;\n for (int i = left; i <= right; i++) {\n if (isPrime(Integer. | Sulaymon-Dev20 | NORMAL | 2022-05-26T10:37:47.418513+00:00 | 2022-05-26T10:37:47.418545+00:00 | 735 | false | ```\n public int countPrimeSetBits(int left, int right) {\n int res = 0;\n for (int i = left; i <= right; i++) {\n if (isPrime(Integer.bitCount(i))) res++;\n }\n return res;\n }\n\n public boolean isPrime(int n) {\n if (n <= 1) return false;\n for (int i = 2... | 5 | 1 | ['Java'] | 2 |
prime-number-of-set-bits-in-binary-representation | Python Solution w/o bit manipulation, 214ms beats 91.23% with Explanation | python-solution-wo-bit-manipulation-214m-kzmv | Explanation\nThe hard part was checking whether the set bit was prime, but the 32-bit integer limit helped us with that! \uD83D\uDE0F\n\n1. Iterate through the | alexl5311 | NORMAL | 2022-05-09T03:54:18.910591+00:00 | 2022-05-09T03:54:18.910619+00:00 | 578 | false | **Explanation**\n*The hard part was checking whether the set bit was prime, but the 32-bit integer limit helped us with that! \uD83D\uDE0F*\n\n1. Iterate through the range\n2. Check the number of `1`\'s in the binary representation of that the number\n3. Check if it is prime: if yes, add 1 to count\n\n if bin(i).count(\'1\') in {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}])\n | jason003 | NORMAL | 2019-02-20T14:45:58.347720+00:00 | 2019-02-20T14:45:58.347760+00:00 | 391 | false | ```python\nreturn len([i for i in range(L, R + 1) if bin(i).count(\'1\') in {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}])\n``` | 5 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | O(log R) solution (C++, Python, Ruby) | olog-r-solution-c-python-ruby-by-stefanp-5kto | So far I think only brute force solutions have been posted (checking all numbers from L to R and counting the "good" ones (those with a prime number of 1-bits)) | stefanpochmann | NORMAL | 2018-01-14T20:29:59.761000+00:00 | 2018-01-14T20:29:59.761000+00:00 | 866 | false | So far I think only brute force solutions have been posted (checking all numbers from L to R and counting the "good" ones (those with a prime number of 1-bits)). Here's an efficient solution, it's O(log<sup>2</sup> R<sub>max</sub>) where R<sub>max</sub> is the maximum allowed R-value (i.e., 10^6 in the problem specific... | 5 | 1 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | C++ Bits count vs Combinatorial Formula(Pascal)||Beats 100% | c-bits-count-vs-combinatorial-formulapas-ffy8 | \n# Intuition\n Describe your first thoughts on how to solve this problem. \nBits counting is very intuitive to solve this question and just uses built-in func | anwendeng | NORMAL | 2023-09-02T05:41:23.153073+00:00 | 2023-09-02T05:41:23.153095+00:00 | 320 | false | \n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBits counting is very intuitive to solve this question and just uses built-in function. The hard way is to use the follolwing combinatorial formula\n$$\n#\\{x <N|bit\\_count(x)=k\\} =\n\\sum_{i=0}^k C(p[i], k-i)\n$$\nwhere p[i]=positi... | 4 | 0 | ['Dynamic Programming', 'Combinatorics', 'Number Theory', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | Simple C++ Code with Explanation || Easy understanding | simple-c-code-with-explanation-easy-unde-y8gs | class Solution {\npublic:\nint countPrimeSetBits(int left, int right) {\n// given constraint left , right <= 10^6\n\n // 2^10 == 10^3 implies 10^6 ==2^20 ( | codevin6 | NORMAL | 2023-02-17T18:11:03.001978+00:00 | 2023-02-17T18:11:03.002012+00:00 | 597 | false | class Solution {\npublic:\nint countPrimeSetBits(int left, int right) {\n// given constraint left , right <= 10^6\n\n // 2^10 == 10^3 implies 10^6 ==2^20 ( 20 bits can be set at max )\n \n vector<bool> p(20,false);\n \n //set prime vector p\n p[2]=p[3]=p[5]=p[7]=p[11]=p[13]=p[17]=p[19]=true;\n ... | 4 | 0 | ['C'] | 1 |
prime-number-of-set-bits-in-binary-representation | C++ 3ms | c-3ms-by-iamssuraj-seak | \n```\nclass Solution {\npublic:\n int cnt1(int n)\n {\n int cnt = 0;\n while(n)\n {\n n = n & (n-1);\n cnt++;\ | iamssuraj | NORMAL | 2022-08-12T05:41:34.873746+00:00 | 2022-08-12T05:45:17.797223+00:00 | 201 | false | \n```\nclass Solution {\npublic:\n int cnt1(int n)\n {\n int cnt = 0;\n while(n)\n {\n n = n & (n-1);\n cnt++;\n }\n return cnt;\n }\n bool isPrime(int n)\n {\n return n == 2 || n ==3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n... | 4 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | C++| EASY TO UNDERSTAND| fast and efficient | Intuitive approach | c-easy-to-understand-fast-and-efficient-p6rf1 | Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\n```\nclass Solution {\npublic:\n int countPrimeSetBits(int le | aarindey | NORMAL | 2021-06-05T17:56:31.937848+00:00 | 2021-06-24T23:19:01.358436+00:00 | 519 | false | **Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)**\n```\nclass Solution {\npublic:\n int countPrimeSetBits(int left, int right) {\n int num;\n int count,c=0;\n while(left!=(right+1)) \n {\n count=0;\n num=left;... | 4 | 0 | ['Bit Manipulation', 'C'] | 2 |
prime-number-of-set-bits-in-binary-representation | easy python again | easy-python-again-by-bor4eg-dkg2 | class Solution:\n \n def countPrimeSetBits(self, left: int, right: int) -> int:\n res = 0\n for x in range(left, right+1):\n if | bor4eg | NORMAL | 2021-05-30T11:22:26.172400+00:00 | 2021-05-30T12:48:05.954369+00:00 | 100 | false | class Solution:\n \n def countPrimeSetBits(self, left: int, right: int) -> int:\n res = 0\n for x in range(left, right+1):\n if bin(x).count(\'1\') in {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}:\n res += 1\n return res\n | 4 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | simple java solution: time O(n), space O(1) | simple-java-solution-time-on-space-o1-by-z1or | Intuition:\nWe have limited range of L and R - up to 10^6, meaning their bit representations will have up to 20 symbols (10^6 < 2^20).\nWe can cash prime number | trofimoff | NORMAL | 2020-06-21T23:13:45.902464+00:00 | 2020-06-21T23:16:09.189534+00:00 | 632 | false | **Intuition:**\nWe have limited range of L and R - up to 10^6, meaning their bit representations will have up to 20 symbols (10^6 < 2^20).\nWe can cash prime numbers in range of 0-20, maybe the most efficient is to use bucket sort so the found prime numbers: 2, 3, 5, 7, 11, 13, 17, 19 are converted into true in the cor... | 4 | 0 | ['Bucket Sort', 'Java'] | 1 |
prime-number-of-set-bits-in-binary-representation | C# Solution | c-solution-by-moh80-6rvd | public int CountPrimeSetBits(int L, int R) {\n int count = 0;\n for (int i = L; i <= R; i++)\n {\n int bitsCount = GetBitsCount( | Moh80 | NORMAL | 2018-01-15T18:36:25.903000+00:00 | 2018-01-15T18:36:25.903000+00:00 | 220 | false | public int CountPrimeSetBits(int L, int R) {\n int count = 0;\n for (int i = L; i <= R; i++)\n {\n int bitsCount = GetBitsCount(i);\n if (IsPrime(bitsCount)) count++; \n }\n \n return count; \n }\n \n private int GetBitsCount(int a)\n {\n ... | 4 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | 762. Simple C++ solution | 762-simple-c-solution-by-shreyash_153-kn2n | Code\n\nclass Solution {\npublic:\n bool isprime(int k)\n {\n if(k==2||k==3||k==5||k==7||k==11||k==13||k==17||k==19)\n return 1;\n el | shreyash_153 | NORMAL | 2024-05-11T10:09:32.016556+00:00 | 2024-05-11T10:09:32.016586+00:00 | 505 | false | # Code\n```\nclass Solution {\npublic:\n bool isprime(int k)\n {\n if(k==2||k==3||k==5||k==7||k==11||k==13||k==17||k==19)\n return 1;\n else\n return 0;\n }\n int countPrimeSetBits(int left, int right) {\n int ans=0;\n for(int i=left;i<=right;i++)\n {\n ... | 3 | 0 | ['Math', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | 762: Beats 91.74%, Solution with step by step explanation | 762-beats-9174-solution-with-step-by-ste-gsob | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n primes = {2, 3, 5, 7, 11, 13, 17, 19}\n\n\nWe create a set of p | Marlen09 | NORMAL | 2023-10-24T17:34:31.611226+00:00 | 2023-10-24T17:34:59.861231+00:00 | 369 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n primes = {2, 3, 5, 7, 11, 13, 17, 19}\n```\n\nWe create a set of prime numbers which are less than 20. Since the maximum number of bits we are concerned with is 20 (for the given problem constraints), we only ne... | 3 | 0 | ['Math', 'Bit Manipulation', 'Python', 'Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | Go - using Kernighan's algorithm for counting set bits | go-using-kernighans-algorithm-for-counti-qtww | \nfunc countPrimeSetBits(left int, right int) int {\n primes := map[int]bool{\n 2: true,\n 3: true,\n 5: true,\n 7: true,\n | tucux | NORMAL | 2022-09-11T16:50:00.873627+00:00 | 2022-09-11T16:50:00.873679+00:00 | 462 | false | ```\nfunc countPrimeSetBits(left int, right int) int {\n primes := map[int]bool{\n 2: true,\n 3: true,\n 5: true,\n 7: true,\n 11: true,\n 13: true,\n 17: true,\n 19: true,\n }\n c := 0\n for i := left; i <= right; i++ {\n s := countSetBits(i)\n... | 3 | 0 | ['Go'] | 0 |
prime-number-of-set-bits-in-binary-representation | Python3 - with checkPrime() function | python3-with-checkprime-function-by-than-2fog | \nclass Solution:\n def checkPrime(self, number):\n if number > 1:\n for i in range(2, number):\n if (number % i) == 0:\n | thanhinterpol | NORMAL | 2020-10-06T08:11:50.173913+00:00 | 2020-10-06T08:11:50.173953+00:00 | 132 | false | ```\nclass Solution:\n def checkPrime(self, number):\n if number > 1:\n for i in range(2, number):\n if (number % i) == 0:\n return False\n break\n else:\n return True\n else:\n return False\n def co... | 3 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | [JavaScript] JS Solution(s) that beats 100% | javascript-js-solutions-that-beats-100-b-kfxo | My first approach was the following (it uses Primality test with 6k \xB1 1 optimization, pre-defined set of known prime numbers and caching):\n\n/**\n * @param | GreenTeaCake | NORMAL | 2019-05-23T17:58:26.912378+00:00 | 2019-05-23T17:58:26.912418+00:00 | 714 | false | My first approach was the following (it uses [Primality test with 6k \xB1 1 optimization](https://en.wikipedia.org/wiki/Primality_test#Pseudocode), pre-defined set of known prime numbers and caching):\n```\n/**\n * @param {number} L\n * @param {number} R\n * @return {number}\n */\nconst getNumOfSetBits = function(n) {\... | 3 | 0 | ['JavaScript'] | 1 |
prime-number-of-set-bits-in-binary-representation | Simple javascript | simple-javascript-by-absborodin-p24x | \n /**\n * https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/\n * @param {number} L\n * @param {number} R\n * @return {nu | absborodin | NORMAL | 2019-01-20T11:35:36.350946+00:00 | 2019-01-20T11:35:36.353913+00:00 | 255 | false | ```\n /**\n * https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/\n * @param {number} L\n * @param {number} R\n * @return {number}\n */\n var countPrimeSetBits = function(L, R) {\n let result = 0;\n let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23];\n\n for(let i = L; i <= ... | 3 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | Short C++, 12 ms | short-c-12-ms-by-stefanpochmann-qqby | int countPrimeSetBits(int L, int R) {\n int count = 0;\n while (L <= R) {\n int b = __builtin_popcount(L++);\n count += b < | stefanpochmann | NORMAL | 2018-01-14T05:43:07.842000+00:00 | 2018-01-14T05:43:07.842000+00:00 | 1,308 | false | int countPrimeSetBits(int L, int R) {\n int count = 0;\n while (L <= R) {\n int b = __builtin_popcount(L++);\n count += b < 4 ? b > 1 : b % 2 && b % 3;\n }\n return count;\n }\n\nNumbers in the allowed range have only up to 20 bits and for a primality check it su... | 3 | 1 | [] | 3 |
prime-number-of-set-bits-in-binary-representation | Prime Numbers of Set Bits | prime-numbers-of-set-bits-by-kartixrivas-wgo5 | IntuitionApproachComplexity
Time complexity: O((right - left + 1) * log(right))
Space complexity: O(1)
Code | kartixrivastava | NORMAL | 2025-03-13T05:38:54.218886+00:00 | 2025-03-13T05:38:54.218886+00:00 | 77 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O((right - left + 1) * log(right))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space com... | 2 | 0 | ['C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | Counting Prime Set Bits in a Binary Range Using C++ | counting-prime-set-bits-in-a-binary-rang-avzf | Intuition\nThe problem revolves around counting the number of prime set bits in binary representations of numbers within a given range. The key insight is recog | Krishnaa2004 | NORMAL | 2024-09-07T13:47:47.993887+00:00 | 2024-09-07T13:47:47.993909+00:00 | 250 | false | # Intuition\nThe problem revolves around counting the number of prime set bits in binary representations of numbers within a given range. The key insight is recognizing that the set bit count can only be prime if it matches one of the known small prime numbers.\n\n# Approach\n1. **Prime Check**: Implement a helper func... | 2 | 0 | ['C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | Easy Java Solution | easy-java-solution-by-parth_dhavan-ifr7 | Intuition\n Describe your first thoughts on how to solve this problem. \nBinary Representation: Any integer can be represented in binary form, consisting of 0s | Parth_Dhavan | NORMAL | 2024-06-24T20:53:06.490837+00:00 | 2024-06-24T20:53:06.490865+00:00 | 325 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBinary Representation: Any integer can be represented in binary form, consisting of 0s and 1s.\nSet Bits: The number of 1s in the binary representation is referred to as the count of set bits.\nPrime Numbers: A prime number is a number gr... | 2 | 0 | ['Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | Easy to Understand || Well defined clean code | easy-to-understand-well-defined-clean-co-ju6l | 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 | Anshika_Srivastava | NORMAL | 2024-06-24T20:49:40.630042+00:00 | 2024-06-24T20:49:40.630065+00:00 | 289 | 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 | ['Bit Manipulation', 'Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | Easiest 6 languages C++ / Python3 / Java / C / Python beats 100% | easiest-6-languages-c-python3-java-c-pyt-22sm | Intuition\n\n\n\n\nC++ []\nclass Solution {\nprivate:\n bool isPrime(int n) {\n if (n == 1)\n return false;\n for (int i = 2; i <= n | Edwards310 | NORMAL | 2024-03-09T11:37:42.299854+00:00 | 2024-03-09T11:37:42.299883+00:00 | 294 | false | # Intuition\n\n\n![Screenshot 2024-03-09 17005... | 2 | 0 | ['C', 'Python', 'C++', 'Java', 'Python3'] | 1 |
prime-number-of-set-bits-in-binary-representation | ++ Intuitive Solution || Well-Explained ✌️🔥 | intuitive-solution-well-explained-by-jas-spn8 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves counting the number of integers in a given range whose binary repr | jasneet_aroraaa | NORMAL | 2024-02-11T15:41:57.036390+00:00 | 2024-02-11T15:41:57.036416+00:00 | 113 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves counting the number of integers in a given range whose binary representation contains a prime number of set bits. We need to devise a method to efficiently count such numbers within the given range.\n\n# Approach\n<!-... | 2 | 0 | ['Math', 'Bit Manipulation', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | C++ || Explained || Short code using sets | c-explained-short-code-using-sets-by-adi-lska | \n# Approach\n Describe your approach to solving the problem. \nTo represent numbers upto 10^6 in binary you only need 20 bits hence the maximum number of 1 bit | adityakhare863 | NORMAL | 2023-07-26T07:07:26.883328+00:00 | 2023-07-26T07:07:26.883358+00:00 | 25 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo represent numbers upto 10^6 in binary you only need 20 bits hence the maximum number of 1 bits in binary representation of numbers from left to right is 20. Hence we store prime numbers upto 20 in $$store$$. \n\nAfter which we count number of set... | 2 | 0 | ['C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | Python one-liner | python-one-liner-by-trpaslik-li0l | Intuition\nJust iterate and sum\n\nNote that 10**6 has 20 bits, so we need only primes <= 20.\nAlso, python 3.10 introduced int.bit_count() that is more or less | trpaslik | NORMAL | 2023-05-12T13:02:11.115699+00:00 | 2023-05-12T13:02:11.115740+00:00 | 779 | false | # Intuition\nJust iterate and sum\n\nNote that 10**6 has 20 bits, so we need only primes <= 20.\nAlso, python 3.10 introduced int.bit_count() that is more or less equivalent to bin(x).count("1") but obviously much faster.\n\n\n# Complexity\n- Time complexity: O(n) - iterating over the left..right range.\n\n- Space comp... | 2 | 0 | ['Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | Solution | solution-by-deleted_user-5w4c | C++ []\nclass Solution {\npublic:\n int countPrimeSetBits(int left, int right) {\n int ans=0;\n while(left<=right) {\n int cnt=__bui | deleted_user | NORMAL | 2023-04-26T21:37:47.182245+00:00 | 2023-04-26T22:02:30.426431+00:00 | 1,296 | false | ```C++ []\nclass Solution {\npublic:\n int countPrimeSetBits(int left, int right) {\n int ans=0;\n while(left<=right) {\n int cnt=__builtin_popcount(left);\n if(cnt==2 || cnt==3 || cnt==5 || cnt==7 || cnt==11 || cnt==13 || cnt==17 || cnt==19)\n ++ans;\n +... | 2 | 0 | ['C++', 'Java', 'Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | Simple Brute Force Approach Java | simple-brute-force-approach-java-by-vand-14d3 | \nclass Solution {\n private boolean isPrime(int num){\n if(num==1 || num==0)return false;\n for(int i=2;i*i<=num;i++){\n if(num%i== | vandittalwadia | NORMAL | 2023-04-14T06:56:58.510516+00:00 | 2023-04-14T06:56:58.510557+00:00 | 185 | false | ```\nclass Solution {\n private boolean isPrime(int num){\n if(num==1 || num==0)return false;\n for(int i=2;i*i<=num;i++){\n if(num%i==0)return false;\n }\n return true;\n }\n public int countPrimeSetBits(int left, int right) {\n int ans=0;\n for(int i=left;... | 2 | 0 | ['Bit Manipulation', 'Java'] | 1 |
prime-number-of-set-bits-in-binary-representation | simple cpp solution | simple-cpp-solution-by-prithviraj26-rksw | 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 | prithviraj26 | NORMAL | 2023-02-27T06:02:19.111901+00:00 | 2023-02-27T06:02:19.111946+00:00 | 1,215 | 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 | ['Math', 'Bit Manipulation', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | CPP SOLUTION || EASY TO UNDERSTAND || SIMPLE AND EASY CODE | cpp-solution-easy-to-understand-simple-a-5jax | 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 | Abhishek777888 | NORMAL | 2023-01-01T12:45:03.954144+00:00 | 2023-01-01T12:45:03.954214+00:00 | 1,346 | 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 | ['Bit Manipulation', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | 70% BEATS || C++ || SIMPLE || EASY | 70-beats-c-simple-easy-by-abhay_12345-fhh9 | \nclass Solution {\npublic:\n int countPrimeSetBits(int &left, int &right) {\n vector<int> v = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0, | abhay_12345 | NORMAL | 2022-09-08T06:50:24.555296+00:00 | 2022-09-08T06:50:24.555334+00:00 | 549 | false | ```\nclass Solution {\npublic:\n int countPrimeSetBits(int &left, int &right) {\n vector<int> v = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0};\n int ans = 0,x,k;\n while(left <= right){\n x = 0;\n k = left;\n while(k){\n x... | 2 | 0 | ['Bit Manipulation', 'C', 'C++'] | 1 |
prime-number-of-set-bits-in-binary-representation | Easiest Java ✅|| short and simple✅|| straight forward Solution✅ | easiest-java-short-and-simple-straight-f-vlsd | \n\nclass Solution {\n public int calculateSetBits(String s){\n int count=0;\n for (int i = 0; i < s.length(); i++) {\n if(s.charA | vaibhavnirmal2001 | NORMAL | 2022-08-07T08:08:03.420529+00:00 | 2022-08-07T08:08:03.420570+00:00 | 617 | false | \n```\nclass Solution {\n public int calculateSetBits(String s){\n int count=0;\n for (int i = 0; i < s.length(); i++) {\n if(s.charAt(i)==\'1\') count++;\n }\n return count;\n }\n\n public boolean isPrime(int n){\n if (n==0 || n==1) return false;\n for (... | 2 | 0 | ['Java'] | 1 |
prime-number-of-set-bits-in-binary-representation | [JAVA] | 3 Methods | Maths Explained | Bruteforce | Better | Optimal | java-3-methods-maths-explained-bruteforc-9goi | \n//Bruteforce Approach\n\nclass Solution {\n public int countPrimeSetBits(int left, int right) {\n int cnt=0;\n for(int i=left;i<=right;i++)\n | anand-swaroop-pandey | NORMAL | 2022-05-29T22:45:33.435722+00:00 | 2022-05-29T22:45:33.435750+00:00 | 399 | false | ```\n//Bruteforce Approach\n\nclass Solution {\n public int countPrimeSetBits(int left, int right) {\n int cnt=0;\n for(int i=left;i<=right;i++)\n if(primeSetBits(i))\n cnt++;\n return cnt;\n }\n \n public static boolean primeSetBits(int num)\n {\n int ... | 2 | 0 | ['Java'] | 1 |
prime-number-of-set-bits-in-binary-representation | o(1) to check Prime or not | o1-to-check-prime-or-not-by-tarunsriniva-uon3 | As no.of primes are limited we can use Hashset to check no.of set bits is prime or not\n\nclass Solution {\n public int countPrimeSetBits(int left, int right | tarunsrinivas | NORMAL | 2022-04-22T06:15:23.857794+00:00 | 2022-04-22T06:15:23.857837+00:00 | 1,581 | false | As no.of primes are limited we can use Hashset to check no.of set bits is prime or not\n```\nclass Solution {\n public int countPrimeSetBits(int left, int right) {\n HashSet<Integer> s=new HashSet<>();\n s.add(2);\n s.add(3);\n s.add(5);\n s.add(7);\n s.add(11);\n s.a... | 2 | 0 | ['Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | Easy solution || Faster than others || bit manipulation | easy-solution-faster-than-others-bit-man-66ab | ```\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==2||n==3||n==5 ||n==7||n==11||n==13||n==17||n==19)\n return true;\n ret | Shristha | NORMAL | 2022-03-31T13:48:24.596032+00:00 | 2022-03-31T13:48:24.596083+00:00 | 378 | false | ```\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==2||n==3||n==5 ||n==7||n==11||n==13||n==17||n==19)\n return true;\n return false;\n }\n int setCount(int n){\n int count=0;\n while(n>0){\n n=n&(n-1);\n count++;\n }\n return ... | 2 | 0 | ['Bit Manipulation', 'C', 'C++'] | 0 |
prime-number-of-set-bits-in-binary-representation | Prime Number of Set Bits in Binary Representation Solution Java | prime-number-of-set-bits-in-binary-repre-r2tr | class Solution {\n public int countPrimeSetBits(int L, int R) {\n // { 2, 3, 5, 7, 11, 13, 17, 19 }th bits are 1s\n // (10100010100010101100)2 = (665772) | bhupendra786 | NORMAL | 2022-03-24T07:35:42.672823+00:00 | 2022-03-24T07:35:42.672855+00:00 | 111 | false | class Solution {\n public int countPrimeSetBits(int L, int R) {\n // { 2, 3, 5, 7, 11, 13, 17, 19 }th bits are 1s\n // (10100010100010101100)2 = (665772)10\n final int magic = 665772;\n int ans = 0;\n\n for (int n = L; n <= R; ++n)\n if ((magic & 1 << Integer.bitCount(n)) > 0)\n ++ans;\n\n ... | 2 | 0 | ['Math', 'Bit Manipulation'] | 0 |
prime-number-of-set-bits-in-binary-representation | c++ faster than 95.75% | c-faster-than-9575-by-bholanathbarik9748-ml1z | \nclass Solution\n{\npublic:\n bool prime(int x)\n {\n return (x == 2 || x == 3 || x == 5 || x == 7 ||\n x == 11 || x == 13 || x == | bholanathbarik9748 | NORMAL | 2021-11-02T15:35:31.672711+00:00 | 2021-11-02T15:35:31.672742+00:00 | 94 | false | ```\nclass Solution\n{\npublic:\n bool prime(int x)\n {\n return (x == 2 || x == 3 || x == 5 || x == 7 ||\n x == 11 || x == 13 || x == 17 || x == 19);\n }\n\n int countPrimeSetBits(int left, int right)\n {\n int count = 0;\n int n;\n for (int i = left; i <= righ... | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | C++ Easy bit manipulation Solution | c-easy-bit-manipulation-solution-by-saat-2qk7 | \nclass Solution {\npublic:\n int CheckCnt(int n) {\n int cnt = 0;\n \n while (n) {\n cnt++;\n n = n & (n - 1);\n | 20250122.saathvik93 | NORMAL | 2021-10-14T02:27:43.165671+00:00 | 2021-10-14T02:27:43.165713+00:00 | 122 | false | ```\nclass Solution {\npublic:\n int CheckCnt(int n) {\n int cnt = 0;\n \n while (n) {\n cnt++;\n n = n & (n - 1);\n }\n \n return cnt;\n }\n \n bool isPrime(int n) {\n if (n <= 1) return false;\n\n for (int i = 2; i < n; ++i)\n ... | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | Java Brute Force. (Count Set Bits + Prime Checking) | java-brute-force-count-set-bits-prime-ch-7ecd | \nclass Solution {\n public int countPrimeSetBits(int left, int right) {\n int primes = 0;\n for (int i = left; i <= right; i++) {\n | abhishek_1110 | NORMAL | 2021-10-10T05:14:49.974362+00:00 | 2021-10-10T05:14:49.974410+00:00 | 173 | false | ```\nclass Solution {\n public int countPrimeSetBits(int left, int right) {\n int primes = 0;\n for (int i = left; i <= right; i++) {\n //counting set bits\n int x = countSetBits(i);\n if (checkingPrime(x)) {\n primes++;\n }\n }\n ... | 2 | 0 | ['Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | Python - solution for only one line, | python-solution-for-only-one-line-by-ste-ka54 | constraints: 1 <= left <= right <= 10^6\n10^6 = \'0b11110100001001000000\'\nmaximum number of 1\'s present is 20\nprime numbers within 20 is all of [2, 3, 5, 7, | steven5601 | NORMAL | 2021-08-11T09:17:20.956264+00:00 | 2021-08-11T09:17:55.657651+00:00 | 129 | false | constraints: 1 <= left <= right <= 10^6\n10^6 = \'0b11110100001001000000\'\nmaximum number of 1\'s present is 20\nprime numbers within 20 is all of [2, 3, 5, 7, 11, 13, 17, 19]\n```\nclass Solution:\n def countPrimeSetBits(self, left: int, right: int) -> int:\n return len([(list(bin(i)).count(\'1\')) for i in... | 2 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | Runtime:-4ms faster than 95% C++ solution | runtime-4ms-faster-than-95-c-solution-by-al5q | ```\nint ones(int num){\nint res=0;\nwhile(n>0){\nnum = num & (num-1);\nres++;\n}\nreturn res;\n}\n int countPrimeSetBits(int left, int right) {\n int | vdas53073 | NORMAL | 2021-07-28T04:12:28.949193+00:00 | 2021-07-28T11:14:47.768080+00:00 | 218 | false | ```\nint ones(int num){\nint res=0;\nwhile(n>0){\nnum = num & (num-1);\nres++;\n}\nreturn res;\n}\n int countPrimeSetBits(int left, int right) {\n int res=0;\n for(int i=left;i<=right;i++){\n int ans = ones(i);\n if((ans & 1)!=0 || ans==2){\n if(ans==3)\n ... | 2 | 0 | ['Bit Manipulation', 'C', 'C++'] | 1 |
prime-number-of-set-bits-in-binary-representation | Java Naive Approach | java-naive-approach-by-dryash213-9jdr | \tpublic int CountNumberofSetBit(int n){\n\t// -n ==2\'s Complement\n\t\t\tint counter=0;\n\t\t\twhile (n!=0){\n\t\t\t\tint temp = n & -n;\n\t\t\t\tn-=temp | dryash213 | NORMAL | 2021-06-19T05:32:19.884400+00:00 | 2021-06-19T05:32:19.884433+00:00 | 80 | false | \tpublic int CountNumberofSetBit(int n){\n\t// -n ==2\'s Complement\n\t\t\tint counter=0;\n\t\t\twhile (n!=0){\n\t\t\t\tint temp = n & -n;\n\t\t\t\tn-=temp;\n\t\t\t\tcounter++;\n\t\t\t}\n\t\t\treturn(counter);\n\t\t}\n\t\tpublic int countPrimeSetBits(int left, int right) {\n\t\t\tint ans=0;\n\t\t\tfor(int i =left;... | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | Python | Sieve of Eratosthenes | python-sieve-of-eratosthenes-by-sanjaych-fckp | \ndef get_prime_list(n: int) -> list:\n # Sieve of Eratosthenes\n primes = [True]*(n+1)\n primes[0] = primes[1] = False\n\n i = 2\n n = 32\n w | sanjaychandak95 | NORMAL | 2021-05-23T11:42:17.397967+00:00 | 2021-05-23T11:42:17.398009+00:00 | 133 | false | ```\ndef get_prime_list(n: int) -> list:\n # Sieve of Eratosthenes\n primes = [True]*(n+1)\n primes[0] = primes[1] = False\n\n i = 2\n n = 32\n while i*i < n:\n if primes[i]:\n j = i\n while i*j < n:\n primes[i*j] = False\n j += 1\n i+=... | 2 | 0 | ['Python', 'Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | Faster than 99.16 java soultion | faster-than-9916-java-soultion-by-rachit-ibdu | class Solution {\n public boolean prime(int n)\n {\n if(n==2 || n==3|| n==5|| n==7|| n==11||n==13|| n==17|| n==19 || n==23)\n {\n | Rachit_3850 | NORMAL | 2021-05-12T09:04:32.433792+00:00 | 2021-05-12T09:04:32.433824+00:00 | 406 | false | class Solution {\n public boolean prime(int n)\n {\n if(n==2 || n==3|| n==5|| n==7|| n==11||n==13|| n==17|| n==19 || n==23)\n {\n return true;\n }\n else\n {\n return false;\n }\n }\n public int countPrimeSetBits(int left, int right) {\n ... | 2 | 0 | ['Bit Manipulation', 'Java'] | 0 |
prime-number-of-set-bits-in-binary-representation | Faster than 94.62% of Python3 online submissions | faster-than-9462-of-python3-online-submi-xd8g | \nclass Solution:\n def countPrimeSetBits(self, L: int, R: int) -> int:\n prime = {2,3,5,7,11,13,17,19}\n count = 0\n for i in range(L,R | bhardwajsiddhant03 | NORMAL | 2021-02-16T00:19:05.111596+00:00 | 2021-02-16T00:19:05.111642+00:00 | 59 | false | ```\nclass Solution:\n def countPrimeSetBits(self, L: int, R: int) -> int:\n prime = {2,3,5,7,11,13,17,19}\n count = 0\n for i in range(L,R + 1):\n num = bin(i).count(\'1\')\n if num in prime:\n count += 1\n return count\n``` | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | [JAVA] Clean Code, O(N log N) Optimal Solution | java-clean-code-on-log-n-optimal-solutio-ofwe | \nclass Solution {\n \n private boolean isPrime (int n) {\n return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n== 17 || n == 19; | anii_agrawal | NORMAL | 2020-07-14T13:27:01.446688+00:00 | 2020-07-14T13:27:01.446777+00:00 | 139 | false | ```\nclass Solution {\n \n private boolean isPrime (int n) {\n return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n== 17 || n == 19;\n }\n \n public int countPrimeSetBits(int L, int R) {\n \n int ans = 0;\n \n while (L <= R) {\n if (isPrime ... | 2 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | [C++] Easy Solution to understand (No Built-in Methods) | c-easy-solution-to-understand-no-built-i-xymy | \n\nPlease upvote if you like this post.\n\n\nResult: Accepted\n\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==1)\n return fals | pulkitswami7 | NORMAL | 2020-05-31T11:53:38.161385+00:00 | 2020-06-03T07:14:39.730210+00:00 | 159 | false | <hr>\n\n**Please upvote if you like this post.**\n<hr>\n\n**Result:** Accepted\n```\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==1)\n return false;\n int count=0;\n for(int i=2;i<=n;i++){\n if(n%i==0)\n count++;\n }\n if(count==1)\n... | 2 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | Solution in Python 3 (beats ~98%) (one line) | solution-in-python-3-beats-98-one-line-b-6nap | ```\nclass Solution:\n def countPrimeSetBits(self, L: int, R: int) -> int:\n \treturn sum(1 for i in range(L,R+1) if bin(i).count(\'1\') in {2,3,5,7,11,13 | junaidmansuri | NORMAL | 2019-09-13T06:12:52.693627+00:00 | 2019-09-13T06:18:27.011965+00:00 | 374 | false | ```\nclass Solution:\n def countPrimeSetBits(self, L: int, R: int) -> int:\n \treturn sum(1 for i in range(L,R+1) if bin(i).count(\'1\') in {2,3,5,7,11,13,17,19})\n\t\t\n\t\t\n- Junaid Mansuri\n(LeetCode ID)@hotmail.com | 2 | 1 | ['Python', 'Python3'] | 0 |
prime-number-of-set-bits-in-binary-representation | Java with Sieve of Eratosphenus | java-with-sieve-of-eratosphenus-by-space-cd3q | \nclass Solution {\n boolean[] prime;\n void sieveOfEratosphenus(int n){\n prime = new boolean[n+1];\n for(int i = 0; i < n; i++)\n | space_cat | NORMAL | 2018-08-30T18:15:51.237435+00:00 | 2018-08-30T18:15:51.237482+00:00 | 225 | false | ```\nclass Solution {\n boolean[] prime;\n void sieveOfEratosphenus(int n){\n prime = new boolean[n+1];\n for(int i = 0; i < n; i++)\n prime[i] = true;\n \n for(int p = 2; p*p <=n; p++)\n {\n // If prime[p] is not changed, then it is a prime\n i... | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | 5 lines Python Solution and Simple Explanation | 5-lines-python-solution-and-simple-expla-3owo | \nclass Solution(object):\n def countPrimeSetBits(self, L, R):\n \n # "L, R in the range [1, 10^6]" means it is less than 2**20.\n # So, | positive235 | NORMAL | 2018-04-10T22:40:06.782019+00:00 | 2018-04-10T22:40:06.782019+00:00 | 246 | false | ```\nclass Solution(object):\n def countPrimeSetBits(self, L, R):\n \n # "L, R in the range [1, 10^6]" means it is less than 2**20.\n # So, possible prime numbers are 2, 3, 5, 7, 11, 13, 17, 19.\n \n k = 0\n for n in range(L , R + 1):\n if bin(n).count(\'1\') in [... | 2 | 0 | [] | 1 |
prime-number-of-set-bits-in-binary-representation | O(1) time c++ solution | o1-time-c-solution-by-yoyo0720-cgxo | The idea is based on the following observation:\nIf we want to find out how many numbers in the range of [1,52] have prime number of set bits, we first examine | yoyo0720 | NORMAL | 2018-03-18T16:33:56.818234+00:00 | 2018-03-18T16:33:56.818234+00:00 | 384 | false | The idea is based on the following observation:\nIf we want to find out how many numbers in the range of [1,52] have prime number of set bits, we first examine the most significant bit of the binary representation 110100.\nFor numbers less than 100000, i.e. in the range of [0,11111], we can fill the 5 bits with 2 or 3 ... | 2 | 0 | [] | 2 |
prime-number-of-set-bits-in-binary-representation | Java 2 lines | java-2-lines-by-climberig-frzf | ```\n public int countPrimeSetBits(int L, int R) {\n Set primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));\n return (int)IntStrea | climberig | NORMAL | 2018-03-09T21:34:07.717036+00:00 | 2018-03-09T21:34:07.717036+00:00 | 230 | false | ```\n public int countPrimeSetBits(int L, int R) {\n Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));\n return (int)IntStream.range(L, R + 1).map(Integer::bitCount).filter(primes::contains).count();\n } | 2 | 0 | [] | 0 |
prime-number-of-set-bits-in-binary-representation | Counting Ones | counting-ones-by-khaled-alomari-cp7m | Complexity
Time complexity:
O((R−L)∗log(R−L))
Space complexity:
O(1)
Code | khaled-alomari | NORMAL | 2025-02-24T18:39:07.467761+00:00 | 2025-02-24T18:39:07.467761+00:00 | 58 | false | # Complexity
- Time complexity:
$$O((R - L) * log(R - L))$$
- Space complexity:
$$O(1)$$
# Code
```typescript []
function countPrimeSetBits(left: number, right: number) {
const countOnes = (num: number) => {
let count = 0;
for (const b of num.toString(2)) count += +(b === '1');
return coun... | 1 | 0 | ['Array', 'Math', 'Bit Manipulation', 'Memoization', 'Counting', 'Iterator', 'TypeScript', 'JavaScript'] | 0 |
prime-number-of-set-bits-in-binary-representation | Optimized Approach with Bit Manipulation | JAVA | Bit Manipulation | optimized-approach-with-bit-manipulation-63rx | IntuitionThe problem requires us to count numbers in a given range[left, right]where the number of set bits in their binary representation is a prime number.My | tNPecHTXEY | NORMAL | 2025-02-24T01:47:46.777400+00:00 | 2025-02-24T01:47:46.777400+00:00 | 119 | false |
# Intuition
The problem requires us to count numbers in a given range `[left, right]` where the number of set bits in their binary representation is a prime number.
My first thought was:
1. Iterate through each number in the range.
2. Count the number of `1`s (set bits) in its binary representation.
3. Check if the ... | 1 | 0 | ['Math', 'Bit Manipulation', 'Java'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.