From 559d5880ea2a2967038528776bab972c26dc7b26 Mon Sep 17 00:00:00 2001 From: Priyanshu Date: Sun, 12 Apr 2026 10:45:39 +0530 Subject: [PATCH 1/2] Add two pointer approach for Two Sum in C++ --- cpp/0001-two-sum.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cpp/0001-two-sum.cpp b/cpp/0001-two-sum.cpp index 5ce94c170..dc662e5a3 100644 --- a/cpp/0001-two-sum.cpp +++ b/cpp/0001-two-sum.cpp @@ -24,3 +24,44 @@ class Solution { return {}; } }; + +/* + APPROACH-2: Sorting + Two Pointer + Store the given array in vector of pairs {{value,index}, {value,index}}.... + Then sort the vector of pair.. + After sorting we can apply two pointer aproach. + + Time Complexity: O(nlogn) + Space Complexity: O(n); +*/ + +class Solution2{ +public: + + vector twoSum(vector& nums, int target) { + vector> array; + int len = nums.size(); + for(int i=0; i Date: Sun, 12 Apr 2026 10:55:58 +0530 Subject: [PATCH 2/2] Fix: return indices using min and max --- cpp/0001-two-sum.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/0001-two-sum.cpp b/cpp/0001-two-sum.cpp index dc662e5a3..557be7bf4 100644 --- a/cpp/0001-two-sum.cpp +++ b/cpp/0001-two-sum.cpp @@ -51,7 +51,8 @@ class Solution2{ int sum =array[l].first + array[r].first; if(sum==target){ - return {array[l].second, array[r].second}; + return {min(array[l].second, array[r].second), + max(array[l].second, array[r].second)}; } else if(sum