🚀 Day 56 Out of #365DaysOfCode Github Link: https://lnkd.in/gGUy_MKZ Today I solved an interesting problem sort an integer array based on the number of 1’s in its binary representation. If two numbers have the same number of set bits, sort them in ascending numerical order. 🔎 Key Concept: Use Integer.bitCount() to count the number of 1’s. Apply a custom comparator with Arrays.sort(). Handle tie-breaking by normal ascending comparison. 💡 This problem is a great reminder of how powerful Java’s built-in methods and custom comparators can be for writing clean and efficient solutions. Time Complexity: O(n log n) It’s always exciting to explore bit manipulation problems—they strengthen logical thinking and deepen understanding of how numbers work internally. #Java #Coding #DataStructures #Algorithms #BitManipulation #ProblemSolving #Learning
Java Bit Manipulation: Sorting Array by Binary 1's
More Relevant Posts
-
Binary Search is a powerful algorithm that efficiently finds an element in a sorted array by repeatedly dividing the search space in half. This approach significantly reduces time complexity compared to linear search. ✅ Problem: Binary Search 💻 Language: Java ⚡ Time Complexity: O(log n) Consistent practice is the key to mastering Data Structures & Algorithms. #LeetCode #BinarySearch #DSA #Java #Coding #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Day 90 of My 100 Days LeetCode Challenge | Java Today’s challenge was a fascinating logic + mathematical insight problem. The task was to generate a binary string that does not exist in a given list of binary strings, where all strings have the same length. Instead of trying every possible combination, the solution becomes much simpler when you use the idea of diagonal flipping. By flipping the i-th bit of the i-th string, we guarantee that the newly created string differs from every string in the list at least in one position. This clever trick ensures the generated string is always unique. ✅ Problem Solved: Find Unique Binary String ✔️ All test cases passed (186/186) ⏱️ Runtime: 0 ms 🧠 Approach: Mathematical Insight (Diagonal Construction) 🧩 Key Learnings: ● Sometimes the smartest solutions come from mathematical reasoning rather than brute force. ● The diagonalization technique guarantees uniqueness. ● Understanding problem constraints can drastically simplify the approach. ● Logical insights can reduce exponential possibilities to linear solutions. ● Elegant algorithms often come from simple observations. This problem was a great reminder that a small logical trick can outperform complex algorithms. 🔥 Day 90 complete — sharpening my problem-solving intuition and algorithmic thinking. #LeetCode #100DaysOfCode #Java #Math #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day47 🚀 DSA Spotlight: Find Duplicate Characters in a String (with Count) in Java Today I solved a classic string problem using a basic brute-force approach 👨💻 🔍 Problem Statement: Given a string, identify all duplicate characters along with their frequency. 🧩 Example Input: "akashkulal" 📊 Output: a → 4, k → 2, l → 2 💡 Approach Used (Beginner-Friendly): ✔ Convert string to character array ✔ Compare each character with the rest (nested loop) ✔ Mark visited characters to avoid repetition ✔ Print characters with count > 1 ⏱️ Time Complexity: O(n²) 📦 Space Complexity: O(1) (No extra data structures used) ✨ Key Learning: Even without advanced data structures like HashMap, we can solve problems using strong fundamentals. Mastering these basics builds a solid foundation for optimized solutions later! 🔥 Next Step: Try optimizing this using HashMap to reduce time complexity to O(n) #Java #DSA #CodingPractice #BeginnerFriendly #ProblemSolving #TechInterview #100DaysOfCode #Developers #LearningJourney
To view or add a comment, sign in
-
🚀 Day 85 of My 100 Days LeetCode Challenge | Java Today’s problem was a beautiful recursion + pattern observation challenge. We had to find the k-th bit in a recursively defined binary string sequence — and the string grows exponentially with each step. Clearly, constructing the entire string was not an option. The breakthrough came from understanding the structure: Each sequence is built as: S(n) = S(n-1) + "1" + reverse(invert(S(n-1))) Once I noticed the symmetry around the middle element and how the second half mirrors the first (with inversion), the solution became a clean recursive reduction instead of brute force. ✅ Problem Solved: Find K-th Bit in N-th Binary String ✔️ All test cases passed (63/63) ⏱️ Runtime: 0 ms (Beats 100%) 🧠 Approach: Recursion + Divide & Conquer + Symmetry Observation 🧩 Key Learnings: ● Never build exponential structures directly — reduce the problem. ● Recursive definitions often hide symmetry. ● The middle element becomes a powerful pivot. ● Mirroring + inversion patterns simplify complex generation problems. ● Think structurally, not sequentially. This problem reminded me that many recursive constructions are just cleverly disguised divide-and-conquer problems. 🔥 Day 85 complete — stronger recursion intuition and pattern recognition sharpening further. #LeetCode #100DaysOfCode #Java #Recursion #DivideAndConquer #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 8 – LeetCode Practice Today, I solved the Search Insert Position problem on LeetCode: 🎯 Difficulty: Easy 💻 Language Used: Java 💡 Approach: • Given a sorted array and a target value, the task was to return the index where the target should be inserted if it is not found. • I used an efficient binary search approach to locate the correct position in O(log n) time. • If the target exists, return its index; otherwise, return the index where it would be inserted to maintain sorted order. ⏱ Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) 📚 Key Takeaway: This problem reinforced how binary search can be slightly adapted to both find and insert positions in a sorted array without scanning the entire list — an essential optimization in sorted data structures. Consistent problem-solving practice continues to build intuition for time-efficient algorithms and clean implementations. 💪 #LeetCode #Java #DSA #BinarySearch #Algorithms #ProblemSolving #CodingPractice #100DaysOfCode #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Tackling algorithmic challenges one substring at a time! Just solved the “Number of Substrings Containing All Three Characters” problem in Java using a sliding window approach. ✨ Key takeaway: Efficient solutions often come from thinking in terms of windows and counts rather than brute force. 💻 Output validated with test cases — clean, optimized, and accepted! #Java #CodingChallenge #ProblemSolving #DataStructures #Algorithms #LeetCode #ProgrammingJourney
To view or add a comment, sign in
-
-
Today I solved the Unique Binary String problem on LeetCode using two different approaches in Java. • Method 1 (Optimized – Cantor’s Diagonal Approach) Constructed a new binary string by flipping the i-th bit of the i-th string. This guarantees the generated string differs from every string in the array at least at one position. Time Complexity: O(n) Space Complexity: O(n) (for the result string) • Method 2 (Brute / Less Optimized) Converted each binary string to its decimal representation, stored them, and checked for a number whose binary form (length n) is not present in the list. Time Complexity: O(n²) Space Complexity: O(n) Exploring multiple approaches to the same problem helps strengthen algorithmic thinking and optimization skills. #LeetCode #Java #Algorithms #ProblemSolving #CodingPractice #DataStructures
To view or add a comment, sign in
-
-
🚀 DSA Learning Journey | Day 3 | Java Solved “Repeat and Missing Number in Array.” 💡 Key Idea: Used mathematical relationships between sum and sum of squares to identify the repeating and missing numbers efficiently. ⚙ Implementation • Language: Java • Time Complexity: O(n) • Space Complexity: O(1) 📚 Learning how mathematical observations can help solve array problems efficiently. #Java #DSA #LeetCode #ProblemSolving #JavaDeveloper #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 8 – LeetCode Practice Today, I solved the Search in Rotated Sorted Array problem on LeetCode: 🎯 Difficulty: Medium 💻 Language Used: Java 💡 Approach: • Given a rotated sorted array and a target value, the task was to find the index of the target if it exists — otherwise return -1. • I used a modified binary search to find the target in O(log n) time. • At each step, I determined which half of the array was sorted and used that information to decide where the target could lie. • This careful range narrowing allowed efficient searching even after rotation. ⏱ Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) 📚 Key Takeaway: This problem reinforced how binary search can be adapted beyond simple sorted arrays to handle rotated scenarios by identifying sorted subranges at each step. Consistent problem-solving practice continues to sharpen my algorithmic intuition and implementation skills. 💪 #LeetCode #Java #DSA #BinarySearch #Algorithms #ProblemSolving #CodingPractice #100DaysOfCode #SoftwareDeveloper
To view or add a comment, sign in
-
-
Daily DSA Update – Day 26 Solved: Add Binary (LeetCode) Today’s problem was about adding two binary strings and returning their sum as a binary string. Problem: Given two binary strings a and b, return their sum as a binary string. Approach: I simulated the same process we use while doing binary addition manually. Starting from the end of both strings, I added corresponding digits along with a carry value. After calculating the sum, I appended the result bit and updated the carry. Finally, I reversed the result to get the correct binary order. Key Learning: Problems like this highlight the importance of understanding how operations work internally rather than relying on built-in conversions. What this strengthened: • String traversal from right to left • Handling carry in binary operations • Using StringBuilder for efficient string manipulation • Implementing mathematical logic in code Each daily problem adds a small but meaningful improvement in problem-solving ability. On to the next challenge. #DSA #DataStructures #Algorithms #Java #LeetCode #ProblemSolving #CodingJourney #TechLearning
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