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
LeetCode Day 25: Complement of Base 10 Integer
More Relevant Posts
-
💡 Day 38 of LeetCode Problem Solved! 🔧 🌟643. Maximum Average Subarray I🌟 Task : • You are given an integer array nums consisting of n elements, and an integer k. • Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. • Any answer with a calculation error less than 10-5 will be accepted. Example 1: Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75 Example 2: Input: nums = [5], k = 1 Output: 5.00000 #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 39 of my DSA consistency streak. Today’s problem: Number Complement. Approach: Instead of flipping bits one by one, build a mask of 1s matching the bit length of n, then use XOR to invert the bits efficiently. Example: 11 (1011) → mask 1111 → result 0100 (4) A simple problem that highlights how powerful bit manipulation techniques can be. #DSA #ProblemSolving #Java #LeetCode
To view or add a comment, sign in
-
-
Day 33/50 🚀 — Valid Palindrome (Two Pointer Approach) Today’s problem was a great mix of string manipulation + two pointers. 🔹 Ignored non-alphanumeric characters 🔹 Handled case-insensitivity 🔹 Compared characters from both ends efficiently Key insight: Instead of preprocessing the string, we can optimize in-place using two pointers, skipping unwanted characters on the go. 💡 This improves both readability and performance. Performance: ⚡ Runtime: 2 ms (99%+) 📦 Memory: Efficient #Day33 #LeetCode #TwoPointers #DSA #Java #CodingJourney #50DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day #82/100 – 𝐑𝐞𝐦𝐨𝐯𝐞 𝐄𝐥𝐞𝐦𝐞𝐧𝐭 (LeetCode) Today’s problem was Remove Element — a simple yet important question to understand in-place array manipulation. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: We don’t need extra space! We can solve this using a two-pointer approach efficiently. 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? We overwrite unwanted elements and keep only the valid ones at the beginning of the array. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Initialize k = 0 Traverse array: If element ≠ val → place it at index k Increment k Return k (new length) ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(1) #Day82 #100DaysOfCode #Java #DSA #LeetCode #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
Day 31/75 — Sum of Two Integers (Without + or -) Today's problem was about performing addition using bit manipulation. Key idea: • XOR (^) gives sum without carry • AND (&) + left shift gives carry Algorithm: while (b != 0): carry = (a & b) << 1 a = a ^ b b = carry Repeat until carry becomes zero. Time Complexity: O(1) Space Complexity: O(1) This problem gave a deeper understanding of how addition works at the binary level. 31/75 🚀 #Day31 #DSA #BitManipulation #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 68 — LeetCode Progress Problem: Check if an array is “special” — meaning every pair of adjacent elements has different parity (one even, one odd). Required: Given an integer array, return true if no two adjacent elements have the same parity, otherwise return false. Idea: If two neighboring numbers are both even or both odd, the condition fails immediately. So just compare parity of adjacent elements throughout the array. Approach: Traverse the array starting from index 1 For each element: Compare it with the previous element If both have same parity → return false If the loop completes → return true Time Complexity: O(n) Space Complexity: O(1) Clean, simple, and a good reminder that not every problem needs something fancy — sometimes it’s just about spotting the pattern. #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 17 Solved LeetCode 162 – Find Peak Element using Binary Search approach. ⚠️ Interesting Trap: I initially used the common condition while(left <= right), which caused an infinite loop in this problem. When left == right, mid = left, and right = mid doesn't change — so the loop never ends. ✅ Fix: Using while(left < right) ensures the pointers move correctly and the loop terminates. 💡 A small change in the loop condition can make a huge difference in algorithm behavior. ✔️ Time Complexity: O(log n) ✔️ Space Complexity: O(1) Accepted | Runtime: 0ms #Day17 #LeetCode #DSA #BinarySearch #Java #ProblemSolving
To view or add a comment, sign in
-
-
Day 62 of my 365-Day DSA Challenge 🔥 Solved LC 7 — Reverse Integer (Medium) | Runtime: 1ms ⚡ But here's the real story --> I got accepted on my first attempt, and then I spotted my own bug. My original solution had no overflow guard. The line: rev = rev * 10 + original % 10 ...could silently overflow an int, and I wouldn't even know it. LeetCode just happened not to hit that edge case hard enough. The fix? Check BEFORE multiplying: → If rev > MAX_VALUE / 10, it will overflow → return 0 → If rev == MAX_VALUE / 10 and next digit > 7 → return 0 (Same logic for the negative side with MIN_VALUE and -8) This is why understanding WHY your code works matters more than just seeing "Accepted." A passing test ≠ correct code. 365 days. One problem at a time. 💪 #DSA #LeetCode #Java #365DayChallenge #SoftwareEngineering #CodingJourney
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem: Reverse Linked List Today’s problem focused on reversing a singly linked list using an iterative approach. Approach: Used three pointers to reverse the links: prev → keeps track of the previous node curr → current node being processed next → stores the next node before changing the link Steps: Store curr.next in next Reverse the link → curr.next = prev Move prev and curr one step forward Continue until the list ends. Finally, prev becomes the new head of the reversed list. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List pointer manipulation Iterative reversal technique In-place algorithm #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 54 / 100 – LeetCode Challenge ✅ Solved: Same Tree (Easy) Today’s problem was about comparing two binary trees and checking whether they are structurally identical with the same node values. 🔍 Key Idea: Used Recursion to traverse both trees simultaneously: If both nodes are null → same If one is null → not same If values match → recursively check left & right subtrees 💡 This problem strengthened my understanding of: Tree Traversal Recursion Base Case Handling ⚡ Performance: ⏱ Runtime: 0 ms (Beats 100%) 💾 Memory: 43.07 MB Consistency is the key - one problem closer to the goal! 💪 #Day54 #LeetCode #DSA #BinaryTree #Java #CodingJourney #Consistency
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