29/30 days ✅ Solved a LeetCode Problem #231 Power of Two — Bit Manipulation Logic in Java Solved the Power of Two problem on LeetCode! The challenge is to determine whether a given integer n is a power of two. The key insight lies in understanding the binary representation of numbers that are powers of two — they contain exactly one set bit (1). So, when we perform the operation n & (n - 1), it turns off the only set bit of n. For any power of two, this result becomes 0. Therefore, the condition n > 0 && (n & (n - 1)) == 0 accurately checks if a number is a power of two. This simple yet powerful bitwise trick is both efficient and elegant — a great reminder of how understanding binary operations can make problem-solving in programming much faster! ⚡ #Java #LeetCode #ProblemSolving #BitManipulation #CodingJourney #LearningEveryday
Solved Power of Two problem on LeetCode using Java
More Relevant Posts
-
Day 23 of #50DaysOfCode – Java 💻 Today’s challenge was to check whether a number is a Perfect Number. A Perfect Number is a number that is equal to the sum of all its positive divisors (excluding itself). Example: 6 → 1 + 2 + 3 = 6 28 → 1 + 2 + 4 + 7 + 14 = 28 This problem helped me strengthen my understanding of loops, conditions, and mathematical logic 🔍✨ #Java #CodingChallenge #50DaysOfCode #LearnToCode #ProgrammingBasics #LogicBuilding #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
🚀Day 97/100 #100DaysOfLeetCode 👩💻Problem: Valid Square✅ 💻Language: Java 💡Approach: To check if four given points form a valid square, I calculated all six pairwise distances between the points. 🔹A valid square must have two distinct distances — 4 equal smaller sides and 2 equal longer diagonals. 🔹Used a HashMap to count occurrences of each distance. 🔹If the map has exactly two distinct non-zero distances and the smaller one appears 4 times while the larger appears 2 times, it’s a square! 🧠Key Takeaways: 🔹Strengthened understanding of geometry-based problems. 🔹Reinforced hashing techniques for quick frequency checks. 🔹Improved logic for pairwise comparison and distance calculation. ⚙️Performance: ⏱️Runtime: 2 ms (Beats 27.27%) 💾Memory: 41.91 MB (Beats 22.03%) #100DaysOfLeetCode #Java #CodingChallenge #LeetCode #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🌳 Day 68 of Posting on LinkedIn LeetCode 112. Path Sum — Java Solution Today’s problem checks whether a root-to-leaf path exists in a binary tree such that the sum of the node values equals a target sum. This is a classic DFS (Depth-First Search) problem. We recursively explore left and right subtrees, subtracting the node’s value from the target at each step. When we reach a leaf node, we simply verify if the remaining sum matches the leaf’s value. ✅ Approach Use DFS recursion. At each node, subtract its value from the target sum. When a leaf node is reached, check if the remaining sum is zero. Return true if any path satisfies the condition. 🧠 Key Takeaways Classic DFS problem on binary trees Base case: checking leaf nodes Use subtraction to track required remaining sum #leetcode #leetcode112 #dsa #coding #java #binarytrees #programming #tech #interviewprep #softwareengineering #100daysofcode
To view or add a comment, sign in
-
-
🚀 Day 52 | DSA with Java Today, I practiced the Painters Partition Problem — a direct variation of the Book Allocation Problem, based on Binary Search on Answers. 🎨📚 --- 🔹 Problem Statement: You are given n boards of lengths arr[] and k painters. Each painter paints contiguous boards, and all painters take the same time per unit length. Find the minimum time required to paint all boards if painters work simultaneously. --- ⚙️ Approach – Binary Search on Answers The possible minimum time lies between max(arr) and sum(arr). Use Binary Search to find the least time so that all boards can be painted by k painters. The check function ensures painters are not overloaded beyond mid time. Time Complexity: O(n × log(sum(arr))) Space Complexity: O(1) --- 🧠 Learning Outcomes: ✅ Strengthened understanding of Binary Search on Answers ✅ Learnt how to apply feasibility checks to partition-based problems ✅ Similar pattern to Book Allocation and Split Array Largest Sum --- #DSA #Java #BinarySearch #PaintersPartition #ProblemSolving #100DaysOfCode #GitHub #Programming #LogicBuilding
To view or add a comment, sign in
-
-
🌟 Day 86 of #100DaysOfCodingChallenge 🌟 🚀 Problem: 561. Array Partition 💻 Language: Java Today’s challenge was about maximizing the sum of the minimum values in n pairs formed from an array of 2n integers. 🧩 Approach: 1️⃣ Sort the array in ascending order. 2️⃣ Take every alternate element (starting from index 0). 3️⃣ Add them up — this gives the maximum possible sum of mins in all pairs. 🧠 Key Insight: When the array is sorted, pairing adjacent elements ensures the smallest numbers are always matched optimally, leading to the maximum possible result. 📊 Example: Input → [1,4,3,2] Sorted → [1,2,3,4] Pairs → (1,2), (3,4) Output → 4 ✅ ✨ Learning: Sometimes the simplest approach — sorting and selecting — can yield the most optimal result! #Day86 #LeetCode #Java #100DaysOfCode #CodingChallenge #ProblemSolving #ArrayPartition #DSA #WomenInTech #SathyabamaUniversity
To view or add a comment, sign in
-
-
🧩 Day 79 of #100DaysOfCode Today’s problem: LeetCode 3370 – Smallest Number With All Set Bits 💡 The challenge was to find the smallest number greater than or equal to n whose binary representation contains only set bits (1s). 💻 Language: Java ⚙️ Approach: Used bitwise operations to identify numbers whose binary form contains all 1s. The key condition used: (x & (x + 1)) == 0 This ensures that the number has all bits set. ✅ Result: Runtime – 0 ms, beating 100% of Java submissions! ⚡ 📈 Learning: This problem improved my understanding of bit manipulation and binary operations — core concepts for optimizing code performance. #Day79 #LeetCode #100DaysOfCode #CodingChallenge #JavaProgramming #ProblemSolving #BitManipulation #DeveloperJourney #CodeEveryday
To view or add a comment, sign in
-
-
🚀 Day 94 of #100DaysOfCode 🧠 Solved "Reverse String II" in Java using a clear two-pointer approach with modular design. 🔹 Problem: • Given a string s and an integer k, reverse the first k characters for every block of 2k characters from the start. • If fewer than k characters remain, reverse all of them. • If between k and 2k remain, reverse only the first k. 🔹 Approach: • Convert the string into a char[] for in-place editing. • Iterate in steps of 2k. • For each block, find left and right boundaries (right = min(left + k - 1, n - 1)). • Reverse the section using a helper method with two pointers. • Return the new string after all blocks are processed. 🔹 Key Learning: • Renaming pointers to left and right improves readability for two-pointer logic. • Modularizing reversal logic into a helper method makes the code cleaner and reusable. • Handling partial segments (less than k left) becomes easy with boundary checks. 🔹 Thanks to: • Dr Bharathi Raja N — Director of Training & Placement, Dhanalakshmi College of Engineering • Sreesairam V Sir — Aptitude Guru Hem #DhanalakshmiCollegeOfEngineering #LeetCode #Java #100DaysOfCode #Day94 #TwoPointers #StringManipulation #CodingJourney
To view or add a comment, sign in
-
-
Solved “Palindrome Number” in Java today. A classic problem, but always a good one for working on logic clarity and string manipulation skills. ✅ 11,511 / 11,511 test cases passed ⚡ Runtime: 6 ms (Beats 27%) 💾 Memory: Beats 89.5% The challenge: check if an integer reads the same backward and forward. The key: converting to the int to a string and using a two-pointer approach from both ends — clean, simple, and effective. Not the fastest runtime, but a small win for writing something readable, reliable, and authentic. #LeetCode #Java #Programming #StudentDeveloper #CodingJourney #KeepLearning
To view or add a comment, sign in
-
-
Finally implemented the 0/1 Knapsack problem using Dynamic Programming in Java 🧠💻 While revisiting classic algorithms, I decided to go old school with Java — and this time, I went deep into the Knapsack problem. It’s fascinating how such a seemingly simple problem teaches you so much about: Optimal substructure Overlapping subproblems And most importantly, how dynamic programming can turn exponential recursion into an elegant bottom-up solution. Watching those arrays fill up and tracing back the solution always gives that satisfying “it finally clicked” moment. 😅 Sometimes, revisiting the basics is the best way to sharpen your fundamentals. Have you ever gone back to re-implement classic DSA problems after years of working in real-world projects? #Java #DynamicProgramming #Algorithms #DSA #ProblemSolving #Knapsack
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
Nice bit manipulation insight. This same `n & (n-1)` trick is incredibly useful in RTL design for power-of-two detection in address decoders and memory controllers — I've implemented similar logic countless times in SystemVerilog for parameterizable bus widths and buffer sizing.