🚀 Day 511 of #750DaysOfCode 🚀 🔢 762. Prime Number of Set Bits in Binary Representation (LeetCode - Easy) Today’s problem was a beautiful mix of bit manipulation + prime checking. 🧠 Problem Summary Given two integers left and right, count how many numbers in that range have a prime number of set bits (1s) in their binary representation. 📌 Example: 21 → Binary: 10101 Set bits = 3 → 3 is prime ✅ 💡 Key Observations We need to: Convert each number to binary. Count the number of set bits. Check if that count is prime. Since right ≤ 10^6, the maximum number of set bits possible is 20 (because 2²⁰ ≈ 10⁶). 👉 So instead of checking prime again and again, we can pre-store prime numbers up to 20. 🔥 What I Practiced Today Using Integer.bitCount() efficiently Understanding binary representations deeply Optimizing prime checks for small ranges Clean looping logic 🎯 Time Complexity O(N * sqrt(K)) Where: N = right - left K ≤ 20 (max set bits) So practically very efficient 🚀 💬 Takeaway Sometimes an “Easy” problem is about combining two simple concepts correctly: 👉 Bit manipulation 👉 Prime number checking Consistency > Difficulty. #750DaysOfCode #Day511 #LeetCode #Java #BitManipulation #PrimeNumbers #ProblemSolving #DataStructuresAndAlgorithms
Prime Set Bits in Binary Representation (LeetCode Easy)
More Relevant Posts
-
Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 12/30 – LeetCode streak Today’s problem: Number of Steps to Reduce a Number in Binary Representation to One Rules are simple: if the number is even, divide by 2; if it’s odd, add 1. But doing this by simulating the whole number each time is slow and messy. The trick is to walk the binary string from right to left and just count how many operations each bit contributes, while carrying a 1 when we “add 1”: - Ignore index 0 (most significant bit); we stop at 'i > 0'. - For each bit, compute 'bit = (s.charAt(i) - '0') + carry'. - If 'bit' is '1', it’s effectively odd → we need '+1' then '/2' → 'steps += 2' and set 'carry = 1'. - If 'bit' is '0' or '2', it’s even → just '/2' → 'steps += 1', 'carry' stays as is. - After the loop, if there’s still a carry, add one more step because we effectively turned something like '"111"' into '"1000"' and need one last divide to reach '1'. Day 12 takeaway: Instead of repeatedly mutating the number, counting how many times each bit causes “+1 then /2” vs “just /2” gives an O(n), no-conversion solution that feels much cleaner once the carry idea clicks. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 67/100 – LeetCode Challenge ✅ Problem: #228 Summary Ranges Difficulty: Easy Language: Java Approach: Single Pass with Range Detection Time Complexity: O(n) Space Complexity: O(1) excluding output Key Insight: Track start of current consecutive range. When gap found (next ≠ previous + 1), close current range: Single number if start == previous Range "start → previous" otherwise Solution Brief: Iterated through array comparing consecutive elements. Detected breaks in consecutive sequences. Formatted ranges appropriately with arrow for multi-number ranges. Added final range after loop. #LeetCode #Day67 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #SummaryRanges #EasyProblem #RangeDetection #SinglePass #DSA
To view or add a comment, sign in
-
-
Day 8 Today I solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. At first glance, the problem looks simple: Given a binary string, check whether it contains only one continuous segment of '1's. Example: "110" → Valid (one segment of 1s) "1001" → Invalid (two separate segments of 1s) While solving it, I realized an interesting observation: 👉 If a binary string has more than one segment of 1s, the pattern "01" must appear before another "1". So instead of counting segments explicitly, we can simply check whether '01' appears and is followed by another '1'. This turns the problem into a very efficient linear scan with O(n) time complexity. 💡 Key takeaway: Sometimes the simplest solution comes from recognizing patterns in the string rather than simulating the whole process. Also happy to see my solution running at 0 ms runtime (100% faster) 🚀 #LeetCode #DSA #ProblemSolving #Java #CodingJourney #BinaryStrings
To view or add a comment, sign in
-
-
🚀 Day 21 of my #100DaysOfCode Journey Today, I solved the LeetCode question Contains Duplicate II. The task is to check if there are two equal elements in the array such that their index difference is at most k. ✅ Steps: First, iterate through each element in the array For every element, check only the next k elements Compare values within this limited range If a match is found → return true 💻 My Approach: I used a sliding window + brute force approach (without using HashMap/HashSet). Instead of checking the whole array, I optimized it by limiting the inner loop to k distance only. 🌟 Learning Takeaways: Optimizing brute force can avoid TLE Understanding constraints (like k distance) helps reduce complexity Multiple approaches exist — choosing based on space/time is key #DSA #Java #LeetCode #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 36 of #100DaysOfLeetCode ✅ Solved: Plus One (LeetCode 66) Difficulty: Easy Status: Accepted (114/114 Testcases Passed) Runtime: 0 ms 💯 Today’s problem looked simple but reinforced an important concept — handling carry in arrays. 🧠 Problem Summary: We are given a large integer represented as an array of digits. We need to increment the number by one and return the updated array. 🔎 Key Insight: Start from the last digit (right to left): If digit < 9 → increment and return. If digit == 9 → set it to 0 and carry over. If all digits are 9 → create a new array with an extra digit at the beginning. 💡 Example: Input: [9,9,9] Output: [1,0,0,0] 🎯 What I Practiced Today: Reverse traversal of arrays Carry-forward logic Edge case handling (all 9s case) Writing optimized O(n) solution Even easy problems strengthen fundamentals when you focus on edge cases. Consistency > Complexity. 🔥 #Day36 #100DaysOfCode #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
💻 Day 37 of #75DaysOfLeetCode Today’s problem: Remove Linked List Elements (Easy) 🔗 The task was to remove all nodes from a linked list that have a specific value. 🧠 Key Learning: The tricky part was handling edge cases — especially when the head node itself needs to be deleted. 💡 I used a dummy node approach, which made the solution clean and avoided special handling for the head. ⚙️ Approach: Create a dummy node pointing to head Traverse the list using a pointer Skip nodes where value == target Return dummy.next ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 🔥 What I learned today: Consistency matters more than difficulty. Even “easy” problems strengthen core concepts like pointers and edge cases. #Day38 #LeetCode #DataStructures #LinkedList #Java #CodingJourney #75DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 22 of Daily DSA 🚀 Solved LeetCode 66: Plus One ✅ Approach: Treat the array as a number and simulate addition from the last digit: • If the last digit is not 9, increment and return • Handle carry by setting 9 → 0 and moving left • If all digits are 9, create a new array with leading 1 This avoids converting the array into an integer and handles large numbers safely. ⏱ Complexity: • Time: O(n) • Space: O(1) (extra array only when overflow happens) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.58 MB (Beats 38.49%) A simple problem that tests edge-case thinking 💡 #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode #16 – 3Sum Closest After solving the classic 3Sum problem earlier, this variation pushed me to think slightly differently. Instead of finding an exact triplet sum equal to 0, this time the goal was to find the sum *closest* to a given target. 🔹 Approach: • Sorted the array • Fixed one element at a time • Applied the Two Pointer technique • Continuously tracked the minimum absolute difference Key Insight: Sometimes optimization problems are not about exact matches, but about minimizing deviation. Time Complexity: O(n²) Space Complexity: O(1) This problem strengthened my understanding of: ✔ Two Pointers ✔ Greedy comparison logic ✔ Edge case handling What I learned: Small variations in problem statements can completely change the thinking approach. Classic 3Sum focuses on exact zero. 3Sum Closest focuses on minimizing |sum - target|. #LearnInPublic #DSA #Java #LeetCode #TwoPointers #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development