diff --git a/c++/4Sum.cpp b/c++/4Sum.cpp new file mode 100644 index 0000000..4696f60 --- /dev/null +++ b/c++/4Sum.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + vector> fourSum(vector& nums, int target) { + int n = nums.size(); + sort(nums.begin(), nums.end()); + set> set; + vector> output; + for(int i=0; i newTarget){ + high--; + } + else{ + set.insert({nums[i], nums[j], nums[low], nums[high]}); + low++; high--; + } + } + } + } + for(auto it : set){ + output.push_back(it); + } + return output; + } +}; + + + + + + +/* + + Time Complexity : O(N^3), Here Three nested loop creates the time complexity. Where N is the size of the + array(nums). + + Space Complexity : O(1), Constant space. Extra space is only allocated for the Vector(output), however the + output does not count towards the space complexity. + + Solved using Array(Three Nested Loop) + Sorting. Optimized Approach.