🚀Day 4/100 – LeetCode Journey Today’s problem: 3Sum Closest🔥 Approach (Sorting + Two Pointer)💡 👉 Workflow (step-by-step): 1. Sort the array → helps in using two-pointer efficiently 2. Fix one element (nums[k]) 3. Use two pointers: i = k + 1 (left) j = n - 1 (right) 4. Calculate: sum = nums[k] + nums[i] + nums[j] 5. Compare: If this sum is closer to target than previous → update closestSum ✅ 6. Move pointers: If sum < target → i++ (increase sum) If sum > target → j-- (decrease sum) Repeat until all possibilities are checked. Key Idea: We are not finding exact match, we are finding the closest possible sum to target ⚡ Time Complexity: O(n²) → outer loop + two pointer 🧠 Space Complexity: O(1) → no extra space used Thanks to RAVI KUMAR Sir for guidance! Getting better every day 🚀 #100DaysOfCode #LeetCode #DSA #Java
LeetCode 3Sum Closest Problem Solution
More Relevant Posts
-
🚀 Day 5/100 – LeetCode Journey Today’s problem: Container With Most Water 💧 Approach (Two Pointer) 1. Take two pointers: left = 0 right = n - 1 2. Workflow: Calculate height → min(height[left], height[right]) Width → right - left Area → height × width Update maxWater if needed ✅ 3. Move pointer: Move the smaller height pointer Because larger height won’t increase area if width decreases. 🧠 Key Idea: Area depends on minimum height, so always try to improve the smaller one. ⚡ Time Complexity: O(n) → single traversal 🧠 Space Complexity: O(1) → no extra space Thanks to RAVI KUMAR Sir for guidance! Consistency building day by day 💪 #100DaysOfCode #LeetCode #DSA #Java
To view or add a comment, sign in
-
-
🚀 Day 58 of #100DaysOfCode Solved 165. Compare Version Numbers on LeetCode 🔗 🧠 Key Insight: Version strings are split by "." into multiple revisions. We compare each revision numerically (not lexicographically). Example: "1.01" = "1.1" (leading zeros don’t matter) ⚙️ Approach (Split + Compare): 1️⃣ Split both versions using "." 🔹 version1 → s1[] 🔹 version2 → s2[] 2️⃣ Traverse till max length of both arrays 3️⃣ For each index i: 🔹 num1 = i < s1.length ? parseInt(s1[i]) : 0 🔹 num2 = i < s2.length ? parseInt(s2[i]) : 0 4️⃣ Compare: 🔹 if num1 < num2 → return -1 🔹 if num1 > num2 → return 1 5️⃣ If all equal → return 0 ⏱️ Time Complexity: O(n + m) 📦 Space Complexity: O(n + m) (for split arrays) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 75 of 365 – Triangular Sum of an Array 🔺 One of those problems that looks harmless… until it quietly tests your fundamentals. My approach? Recursive reduction. Keep building the next array using adjacent sums until only one element remains. [1,2,3,4,5] [3,5,7,9] [8,2,6] [0,8] [8] ⚠️ Where I went wrong I initially messed up the base case and recursion flow. Returned from the wrong place Tried accessing values that didn’t exist Didn’t fully trust the recursion chain Classic case of: “Logic sahi tha, execution hil gaya.” ✅ Fix Correct base case → when size == 1, return it Build next array properly Pass result through recursion cleanly That’s it. No overthinking. 💡 What I learned Recursion is simple… until you break the base case Most bugs are not in logic, but in boundaries Clean recursion = clean thinking Day 75 done ✅ Still showing up. Still sharpening. #Day75 #365DaysOfDSAChallenge #LeetCode #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 71/100 – LeetCode Challenge Solved: Remove Duplicates from Sorted List II (Medium) Today’s problem was a great test of Linked List manipulation and handling edge cases efficiently. 🔍 Key Insight: Since the list is sorted, duplicates appear consecutively. Instead of keeping one copy, the challenge is to remove all nodes with duplicate values, leaving only distinct elements. 💡 Approach: Used a dummy node to handle edge cases Applied two-pointer technique (prev & current) Skipped entire duplicate sequences in one pass ⚡ Result: Runtime: 0 ms (Beats 100%) Space Complexity: O(1) 🎯 Key Learning: Handling duplicates in linked lists requires careful pointer updates — especially when the head itself is part of duplicates. Consistency is key 🔥 #Day71 #LeetCode #100DaysOfCode #Java #DataStructures #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Day 77/100 Completed ✅ 🚀 Solved LeetCode – Search a 2D Matrix II (Java) ⚡ Implemented an efficient approach by starting from the top-right corner of the matrix and eliminating rows or columns based on comparison with the target. This reduces the search space at every step, achieving O(m + n) time complexity. 🧠 Key Learnings: • Smart traversal in a sorted 2D matrix • Eliminating search space using row & column properties • Moving left (col--) when value is greater • Moving down (row++) when value is smaller • Better than brute-force (O(m × n)) approach 💯 This problem improved my understanding of matrix traversal strategies and how to optimize searching using sorted properties. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #problemSolving #optimization #arrays #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 114 - LeetCode Journey Solved LeetCode 572 – Subtree of Another Tree ✅ This problem focuses on determining whether one binary tree is a subtree of another. A subtree must match both in structure and node values, which makes it more than just a simple value comparison problem. Approach: I used a recursive strategy combining two key steps: Traverse each node of the main tree At every node, check if the subtree starting from that node is identical to the given subRoot For checking identical trees, I implemented a helper function that compares: • Node values • Left subtree • Right subtree If all match, we confirm the subtree exists. Otherwise, we continue searching in the left and right branches of the main tree. Complexity Analysis: • Time Complexity: O(n × m) in the worst case, where n is nodes in root and m is nodes in subRoot • Space Complexity: O(h), due to recursion stack Key Takeaways: • Tree problems often require combining traversal + comparison logic • Breaking problems into helper functions simplifies implementation • Understanding recursion flow is crucial for tree-based questions 🌳 All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 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
To view or add a comment, sign in
-
-
LeetCode — Problem 11 | Day 2 💡 Problem: Container With Most Water Given an array of heights, find two lines that together form a container which holds the maximum water. 🧠 My Approach: - Used two pointers (start & end) - Calculated area using "min(height) * width" - Moved the pointer with smaller height to maximize area - Repeated until pointers meet This problem gave a good understanding of: ✔️ Two Pointer Technique ✔️ Optimizing from brute force to O(n) ✔️ Logical thinking in arrays #LeetCode #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on rotating an array to the right by k steps efficiently. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Rotate Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐑𝐞𝐯𝐞𝐫𝐬𝐚𝐥 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞 • First normalized k using k % n • Reversed the entire array • Reversed the first k elements • Reversed the remaining elements This results in the desired rotation. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Reversal technique is a powerful in-place trick • Breaking a problem into steps simplifies logic • Modulo helps handle large values of k • In-place operations reduce space complexity 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the best solution is not shifting elements — but rearranging them smartly. 63 days consistent 🚀 On to Day 64. #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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