From 6daada4e560b90ac1b5f0ccd44b66b17d77e5169 Mon Sep 17 00:00:00 2001 From: ARADHY MISHRA <111994905+aradhymishra09@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:01:01 +0530 Subject: [PATCH] 4Sum.cpp --- c++/4Sum.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 c++/4Sum.cpp 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.