Finally day 6 of #60daysOfLeetcode! Life employed some dirty tricks to delay this one but here we are. Todays question. 3sum - https://lnkd.in/d8E-SVvx Solution. We sort the array initially in order to allow use of two pointers from both ends. Then we iterate using from the beginning of the array. For each position, we check for the other the two numbers that might add up to zero using opposing two pointers, i+1 and len(arr) - 1. If the sum is too large then the right pointer reduces, otherwise the left increases. If the sum equals to zero, that combination is saved. At end we return a list of these combinations. Time complexity is O(N^2) due to the nested loop. Space complexity is O(N) due to the set. #Java #LearningInPublic #LeetCode #DSA
3Sum LeetCode Solution in Java
More Relevant Posts
-
🚀 Day 37 / 100 | Smallest Pair With Different Frequencies -Intuition: -This problem is based on frequency counting and greedy selection. -So the idea is to count the frequency of each number and then check pairs in increasing order. -Approach: O(n log n) -First, store the frequency of each number using HashMap. -Then, store all distinct numbers in a list. -Sort the list so we can get the smallest values first. -Now, check every pair (x, y) where x < y: If freq(x) != freq(y), return that pair immediately. -Since the list is sorted, the first valid pair will be the answer. -Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 10 – #50DaysLeetCodeChallenge Today I solved the Remove Element problem. 📌 Problem Statement Given an integer array nums and a value val, remove all occurrences of val in-place and return the number of remaining elements k. ⚠️ The order of elements may change, and no extra space should be used. Example: Input: nums = [3,2,2,3], val = 3 Output: k = 2 → [2,2,...] 💡 Approach I Used – One Pointer / Two Pointer Hybrid 1️⃣ Use a pointer k to track the position for valid elements 2️⃣ Traverse the array using i 3️⃣ If nums[i] != val Swap it with nums[k] Increment k ⚙️ Key Idea Keep all valid elements at the front Ignore unwanted values (val) Perform everything in-place #LeetCode #Java #DSA #CodingChallenge #ProblemSolving #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 5 of Solve With Me ⚡ Today, we’re solving Running Sum of 1D Array (Problem #1480) on LeetCode 💻✨ Here’s the thought process 👇 🔎 Approach 1️⃣ Create a variable sum and initialize it to 0. 2️⃣ Traverse the array from index 0 to the end. 3️⃣ At each index: Add the current element to sum. Replace the current element with the updated sum. 4️⃣ Return the modified array. #Day5 #SolveWithMe #Java #DSA #LeetCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode 🌱 Topic: Linked List / Recursion ✅ Problem Solved: LeetCode 24 – Swap Nodes in Pairs 🛠 Approach: Used a recursive approach to swap every two adjacent nodes in the linked list. If the list has 0 or 1 node, return it directly (base case). Store the second node (head.next) as a temporary node. Recursively swap the remaining list starting from temp.next. Adjust pointers so the second node becomes the new head of the pair. Connect the swapped pair with the recursively processed list. This swaps nodes without modifying their values, only changing pointers. #100DaysOfCode #Day38 #DSA #LinkedList #Recursion #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 25 of #100DaysOfCode Solved 24. Swap Nodes in Pairs on LeetCode 🔗🔄 🧠 Key insight: Swapping nodes in a linked list doesn’t require extra memory—careful pointer updates are enough to reverse every adjacent pair. ⚙️ Approach: 🔹Traverse the list two nodes at a time 🔹Reverse links between each adjacent pair 🔹Maintain connections with the previous pair to keep the list intact 🔹Handle edge cases for odd-length lists ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 DSA Consistency – Day 54 Solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. The task is to determine whether a binary string contains at most one contiguous block of 1s. Example: 1100111 → ❌ False (two segments of 1s) 111000 → ✅ True (only one segment) 🔹 Approach 1: Traverse the String Idea: While traversing the string, once we encounter a 0, no 1 should appear afterward. If 1 appears again, it means a second segment of ones has started. 🔹 Approach 2: String Pattern Check A binary string with only one segment of 1s should never contain "01" followed by another 1. So, we simply check for the pattern "01". Both the approaches have same complexities: Time Complexity: O(n) Space Complexity: O(1) #DSA #LeetCode #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 41 of #100DaysOfCode Solved 189. Rotate Array on LeetCode 🔄 🧠 Key Insight: Rotating an array by k steps to the right means the last k elements move to the front, while the remaining elements shift to the right. ⚙️ Approach: 1️⃣ Compute effective rotations using k % n 2️⃣ Store the last k elements in a temporary array 3️⃣ Shift the remaining n-k elements to the right 4️⃣ Copy the stored elements back to the beginning of the array This ensures the rotation happens in-place with controlled extra space. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) #100DaysOfCode #LeetCode #DSA #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 48 / 100 | Combinations Intuition: The idea is to gradually build combinations by choosing numbers starting from a given index. By always moving forward, we avoid duplicates and ensure each combination is unique. Approach: Use backtracking to generate all possible combinations. Maintain a list to store the current combination. Start selecting numbers from a given starting point. Add a number to the list and recursively continue building the combination. Once the size of the list becomes k, we store it as a valid combination. After exploring a choice, remove the last number (backtrack) and try the next possible number. Complexity: Time Complexity: O(k*C(n,k)) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #Backtracking
To view or add a comment, sign in
-
-
Day 9/100 – LeetCode Challenge Problem: 3Sum Today’s challenge was to find all unique triplets in an array whose sum equals 0. Approach: Sort the array to efficiently apply the two-pointer technique. Fix one element nums[i] and use two pointers (left and right) to find the remaining two numbers. If the sum equals 0, store the triplet. Skip duplicate elements to ensure only unique triplets are included. Key Idea: Sorting + Two Pointers helps reduce the brute-force O(n³) approach to O(n²). Concepts Practiced: Sorting Two-pointer technique Duplicate handling Array traversal optimization #100DaysOfCode #LeetCode #DSA #Java #TwoPointers #ProblemSolving #CodingJourney
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