𝗗𝗮𝘆 𝟮𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved 3Sum using Sorting + Two Pointer technique. ➤ Approach (O(n²), O(1) extra space apart from result): • First, sort the array • Fix one element i • Use two pointers (left, right) to find pairs such that --> nums[i] + nums[left] + nums[right] == 0 • Move pointers based on whether the sum is smaller or larger than zero • Store unique triplets ➤ Key Insight: Sorting simplifies the problem. Once sorted, the two-pointer technique efficiently avoids checking every combination. Brute force would be O(n³). Optimized approach reduces it to O(n²). #LeetCode #Java #DSA #TwoPointers #Sorting #ProblemSolving #20DaysChallenge #Consistency
LeetCode Challenge: 3Sum with Sorting and Two Pointers
More Relevant Posts
-
Day 23 - Find Minimum in Rotated Sorted Array Technique Used: Modified Binary Search Key Idea: Since the array is rotated but originally sorted, used binary search to identify the pivot (minimum element). Compared mid with end to determine which half is unsorted and adjusted boundaries accordingly. Time Complexity: O(log n) #Day23 #LeetCode #Java #BinarySearch #DSA #Algorithms
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
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 26 - Single Element in a Sorted Array Technique Used: Parity-Based Binary Search Key Idea: Since every element appears twice except one, used index parity (even/odd positions) to determine which half violates the pairing rule. If pairing is correct on the left side, move right. Otherwise, move left. Time Complexity: O(log n) #Day26 #LeetCode #Java #BinarySearch #DSA #Algorithms
To view or add a comment, sign in
-
-
Day 25 - Search Insert Position Technique Used: Binary Search Key Idea: Applied binary search to locate the target in a sorted array. If the target is not found, returned the index where it would be inserted while maintaining sorted order. The final start pointer represents the correct insertion position. Time Complexity: O(log n) #Day25 #LeetCode #Java #BinarySearch #DSA #Algorithms
To view or add a comment, sign in
-
-
🚀 DSA Tip: Two Pointer Approach for Target Sum If your array is sorted, you don’t need nested loops to find a pair with a given target sum. 👉 Use the Two Pointer Technique — simple and O(n)! 💡 How it works: • Place one pointer at the start (left) • Place another at the end (right) • Compare their sum with the target ✅ If sum is small → move left++ ✅ If sum is large → move right-- ✅ If equal → 🎯 pair found ⏱️ Complexity: Time → O(n) Space → O(1) 🔥 When to use: ✔️ Target sum in sorted array ✔️ Pair problems ✔️ Remove duplicates ✔️ Reverse problems Mastering two pointers can save you from unnecessary O(n²) solutions! #DSA #CodingInterview #Java #TwoPointers #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode Solved 154. Find Minimum in Rotated Sorted Array II on LeetCode 🔍 🧠 Key Insight: The array is sorted but rotated, and this version introduces duplicates, which makes the binary search logic slightly trickier. ⚙️ Approach: 🔹Use binary search with left and right pointers 🔹Compare nums[mid] with nums[right]: 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹If nums[mid] < nums[right] → minimum lies in the left half (including mid) 🔹If equal → safely shrink the search space by decrementing right This handles the ambiguity caused by duplicates. ⏱️ Time Complexity: Average: O(log n) Worst case: O(n) (when many duplicates exist) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
3Sum Problem: Recently solved the classic 3Sum problem by implementing it in three progressively optimized ways: 🔹 Brute Force – Checked all triplets (O(n³)) 🔹 Better Approach (Hashing) – Reduced one loop using a set (O(n²)) 🔹 Optimal Approach (Sorting + Two Pointers) – Efficient duplicate handling with O(n²) time and O(1) extra space Key takeaways: ✔️ Writing the brute-force solution first builds clarity ✔️ Sorting often unlocks powerful optimizations ✔️ Two-pointer technique is essential for array problems ✔️ Handling edge cases and duplicates carefully matters This exercise reinforced how structured thinking leads to better optimization — step by step. #DSA #Java #ProblemSolving #CodingPractice #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 29/75 — Search Insert Position Today's problem focused on applying binary search on a sorted array. Goal: Return the index of a target value if it exists. If it does not exist, return the index where it should be inserted. Approach: • Use binary search to locate the target • If the element is not found, the left pointer represents the correct insertion index Key idea: return left Because after binary search ends, left naturally points to the position where the target should be inserted. Time Complexity: O(log n) Space Complexity: O(1) 29/75 🚀 #Day29 #DSA #BinarySearch #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 3 #SDE Binary Search and Array problems. Solved: • Single Element in a Sorted Array (Binary Search) • Array Duplicates – Find elements appearing twice Key Learnings: • Binary Search can be applied beyond simple searching — it can help identify patterns like the single non-duplicate element in a sorted array. • For duplicate detection in arrays with values in the range 1..n, careful traversal and counting strategies help identify repeated elements efficiently. #LeetCode #DSA #Arrays #BinarySearch #Java #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