|
| 1 | +<h1><a href = "https://leetcode.com/problems/3sum/description/" target="_blank">Three Sum</a><h1> |
| 2 | +<h3>This repository contains a C++ solution to the Leetcode Three Sum problem using the optimal two-pointer approach. The Three Sum problem is a classic algorithmic problem where the goal is to find all unique triplets in an array that sum up to zero.</h3> |
| 3 | +<h2>Problem Statement</h2> |
| 4 | +Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. |
| 5 | + |
| 6 | +Notice that the solution set must not contain duplicate triplets. |
| 7 | + |
| 8 | + <h2>Approach: Optimal Two Pointer Approach for Three Sum Problem</h2> |
| 9 | + The two-pointer approach is an efficient way to solve the Three Sum problem. Here's a breakdown of the approach used in the code: |
| 10 | + <ul> |
| 11 | + <li><h3>Sorting:</h3> |
| 12 | + <ul> |
| 13 | + <li>The first step involves sorting the input array nums. Sorting the array allows us to use the two-pointer technique efficiently.</li> |
| 14 | + </ul> |
| 15 | + </li> |
| 16 | + <li><h3>Iterating Through the Array::</h3> |
| 17 | + <ul> |
| 18 | + <li>We iterate through the sorted array using a for loop.</li> |
| 19 | + <li>Inside the loop, we handle duplicates by checking if the current element is the same as the previous element. If it is, we skip to the next element to avoid duplicate triplets.</li> |
| 20 | + </ul> |
| 21 | + </li> |
| 22 | + <li><h3>Two Pointers:</h3> |
| 23 | + <ul> |
| 24 | + <li>For each element nums[i], we initialize two pointers, j and k, where j points to i + 1, and k points to the last element of the array.</li> |
| 25 | + <li>We use these two pointers to traverse the array inward to find pairs that sum up to the negation of the current element (-nums[i]).</li> |
| 26 | + </ul> |
| 27 | + </li> |
| 28 | + <li><h3>Finding Triplets:</h3> |
| 29 | + <ul> |
| 30 | + <li>Within the two-pointer loop, we calculate the sum of the current triplet (nums[i] + nums[j] + nums[k]).</li> |
| 31 | + <li>If the sum is greater than zero, we decrement k to decrease the sum.</li> |
| 32 | + <li>If the sum is less than zero, we increment j to increase the sum.</li> |
| 33 | + <li>If the sum equals zero, we found a triplet. We add this triplet to the ans vector.</li> |
| 34 | + <li>After finding a triplet, we handle duplicates by skipping identical elements for both j and k.</li> |
| 35 | + </ul> |
| 36 | + </li> |
| 37 | + <li><h3>Returning the Result:</h3> |
| 38 | + <ul> |
| 39 | + <li>Finally, we return the vector ans containing all unique triplets that sum up to zero.</li> |
| 40 | + </ul> |
| 41 | + |
| 42 | +<h3>Pseudocode: </h3> |
| 43 | + |
| 44 | + function threeSum(nums): |
| 45 | + // Sort the input array |
| 46 | + sort(nums) |
| 47 | + |
| 48 | + // Initialize an empty list to store triplets |
| 49 | + ans = [] |
| 50 | + |
| 51 | + // Iterate through the sorted array |
| 52 | + for i from 0 to length(nums) - 1: |
| 53 | + // Skip duplicates |
| 54 | + if i > 0 AND nums[i] == nums[i-1]: |
| 55 | + continue |
| 56 | + |
| 57 | + // Initialize two pointers, j and k |
| 58 | + j = i + 1 |
| 59 | + k = length(nums) - 1 |
| 60 | + |
| 61 | + // Two Pointer Technique |
| 62 | + while j < k: |
| 63 | + // Calculate the sum of current triplet |
| 64 | + sum = nums[i] + nums[j] + nums[k] |
| 65 | + |
| 66 | + // If sum is greater than zero, decrement k |
| 67 | + if sum > 0: |
| 68 | + k = k - 1 |
| 69 | + // If sum is less than zero, increment j |
| 70 | + else if sum < 0: |
| 71 | + j = j + 1 |
| 72 | + // If sum is zero, found a triplet |
| 73 | + else: |
| 74 | + // Add the triplet to the list |
| 75 | + ans.append([nums[i], nums[j], nums[k]]) |
| 76 | + |
| 77 | + // Handle duplicates for j and k |
| 78 | + while j < k AND nums[j] == nums[j + 1]: |
| 79 | + j = j + 1 |
| 80 | + while j < k AND nums[k] == nums[k - 1]: |
| 81 | + k = k - 1 |
| 82 | + |
| 83 | + // Move j and k pointers |
| 84 | + j = j + 1 |
| 85 | + k = k - 1 |
| 86 | + |
| 87 | + // Return the list of triplets |
| 88 | + return ans |
| 89 | + |
| 90 | +<h3>Cpp Code:</h3> |
| 91 | + |
| 92 | + vector<vector<int>>ans; |
| 93 | + sort(nums.begin(),nums.end()); |
| 94 | + for(int i = 0 ;i < nums.size();i++){ |
| 95 | + |
| 96 | + if(i > 0 && nums[i] == nums[i-1]){ |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + int j = i + 1; |
| 101 | + int k = nums.size()-1; |
| 102 | + while(j<k){ |
| 103 | + int sum = nums[i] + nums[j]+nums[k]; |
| 104 | + if(sum > 0){ |
| 105 | + k--; |
| 106 | + } |
| 107 | + else if(sum < 0){ |
| 108 | + j++; |
| 109 | + } |
| 110 | + else{ |
| 111 | + vector<int>temp = {nums[i],nums[j],nums[k]}; |
| 112 | + ans.push_back(temp); |
| 113 | + j++,k--; |
| 114 | + while(j < k && nums[j] == nums[j-1]) { |
| 115 | + j++; |
| 116 | + } |
| 117 | + while(j<k && nums[k] == nums[k+1]){ |
| 118 | + k--; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + } |
| 124 | + return ans; |
| 125 | + |
| 126 | +\*\*(For full code , refer to the .cpp file) |
| 127 | + |
| 128 | +<h2>Complexity Analysis</h2> |
| 129 | +<ul> |
| 130 | + <h3>Time Complexity: 𝑂(nlog n) + O(n^2) where n is size of array.</h3> |
| 131 | + |
| 132 | + <h3>Space Complexity: O(number of triplets)</h3> |
| 133 | + |
| 134 | +</ul> |
| 135 | + |
| 136 | +<h2>Contributing:</h2> |
| 137 | +Contributions to improve the efficiency or readability of the code are welcome. Feel free to submit pull requests with your enhancements. |
0 commit comments