#200DaysOfCode – Day 116 Problem: Minimum Bit Flips to Convert Number Task: Given two integers start and goal, determine the minimum number of bit flips required to convert start into goal. Example: Input: start = 10, goal = 7 Output: 3 My Approach: Used XOR (^) to identify positions where bits differ. Counted the number of 1s in the XOR result, since each 1 represents a required bit flip. Implemented the solution using: Bitwise operations A fixed 32-bit loop (Java int size) Complexity: Time Complexity: O(1) Space Complexity: O(1) Bit manipulation problems often look tricky, but once you understand XOR and bit counting, the solution becomes elegant and efficient. Sometimes, knowing the fundamentals deeply beats overcomplicating the logic. #200DaysOfCode #Java #BitManipulation #XOR #LeetCode #ProblemSolving #DataStructures #Algorithms #CodeNewbie #TakeUForward #Consistency
Min Bit Flips to Convert Numbers with Java XOR
More Relevant Posts
-
Day 57/100 – LeetCode Challenge ✅ Problem: Longest Balanced Substring I Difficulty: Medium Language: Java Approach: Brute Force with Frequency Array Time Complexity: O(n² × 26) ≈ O(n²) Space Complexity: O(26) → O(1) Key Insight: A balanced substring requires all occurring characters to have the same frequency. For each starting index, expand substring while tracking character counts. Check if all non-zero frequencies are equal. Solution Brief: Nested loops to generate all substrings. Used fixed-size frequency array (26) for lowercase letters. Helper function issamefreq() verifies all non-zero counts are identical. Tracked maximum valid substring length. #LeetCode #Day57 #100DaysOfCode #String #Java #Algorithm #CodingChallenge #ProblemSolving #LongestBalancedSubstring #MediumProblem #BruteForce #FrequencyArray #Substring #DSA
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 118 Problem:- Find XOR of numbers from L to R Task: Given two integers L and R, find the XOR of all numbers in the range ([L, R]). Example: Input: L = 4, R = 8 Output: 8 My Approach: Observed the repeating pattern of XOR results from 1 to N based on N % 4. Created a helper function to compute XOR from 1 to N in constant time. Used the formula: XOR(L to R) = XOR(1 to R) ^ XOR(1 to L-1) Complexity Analysis: Time Complexity: O(1) Space Complexity: O(1) Understanding patterns in bitwise operations can turn what looks like an iterative problem into a constant-time solution. Sometimes, smart math + XOR properties = elegant code #takeUforward #200DaysOfCode #Java #ProblemSolving #GeeksForGeeks #BitManipulation #XOR #DSA #CodingJourney #CodeNewbie
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 117 Subsets (Power Set) using Bit Manipulation Problem: LeetCode 78 – Subsets Task:- Given an integer array of unique elements, return all possible subsets (the power set). Example: Input: nums = [1, 2, 3] Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]] My Approach (Bit Manipulation): Observed that for an array of size n, total subsets = 2^n. Used numbers from 0 to (2^n - 1) as binary masks. Each bit position represents whether an element is included (1) or excluded (0) in the subset. Iterated through each mask and built subsets accordingly. Complexity: Time Complexity: O(n * 2^n) Space Complexity: O(2^n) Bit manipulation is not just about low-level tricks it can provide elegant and efficient solutions to problems like generating subsets. Once you see the binary pattern, the solution becomes surprisingly intuitive. #LeetCode #Java #BitManipulation #DSA #ProblemSolving #TakeUForward #CodingJourney #200DaysOfCode #PowerSet #CleanCode
To view or add a comment, sign in
-
-
Day 15 of #200daysofdsa LeetCode 13 | Roman to Integer (Java) Solved this problem using a HashMap + traversal from right to left. Key logic: If current value < next value → subtract Else → add *>Time Complexity: O(n) *>Space Complexity: O(1) (fixed map size) *>Clean handling of subtractive cases like IV, IX, XL, XC, CD, CM Consistency is the real progress #LeetCode #DSA #Java #Strings #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 28 - Maximum Subarray Implemented a linear-time dynamic approach to compute the maximum contiguous subarray sum. Maintained: sum→ best subarray ending at current index maxsum → global maximum Reset accumulation when extending the previous segment becomes suboptimal. Achieved optimal O(n) time with constant space. #Day28 #ProblemSolving #Algorithms #InterviewPrep #Java
To view or add a comment, sign in
-
-
Day 69/100 – LeetCode Challenge ✅ Problem: #229 Majority Element II Difficulty: Medium Language: Java Approach: Extended Boyer-Moore Voting Algorithm Time Complexity: O(n) Space Complexity: O(1) Key Insight: At most two elements can appear more than ⌊n/3⌋ times. Track two candidates and their counts simultaneously. When a new element matches neither candidate, decrement both counts. Solution Brief: Phase 1: Find two potential candidates: Maintain two candidates and their counts Update based on matches or count resets Phase 2: Verify both candidates actually appear > n/3 times Add valid candidates to result list. #LeetCode #Day69 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #MajorityElementII #MediumProblem #BoyerMoore #VotingAlgorithm #DSA
To view or add a comment, sign in
-
-
Day 7 of Daily DSA 🚀 Solved LeetCode 167: Two Sum II – Input Array Is Sorted Approach: Used the two-pointer technique leveraging the sorted nature of the array. Moved pointers inward based on the comparison with the target to find the answer in one pass. Complexity: • Time: O(n) • Space: O(1) (constant extra space) LeetCode Stats: • Runtime: 2 ms (Beats 96.10%) • Memory: 48.54 MB (Beats 38.87%) This problem highlights how understanding input constraints can turn a brute-force solution into an optimal one. #DSA #LeetCode #Java #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Optimizing Sorting Logic Using Bit Manipulation (Java) Today I solved a problem that required sorting integers based on the number of set bits in their binary representation. Instead of using a direct sorting approach, I implemented a custom comparator with a PriorityQueue and used Brian Kernighan’s algorithm to count set bits efficiently: n = n & (n - 1) This reduces time complexity for counting bits to O(number of set bits). 📊 Result: • 77/77 test cases passed • Runtime: 7ms • Beat 78% of submissions Key takeaway: Optimization is not just about solving — it’s about choosing the right approach and reducing unnecessary operations. Bit manipulation continues to be one of the most powerful tools in algorithm design. #DSA #Java #BitManipulation #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
After permutations, I worked on generating binary strings without consecutive 1s. This problem introduced something important — constraint control. Instead of blindly generating all combinations, I had to prevent invalid states during recursion itself. What became clear here: - Recursion trees can be pruned early. - Constraints should be checked before making the next call. - Smart branching reduces unnecessary work. - Not all recursion problems require exploring every path. Core logic : if (n == 0) { System.out.println(current); return; } binary(n - 1, current + "0"); if (lastChar != '1') { binary(n - 1, current + "1"); } This was different from earlier problems. Now recursion wasn’t just exploring — it was filtering while exploring. That control makes a big difference in efficiency. #recursion #java #dsajourney #backtracking #problemSolving
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟳/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲🎯 Solved Longest Valid Parentheses ➤ Problem: Given a string of ‘(’ and ‘)’, determine the length of the longest well-formed (valid) parentheses substring. ➤ Approach: Used a clean and structured stack-based technique to identify valid boundaries efficiently. • Initialize the stack with -1 as the base index. • Push every '(' index onto the stack. • On encountering ')', pop to attempt a match. • If the stack becomes empty, push the current index as the new starting point. • Otherwise, compute the valid substring length using currentIndex − stackTop. Continuously update the maximum length. This ensures each character is processed in a single pass, maintaining optimal efficiency. #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #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