🚀 Day 16 of #100DaysOfCode Today’s problem: Minimum Absolute Difference 📊 🔍 Problem Understanding: Given an array of distinct integers, find all pairs with the minimum absolute difference. 🧠 Approach: First, I sorted the array Then compared adjacent elements (since minimum difference will always be between neighbors in sorted order) Tracked the minimum difference and stored all valid pairs 👉 Key Insight: Sorting reduces unnecessary comparisons and simplifies the problem ⚙️ Concepts Used: Sorting (Arrays.sort) Greedy observation (check only neighbors) List handling for storing pairs 📊 Complexity: ⏱️ Time Complexity: O(N log N) (due to sorting) 💾 Space Complexity: O(N) (for storing result pairs) 📚 Key Learnings: Learned how sorting simplifies problems Understood how to reduce complexity from brute force O(N²) → O(N log N) Improved thinking in terms of patterns and optimization 💯 Result: ✔️ Accepted (All test cases passed) ✔️ Runtime: 1 ms (100%) 🔥 ✔️ Clean and efficient solution Sometimes the best optimization is just sorting + observation 💡 Let’s keep building consistency 🚀 #Day16 #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving #Sorting
Minimum Absolute Difference in Array with Sorting and Observation
More Relevant Posts
-
🚀 Day 7 – Exception Handling: More Than Just try-catch Today I focused on how exception handling should be used in real applications—not just syntax. try { int result = 10 / 0; } catch (Exception e) { System.out.println("Error occurred"); } This works… but is it the right approach? 🤔 👉 Catching generic "Exception" is usually a bad practice 💡 Better approach: ✔ Catch specific exceptions (like "ArithmeticException") ✔ Helps in debugging and handling issues more precisely ⚠️ Another insight: Avoid using exceptions for normal flow control Example: if (value != null) { value.process(); } 👉 is better than relying on exceptions 💡 Key takeaway: - Exceptions are for unexpected scenarios, not regular logic - Proper handling improves readability, debugging, and reliability Small changes here can make a big difference in production code. #Java #BackendDevelopment #ExceptionHandling #CleanCode #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 43 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sum of Unique Elements Problem Insight: Find elements in an array that appear exactly once and calculate their total sum. Approach: • Used a frequency array to count occurrences of each number • Traversed the array to build frequency • Added only those elements to sum whose frequency is exactly 1 Time Complexity: • O(n) Space Complexity: • O(1) (fixed size array used for constraints) Key Learnings: • Frequency array is faster than HashMap when range is fixed • Two-pass approach makes logic clear and simple • Always check constraints before choosing data structure Takeaway: Right data structure makes the solution simple and efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 70/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Reverse Linked List II A neat variation of linked list reversal where only a specific portion of the list is reversed. Problem idea: Reverse a linked list from position left to right, keeping the rest of the list unchanged. Key idea: In-place reversal using pointer manipulation. Why? • We don’t reverse the whole list, only a segment • Need to reconnect the reversed part correctly • Must carefully track boundaries (left and right) How it works: • Use a dummy node to handle edge cases • Move a pointer to the node just before left • Start reversing nodes one by one within the range • Adjust links to insert nodes at the front of the sublist • Reconnect the reversed portion with remaining list Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Partial reversal in linked lists requires precise pointer updates, not extra space. This builds strong intuition for advanced linked list problems. 🔥 Day 70 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 28/100 – #100DaysOfCode Today’s problem: Reverse Linked List (LeetCode 206) 💡 What I learned: How to reverse a singly linked list using an iterative approach Managing pointers efficiently (prev, curr, next) Understanding how links change direction step by step 🧠 Key Idea: Instead of creating a new list, we reverse the links in-place: Store next node Reverse current pointer Move forward 📊 Complexity: Time: O(n) Space: O(1) Staying consistent, one problem at a time... #Day28 #100DaysOfCode #LeetCode #DSA #LinkedList #Java #CodingJourney #ProblemSolving #TechSkills
To view or add a comment, sign in
-
-
🚀 Day 45 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Convert to Base 7 Problem Insight: Convert a given integer into its base 7 representation. The challenge is to handle both positive and negative numbers correctly while building the result efficiently. Approach: • Handled edge case when number is 0 • Checked if the number is negative and stored the sign • Used modulo (% 7) to extract digits • Divided the number by 7 in each step • Stored digits using StringBuilder and reversed at the end • Added negative sign if required Time Complexity: • O(log₇ n) Space Complexity: • O(log₇ n) (for storing result) Key Learnings: • Base conversion can be efficiently done using division & modulo • StringBuilder helps optimize string operations • Handling negative numbers is an important edge case • Reverse logic is commonly used in digit-building problems Takeaway: Breaking a problem into small steps makes even number system conversions simple and intuitive! #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 60/100 📌 Problem: String to Integer Given a string, convert it into a 32-bit signed integer while: • Ignoring leading whitespaces • Handling '+' and '-' signs • Reading digits until a non-digit appears • Returning INT_MAX or INT_MIN in case of overflow 💡 What I Learned: • Importance of handling edge cases • How to safely manage overflow • Writing clean and efficient parsing logic ⚡ Result: Runtime 1 ms (Beats 100%) Consistency + Practice = Improvement 📈 #Day60 #Java #DSA #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode #LearnToCode
To view or add a comment, sign in
-
-
Day 66/100 🚀 | #100DaysOfDSA Solved LeetCode 14 – Longest Common Prefix today. The task was to find the longest common prefix string among an array of strings. Initially, I couldn’t come up with this optimal approach and first solved it using a brute-force O(n²) method, comparing characters across all strings. Approach (Optimized): Used sorting to simplify the problem. • Sorted the array of strings. • Took the first and last strings after sorting. • Compared characters of both strings until they differ. • The common prefix between these two will be the answer for all strings. This works because after sorting, the most different strings will be at the extremes. Time Complexity: O(n log n + m) (where n = number of strings, m = length of prefix comparison) Space Complexity: O(1) (ignoring sorting space) Key takeaway: Sorting can sometimes reduce multi-string comparison problems to just comparing two extreme cases, making the solution much simpler and efficient. #100DaysOfDSA #LeetCode #DSA #Java #Strings #Sorting #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 51/100 – LeetCode Challenge Problem: Third Maximum Number Today I solved the “Third Maximum Number” problem, which focuses on identifying the third distinct maximum value in an array. The key challenge here was handling duplicates while keeping track of the top three distinct values. Instead of sorting the array directly, I used a set to eliminate duplicates first. This simplified the problem by ensuring that only unique values were considered. From there, I handled two cases. If there were fewer than three distinct numbers, I returned the maximum. Otherwise, I iterated through the set and tracked the top three maximum values using variables, updating them as I encountered larger numbers. This approach avoids unnecessary sorting and keeps the logic efficient and controlled. The solution runs in O(n) time with O(n) space complexity due to the set. This problem reinforced the importance of handling duplicates carefully and thinking about edge cases before jumping into implementation. Fifty-one days in. The focus is now on writing cleaner logic with fewer assumptions. #100DaysOfLeetCode #Java #DSA #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Solved: Product of Array Except Self 💡 Key Takeaway: Instead of recalculating product for every index, we can use prefix and suffix products to build the result efficiently. 👉 Approach I followed: - First pass → store left (prefix) product - Second pass → multiply with right (suffix) product - No division used 📊 Time Complexity: O(n) 📦 Space Complexity: O(1) 🔍 What made it interesting: Understanding how left and right contributions combine at each index to avoid redundant calculations. Consistency + clarity is slowly building confidence 💪 #DSA #Java #LeetCode #CodingJourney #BackendDeveloper #SoftwareEngineering
To view or add a comment, sign in
-
Day 31/50 🚀 — Reverse Vowels of a String (LeetCode 345) Today’s problem was a great reminder that sometimes the simplest approaches are the most efficient. 🔹 Used the two-pointer technique 🔹 Focused on in-place swapping 🔹 Optimized for both time (O(n)) and space (O(1)) Key takeaway: Instead of overthinking, break the problem into smaller checks—identify vowels, move pointers smartly, and swap only when needed. Clean and efficient 💡 Happy to see this solution performing well: ⚡ Runtime: 2 ms (faster than 99%+) 📦 Space: Decent optimization #Day31 #LeetCode #DSA #Java #CodingJourney #50DaysOfCode #ProblemSolving #SoftwareEngineering
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