🚀 LeetCode Day 18 – Complement of Base 10 Integer Today I solved LeetCode Problem 1009: Complement of Base 10 Integer using Java. 🧠 Problem: Given a non-negative integer n, return the complement of its binary representation. The complement means flipping all bits in the binary form of the number. 📌 Example: Input: n = 5 Binary of 5 → 101 Complement → 010 Output → 2 💡 Approach: • Create a bitmask with all bits set to 1 up to the highest bit of n. • Use the XOR (^) operator with the mask to flip the bits. 💻 Java Code: class Solution { public int bitwiseComplement(int n) { if (n == 0) return 1; int mask = 0, temp = n; while (temp > 0) { mask = (mask << 1) | 1; temp >>= 1; } return n ^ mask; } } ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) 📚 Practicing Data Structures & Algorithms daily to improve problem-solving skills. #LeetCode #Java #DSA #ProblemSolving #CodingJourney #100DaysOfCode
LeetCode 1009: Complement of Base 10 Integer in Java
More Relevant Posts
-
Day 75 - LeetCode Journey Solved LeetCode 21: Merge Two Sorted Lists in Java ✅ Moving from Strings to Linked Lists! Today’s challenge was about maintaining order while merging two separate data structures into one seamless, sorted list. The Approach: 1) Dummy Node Strategy: Started with a "dummy" node to act as the foundation of the new list, which simplifies edge cases where the head might change. 2) Iterative Comparison: Used a while loop to compare the values of the current nodes in both lists, always attaching the smaller value to our result list. 3) Pointer Management: Carefully advanced the pointers for both the source lists and the new merged list to ensure no data was lost. 4) Cleanup: Once one list was exhausted, I simply appended the remainder of the other list—since they are already sorted, no further comparisons were needed! Key Takeaways: ->Memory Efficiency: Merged the lists in-place by re-linking existing nodes rather than creating new ones. ->100% Runtime: Achieved a 0 ms runtime by focusing on a clean iterative approach. ->Pointer Logic: Reinforced the importance of keeping track of "next" references to prevent breaking the chain. Every pointer moved correctly is a step closer to mastering Linked Lists. Consistency is doing its work! 💪 #LeetCode #DSA #Java #LinkedList #Algorithms #ProblemSolving #CodingPractice #InterviewPreparation #Consistency #DailyCoding
To view or add a comment, sign in
-
-
🚀 Day 99 of My 100 Days LeetCode Challenge | Java Today’s problem was all about randomization and linked list traversal — a nice break from heavy DP and matrices. The challenge was to design a system that returns a random node’s value from a singly linked list, ensuring that every node has an equal probability of being chosen. Since linked lists don’t allow direct indexing, the key idea was to first determine the size of the list, and then generate a random index to fetch the corresponding node. This approach ensures uniform randomness while keeping the implementation simple and efficient. ✅ Problem Solved: Linked List Random Node ✔️ All test cases passed (8/8) ⏱️ Runtime: 11 ms 🧠 Approach: Linked List Traversal + Randomization 🧩 Key Learnings: ● Randomization problems require ensuring uniform probability distribution. ● Linked lists limit direct access, so traversal becomes essential. ● Precomputing size can simplify random selection. ● Sometimes simple approaches are the most effective. ● Understanding data structure limitations helps design better solutions. This problem highlighted how probability + data structures can come together in elegant ways. 🔥 Day 99 complete — sharpening my understanding of randomization and linked list behavior. #LeetCode #100DaysOfCode #Java #LinkedList #Randomization #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 87 - LeetCode Journey Solved LeetCode 382: Linked List Random Node in Java ✅ This problem is all about picking a random node where every node has equal probability, even when the size of the linked list is unknown. Used the concept of Reservoir Sampling to solve this efficiently. While traversing the list, each node gets a fair chance to be selected without storing all values. No extra space, single pass, and perfectly balanced randomness. Key takeaways: • Learned Reservoir Sampling technique • Handling unknown-sized data efficiently • Achieving equal probability without extra space • Strengthening linked list traversal concepts ✅ All test cases passed ⚡ Optimal O(n) time, O(1) space solution This is one of those problems that feels simple but teaches a powerful concept 🔥 #LeetCode #DSA #Java #LinkedList #ReservoirSampling #Algorithms #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 97 of My 100 Days LeetCode Challenge | Java Today’s problem was a solid exercise in matrix processing and prefix sum optimization. The goal was to count the number of submatrices whose sum is less than or equal to a given value (k). A brute-force approach would be too slow, so the key was to use 2D prefix sums to efficiently compute submatrix sums. By converting the matrix into a prefix sum matrix, we can calculate the sum of any submatrix in constant time, making the overall solution much more efficient. ✅ Problem Solved: Count Submatrices With Sum ≤ K ✔️ All test cases passed (859/859) ⏱️ Runtime: 7 ms 🧠 Approach: 2D Prefix Sum 🧩 Key Learnings: ● Prefix sums are powerful for optimizing repeated range sum queries. ● 2D prefix sums extend the same idea from arrays to matrices. ● Preprocessing can drastically reduce computation time. ● Avoiding brute force is key in large input problems. ● Matrix problems often become easier with the right transformation. This problem reinforced how preprocessing techniques like prefix sums can turn complex problems into efficient solutions. 🔥 Day 97 complete — sharpening matrix optimization and prefix sum skills. #LeetCode #100DaysOfCode #Java #PrefixSum #Matrix #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 91 of My 100 Days LeetCode Challenge | Java Today’s challenge was a clever string + frequency analysis problem. The task was to reconstruct the original digits (0–9) from a scrambled string containing the letters of their English names (like zero, one, two…). At first glance it looks messy because letters overlap across multiple numbers. The key insight was noticing that certain letters are unique to specific digits: z → zero (0) w → two (2) u → four (4) x → six (6) g → eight (8) By identifying these unique markers first, we can determine those digits and then subtract their letter counts to uncover the remaining ones. ✅ Problem Solved: Reconstruct Original Digits from English ✔️ All test cases passed (24/24) ⏱️ Runtime: 4 ms 🧠 Approach: Frequency Counting + Greedy Deduction 🧩 Key Learnings: ● Character frequency counting can simplify complex string problems. ● Unique identifiers often unlock greedy solutions. ● Solving certain elements first can reveal hidden dependencies. ● Careful ordering of deductions avoids incorrect counts. ● Many string puzzles become manageable with pattern observation. This problem was a great reminder that recognizing unique patterns can turn a confusing problem into a structured solution. 🔥 Day 91 complete — improving pattern recognition and string processing skills. #LeetCode #100DaysOfCode #Java #Strings #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 98 - #100DaysOfCode Today’s problem was all about string comparison and operations simulation in Java. 💡 Problem Insight: Given a list of operations like "++X", "X++", "--X", "X--", we need to compute the final value of X after performing all operations. ⚠️ One key learning today: In Java, always use .equals() for string comparison instead of ==. Using == compares references, not actual content — a very common mistake! 🧠 Approach: Initialize x = 0 Traverse through each operation Increment or decrement based on the operation string 📌 What I Improved Today: Better understanding of string handling in Java Avoiding common pitfalls in comparisons Writing cleaner conditional logic #Java #CodingJourney #LeetCode #100DaysOfCode #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 66 of #100DaysOfLeetCode 💻✅ Solved #3427. Sum of Variable Length Subarrays problem in Java. Approach: • Iterated through each index of the array • Determined the starting index using i - nums[i] • Ensured the start index does not go below 0 • Calculated sum of elements from start to current index i • Added each subarray sum to the total Performance: ✓ Runtime: 1 ms (Beats 99.90% submissions) ✓ Memory: 45.22 MB (Beats 56.30% submissions) Key Learning: ✓ Practiced handling variable-length subarrays ✓ Improved understanding of index-based range calculations ✓ Strengthened nested loop logic for array problems Learning one problem every single day 🚀 #Java #LeetCode #DSA #Arrays #PrefixSum #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 7 / 100 – LeetCode Challenge Today I solved Length of Last Word (LeetCode #58) using Java. 🔹 Problem: Given a string "s" containing words and spaces, return the length of the last word in the string. 📌 Approach: - Traverse the string from the end. - Ignore trailing spaces. - Start counting characters of the last word. - Stop when a space appears after counting begins. 💡 Key Concepts Practiced: - String traversal - "charAt()" usage - Reverse iteration - Conditional logic ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) ✅ Key Takeaway: Sometimes solving a problem becomes easier when we traverse the data from the end instead of the beginning. #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 24 📌 Problem: Two Sum II – Input Array Is Sorted 💻 Language: Java 🧠 Concept Used: Two Pointer Technique 🔍 Platform: LeetCode Today’s challenge was to find two numbers in a sorted array that add up to a given target and return their 1-indexed positions. The problem guarantees exactly one valid solution and requires constant extra space. Example: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Approach: ✔ Use two pointers — one at the start (left) and one at the end (right) ✔ Calculate the sum of both elements ✔ If the sum equals the target → return their indices ✔ If the sum is smaller → move left forward ✔ If the sum is larger → move right backward Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gcM_dBA7 🔗 Code: https://lnkd.in/gaUrS-Ne #100DaysOfCode #Day24 #Java #DSA #LeetCode #TwoPointers #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 64 of #100DaysOfLeetCode 💻✅ Solved #724. Find Pivot Index problem in Java. Approach: • Calculated the total sum of the array • Maintained a variable left to track left sum • For each index, computed right sum as total - left - nums[i] • Compared left and right sums • If both are equal, returned the index as pivot • Updated left sum by adding current element Performance: ✓ Runtime: 1 ms (Beats 98.68% submissions) 🚀 ✓ Memory: 47.58 MB (Beats 32.00% submissions) Key Learning: ✓ Learned prefix sum technique for efficient calculations ✓ Practiced reducing time complexity from O(n²) to O(n) ✓ Strengthened understanding of array traversal and sum logic Learning one problem every single day 🚀 #Java #LeetCode #DSA #Arrays #PrefixSum #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
Explore related topics
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