From 3ed783cf2288f5e683ec4c08876247d452da86b5 Mon Sep 17 00:00:00 2001 From: Kshitiz Sharma <110187324+kshitiz11101@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:15:47 +0530 Subject: [PATCH] Create 3Sum.cpp --- Graphs/3Sum.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Graphs/3Sum.cpp diff --git a/Graphs/3Sum.cpp b/Graphs/3Sum.cpp new file mode 100644 index 0000000..b229851 --- /dev/null +++ b/Graphs/3Sum.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + vector> threeSum(vector& arr) { + /* + for (int i = 0; i < n; i++) { + set hashset; + for (int j = i + 1; j < n; j++) { + int third = -(arr[i] + arr[j]); + if (hashset.find(third) != hashset.end()) { + vector temp = {arr[i], arr[j], third}; + sort(temp.begin(), temp.end()); + st.insert(temp); + } + hashset.insert(arr[j]); + } + } + + + vector> ans(st.begin(), st.end()); + return ans; + */ + int n=arr.size(); + sort(arr.begin(), arr.end()); + set> st; + vectortemp; + int sum=0; + for(int i=0;i0){ + k--; + } + else{ + j++; + } + } + } + vector> ans(st.begin(), st.end()); + return ans; + } +};