From 539af72fe78f593a8b9f387f80c65a2029d85bca Mon Sep 17 00:00:00 2001 From: Manassa2000 Date: Wed, 26 Nov 2025 14:56:03 -0500 Subject: [PATCH] Two-Pointers-2 --- MergeSortedArray.java | 26 ++++++++++++++++++++++++++ RemoveDuplicatesFromSortedArrayII.java | 18 ++++++++++++++++++ SearchA2DMatrixII.java | 16 ++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 MergeSortedArray.java create mode 100644 RemoveDuplicatesFromSortedArrayII.java create mode 100644 SearchA2DMatrixII.java diff --git a/MergeSortedArray.java b/MergeSortedArray.java new file mode 100644 index 00000000..0fcff3ca --- /dev/null +++ b/MergeSortedArray.java @@ -0,0 +1,26 @@ +// Time Complexity :O(m+n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int p1 = m-1; + int p2 = n-1; + int p3 = m+n-1; + while(p1 >= 0 && p2 >=0){ + if(nums2[p2] > nums1[p1]){ + nums1[p3] = nums2[p2]; + p2--; + }else{ + nums1[p3] = nums1[p1]; + p1--; + } + p3--; + } + while(p2 >= 0){//copy rest of nums2 to nums1 + nums1[p3] = nums2[p2]; + p2--; + p3--; + } + } +} + diff --git a/RemoveDuplicatesFromSortedArrayII.java b/RemoveDuplicatesFromSortedArrayII.java new file mode 100644 index 00000000..26050219 --- /dev/null +++ b/RemoveDuplicatesFromSortedArrayII.java @@ -0,0 +1,18 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +class Solution { + public int removeDuplicates(int[] nums) { + int k=2; + int p1=k; + int p2=k; + while(p2=0 && col target) row--; + else col++; + } + return false; + } +} \ No newline at end of file