🔥 Day 508 of #750DaysOfCode 📌 Problem: Binary Number with Alternating Bits (LeetCode 693) Today’s challenge was to determine whether a given positive integer has alternating bits in its binary representation. 👉 A number has alternating bits if no two adjacent bits are the same. 🧠 Example: 5 → 101 ✅ (Alternating → True) 7 → 111 ❌ (Not alternating → False) 11 → 1011 ❌ (Last two bits same → False) 💡 Key Insight If a number has alternating bits: XOR it with itself right-shifted by 1 The result should be all 1s A number with all 1s has the property: 🔎 Why This Works? Example: n = 5 → 101 n = 101 n >> 1 = 010 XOR = 111 Now check: 111 & 1000 = 0 Hence ✅ True. ⏱ Time Complexity: O(1) 📦 Space Complexity: O(1) Day 508 completed. Still building consistency. Still sharpening problem-solving. 💪 On to Day 509 🚀 #750DaysOfCode #Day508 #LeetCode #Java #DSA #BitManipulation #ProblemSolving #CodingJourney #Consistency #TechGrowth
Alternating Bits in Binary Representation
More Relevant Posts
-
Day 25/30 – LeetCode streak Problem: Complement of Base 10 Integer You need to flip all bits in 'n'’s binary representation, but only up to its most significant '1' (no leading zeros). Core idea * The complement should only flip bits within the bit-length of 'n'. For example, '5' is '101₂'; its complement is '010₂' = '2', not some infinite stream of ones. * Build a mask of the form '111...1' that has the same length as 'n' in binary. Then 'n ^ mask' flips exactly those bits. * Special case: if 'n == 0', binary is "0" and complement is "1", so return '1'. Day 25 takeaway: This is a clean bitmask + XOR pattern: instead of flipping bits one-by-one, construct a full '111...1' mask up to the MSB and XOR once to get the complement. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
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 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 99/100 – LeetCode Challenge Today’s problem was 673. Number of Longest Increasing Subsequence. Given an integer array nums, the goal is to determine how many longest strictly increasing subsequences exist in the array. Unlike the classic Longest Increasing Subsequence (LIS) problem where we only find the length, this problem requires counting how many subsequences achieve that maximum length. 💡 Approach I used Dynamic Programming with two arrays: dp[i] → stores the length of the longest increasing subsequence ending at index i count[i] → stores the number of LIS ending at index i For every pair (j, i) where j < i: If nums[i] > nums[j], it can extend the subsequence. If we find a longer subsequence → update length and copy count. If we find another subsequence of the same length → add counts. Finally, we sum the counts of all subsequences whose length equals the maximum LIS length. ⏱ Complexity Time Complexity: O(n²) Space Complexity: O(n) This problem deepens the understanding of dynamic programming transitions and counting paths, which is a very common pattern in advanced DP problems. On to the final day tomorrow! 💯🔥 #Day99 #100DaysOfLeetCode #LeetCode #DSA #DynamicProgramming #CodingChallenge #Java #SoftwareEngineering #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 14/100 – LeetCode Challenge Problem: Linked List Cycle II Today’s problem focused on detecting the node where a cycle begins in a linked list. Approach: Used Floyd’s Cycle Detection Algorithm (Slow & Fast pointers). Move slow by one step and fast by two steps. If they meet, a cycle exists. Reset slow to head. Move both slow and fast one step at a time. The node where they meet again is the starting node of the cycle. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List traversal Floyd’s cycle detection Pointer manipulation #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge Problem: Palindrome Number Today’s problem focused on number manipulation without converting the integer into a string. Approach: If number is negative → return false Reverse the digits using modulo (% 10) and division (/ 10) Compare reversed number with original number Logic Used: Extract last digit: rem = x % 10 Build reversed number: rev = rev * 10 + rem Remove last digit: x = x / 10 Complexity: Time: O(log₁₀ n) Space: O(1) Key takeaway: Problems involving digits can often be solved mathematically without string conversion. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 73/100 of my DSA Journey ✅ Problem solved today: LeetCode 1461 – Check If a String Contains All Binary Codes of Size K 🧠 What I focused on: Using a HashSet to store all substrings of length k Sliding window technique to generate substrings efficiently Understanding why we only need to check if the number of unique substrings equals 2^k Handling edge cases when string length is smaller than k This problem nicely combined strings + sliding window + hashing logic. 📌 Key takeaway: Sometimes the solution is just counting unique patterns efficiently. 📈 Day 73 done. Consistency continues. #LeetCode #DSA #SlidingWindow #Strings #Hashing #Java #ProblemSolving #Consistency #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
Day 115 🚀 | LeetCode Progress Solved 3 problems today 💪 📌 Problem 1: First Bad Version (#278) 👉 Binary search approach ✅ 👉 Efficiently finds the first bad version in O(log n) 👉 Key trick: mid = left + (right - left)/2 to avoid overflow 📌 Problem 2: Set Matrix Zeroes (#73) 👉 Mark rows & columns using boolean arrays 👉 Two-phase approach ensures in-place zeroing 👉 Time: O(m*n), Space: O(m+n) 📌 Problem 3: Reshape the Matrix (#566) 👉 Map original matrix elements to new r x c matrix 👉 Row-wise filling with careful index tracking 👉 Handles invalid reshape by returning original matrix Consistency builds mastery. Every problem solved = step closer to expertise 🚀 #Day115 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #GeekStreak
To view or add a comment, sign in
-
🚀 Day 95 of #100DaysOfCode Solved LeetCode #868 – Binary Gap ✅ A clean and elegant bit manipulation problem focused on tracking distances between set bits in a binary representation. Key Takeaways: -> Using bitwise operations (&, >>) for constant-time checks -> Tracking previous set-bit positions efficiently -> Single-pass solution with optimal performance -> Simple logic, strong fundamentals 💡 Language: Java -> Runtime: 0 ms (Beats 100%) ⚡ -> Memory: 42.34 MB Closing in on the finish line — consistency really compounds. 💻🔥 #LeetCode #Java #BitManipulation #Binary #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 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
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