From a98965216ef45bde8c7767002ae52cea6aa14e04 Mon Sep 17 00:00:00 2001 From: rbhargav0104 Date: Sun, 11 Jan 2026 18:45:01 +0530 Subject: [PATCH] Completed Two-Pointers-1 --- containerwithmostwater.cpp | 22 ++++++++++++++++++++++ sortcolours.cpp | 28 ++++++++++++++++++++++++++++ threesum.cpp | 0 3 files changed, 50 insertions(+) create mode 100644 containerwithmostwater.cpp create mode 100644 sortcolours.cpp create mode 100644 threesum.cpp diff --git a/containerwithmostwater.cpp b/containerwithmostwater.cpp new file mode 100644 index 00000000..3885a4e1 --- /dev/null +++ b/containerwithmostwater.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int maxArea(vector& height) { + int n = height.size(); + int maximum = 0; + //declare a left pointer and a right pointer + int lpointer = 0; + int rpointer = n - 1; + + while(lpointer < rpointer){//making sure left doesnt cross right + int currArea = min(height[lpointer], height[rpointer]) * (rpointer - lpointer);//min of height at left anf right multiplied by width gives us the area + maximum = max(maximum , currArea);//compare previous maximum and curr area and update + if(height[lpointer] < height[rpointer]){ + lpointer ++; + }else{ + rpointer--; + } + } + + return maximum; + } +}; \ No newline at end of file diff --git a/sortcolours.cpp b/sortcolours.cpp new file mode 100644 index 00000000..b08304ac --- /dev/null +++ b/sortcolours.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + void sortColors(vector& nums) { + int n = nums.size(); + int left = 0; + int mid = 0; + int right = n-1; + while(mid <= right){ + if(nums[mid] == 2){ + swap(nums,mid,right); + right--; + }else if(nums[mid] == 0){ + swap(nums,mid,left); + left++; + mid++; + }else{ + mid++; + } + } + } + +private: + void swap(vector& nums, int mid, int idx) { + int temp = nums[idx]; + nums[idx] = nums[mid]; + nums[mid] = temp; +} +}; \ No newline at end of file diff --git a/threesum.cpp b/threesum.cpp new file mode 100644 index 00000000..e69de29b