From 1ad19f8b9044d5bf2ddc983429788787c3c09d1d Mon Sep 17 00:00:00 2001 From: tmujje Date: Thu, 6 May 2021 23:59:48 -0400 Subject: [PATCH 1/2] Two-Pointers-2 solutions --- MergeSortedArray.java | 32 ++++++++++++++++++++++++++++++++ RemoveDuplicates.java | 34 ++++++++++++++++++++++++++++++++++ Search2DMatrix.java | 26 ++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 MergeSortedArray.java create mode 100644 RemoveDuplicates.java create mode 100644 Search2DMatrix.java diff --git a/MergeSortedArray.java b/MergeSortedArray.java new file mode 100644 index 00000000..e5ba3f74 --- /dev/null +++ b/MergeSortedArray.java @@ -0,0 +1,32 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(mn) since we are iterating through both of the arrays +//Space Complexity: O(1) since we are not taking any extra space + + public void merge(int[] nums1, int m, int[] nums2, int n) + { + //[1,2,3,0,0,0] , m=3, [2,5,6], n=3 + int ptr1 = m - 1; + int ptr2 = n - 1; + int i = m + n - 1; + + while(ptr1 >= 0 && ptr2 >= 0) + { + if(nums1[ptr1] > nums2[ptr2]){ //Since the arrays are sorted it would be easy to verify the ends + nums1[i] = nums1[ptr1]; + ptr1--; + } + else + { + nums1[i] = nums2[ptr2]; + ptr2--; + } + i--; + } + while(ptr2>= 0){ // In case if ptr1 reaches 0 and ends the earlier loop + nums1[i] = nums2[ptr2]; + ptr2--; + i--; + } + } \ No newline at end of file diff --git a/RemoveDuplicates.java b/RemoveDuplicates.java new file mode 100644 index 00000000..860edd01 --- /dev/null +++ b/RemoveDuplicates.java @@ -0,0 +1,34 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the array +//Space Complexity: O(1) since we are not taking any extra space + + public int removeDuplicates(int[] nums) { + + int s = 1; + int f = 1; + int count = 1; + + // s pointer to collect the elements that are not repeated more than twice + // while f pointer iterates through the entire array + while(f < nums.length) + { + if(nums[f] == nums[f-1]){ + count++; + } + else{ + count = 1; // new element is found + } + + //Until the count reaches the number of repeated elements allowed (in this case 2), we keep replacing + if(count <= 2){ + nums[s] = nums[f]; + s++; + } + + f++; + } + + return s; + } \ No newline at end of file diff --git a/Search2DMatrix.java b/Search2DMatrix.java new file mode 100644 index 00000000..fddef5d1 --- /dev/null +++ b/Search2DMatrix.java @@ -0,0 +1,26 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(m) + O(n) i.e O(m) for iterating through the rows and O(n) for iterating through the columns +//Space Complexity: O(1) since we are not taking any extra space + + public boolean searchMatrix(int[][] matrix, int target) + { + int nrows = matrix.length; + int ncols = matrix[0].length; + int left = 0; + int right = ncols-1; + + while(left < nrows && right >= 0){ + + if(matrix[left][right] == target) return true; + else if(matrix[left][right] < target){ + left++; // Since the rows are sorted, we can safely assume that the target lies on to the right side + } + else{ + right--; + } + } + return false; + } + From 1b203c24b647be0d94b20001ccaa64492d95bdc3 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Sat, 15 Aug 2026 00:23:32 -0400 Subject: [PATCH 2/2] Complete Two-Pointers-2 assignment --- MergeSortedArray.java | 32 ----------------- MergeSortedArrays.java | 35 +++++++++++++++++++ RemoveDuplicates.java => RemoveDuplicate.java | 30 ++++++++-------- Search2DMatrix.java | 26 -------------- SearchTwoDMatrix.java | 31 ++++++++++++++++ 5 files changed, 81 insertions(+), 73 deletions(-) delete mode 100644 MergeSortedArray.java create mode 100644 MergeSortedArrays.java rename RemoveDuplicates.java => RemoveDuplicate.java (63%) delete mode 100644 Search2DMatrix.java create mode 100644 SearchTwoDMatrix.java diff --git a/MergeSortedArray.java b/MergeSortedArray.java deleted file mode 100644 index e5ba3f74..00000000 --- a/MergeSortedArray.java +++ /dev/null @@ -1,32 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(mn) since we are iterating through both of the arrays -//Space Complexity: O(1) since we are not taking any extra space - - public void merge(int[] nums1, int m, int[] nums2, int n) - { - //[1,2,3,0,0,0] , m=3, [2,5,6], n=3 - int ptr1 = m - 1; - int ptr2 = n - 1; - int i = m + n - 1; - - while(ptr1 >= 0 && ptr2 >= 0) - { - if(nums1[ptr1] > nums2[ptr2]){ //Since the arrays are sorted it would be easy to verify the ends - nums1[i] = nums1[ptr1]; - ptr1--; - } - else - { - nums1[i] = nums2[ptr2]; - ptr2--; - } - i--; - } - while(ptr2>= 0){ // In case if ptr1 reaches 0 and ends the earlier loop - nums1[i] = nums2[ptr2]; - ptr2--; - i--; - } - } \ No newline at end of file diff --git a/MergeSortedArrays.java b/MergeSortedArrays.java new file mode 100644 index 00000000..8d742df9 --- /dev/null +++ b/MergeSortedArrays.java @@ -0,0 +1,35 @@ +//Approach: Since both the arrays are sorted, if we keep pointers at the end and do the iterations for a better time complexity and space complexity +//Time Complexity : O(m) + O(m+n) +//Space Complexity: O(m) + +class MergeSortedArrays { + public void merge(int[] nums1, int m, int[] nums2, int n) { + + int ptrOne = m-1; + int ptrTwo = n-1; + int i = m+n-1; + + //Since the arrays are sorted if we keep pointers at the end of the arrays, it will be easier to compare and populate + while(ptrOne >= 0 && ptrTwo >= 0) + { + if(nums1[ptrOne] > nums2[ptrTwo]) + { + nums1[i] = nums1[ptrOne]; + ptrOne--; + } + else + { + nums1[i] = nums2[ptrTwo]; + ptrTwo--; + } + i--; + } + // In case if ptrOne reaches 0 and ends the earlier loop, we copy the rem + while(ptrTwo>= 0) + { + nums1[i] = nums2[ptrTwo]; + ptrTwo--; + i--; + } + } +} \ No newline at end of file diff --git a/RemoveDuplicates.java b/RemoveDuplicate.java similarity index 63% rename from RemoveDuplicates.java rename to RemoveDuplicate.java index 860edd01..3c1f0d3b 100644 --- a/RemoveDuplicates.java +++ b/RemoveDuplicate.java @@ -1,15 +1,14 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are iterating through the array -//Space Complexity: O(1) since we are not taking any extra space - +//Approach: Use slow pointer to collect the elements within k (in this case 2), and the first pointer to iterate through the entire array +//Time Complexity : O(n) +//Space Complexity: O(1) +public class RemoveDuplicate +{ public int removeDuplicates(int[] nums) { - + int s = 1; int f = 1; int count = 1; - + // s pointer to collect the elements that are not repeated more than twice // while f pointer iterates through the entire array while(f < nums.length) @@ -20,15 +19,16 @@ public int removeDuplicates(int[] nums) { else{ count = 1; // new element is found } - + //Until the count reaches the number of repeated elements allowed (in this case 2), we keep replacing if(count <= 2){ - nums[s] = nums[f]; - s++; - } - + nums[s] = nums[f]; + s++; + } + f++; } - + return s; - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/Search2DMatrix.java b/Search2DMatrix.java deleted file mode 100644 index fddef5d1..00000000 --- a/Search2DMatrix.java +++ /dev/null @@ -1,26 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(m) + O(n) i.e O(m) for iterating through the rows and O(n) for iterating through the columns -//Space Complexity: O(1) since we are not taking any extra space - - public boolean searchMatrix(int[][] matrix, int target) - { - int nrows = matrix.length; - int ncols = matrix[0].length; - int left = 0; - int right = ncols-1; - - while(left < nrows && right >= 0){ - - if(matrix[left][right] == target) return true; - else if(matrix[left][right] < target){ - left++; // Since the rows are sorted, we can safely assume that the target lies on to the right side - } - else{ - right--; - } - } - return false; - } - diff --git a/SearchTwoDMatrix.java b/SearchTwoDMatrix.java new file mode 100644 index 00000000..d5682108 --- /dev/null +++ b/SearchTwoDMatrix.java @@ -0,0 +1,31 @@ +//Approach: Row elimination using binary search to find the element with O(m)+O(n) +//Time Complexity : O(m) + O(n) +//Space Complexity: O(1) + +public class SearchTwoDMatrix +{ + public boolean searchMatrix(int[][] matrix, int target) { + //Validate inputs + if (matrix == null || matrix.length == 0) { + return false; + } + + int m = matrix.length; + int n = matrix[0].length; + + int r = 0, c = n - 1; + + while (r < m && c >= 0) { + //Target found + if (matrix[r][c] == target) return true; + // Since the rows are sorted, we can safely assume that the target lies on to the left side + else if (matrix[r][c] > target) { + c--; + } else { + r++; + } + } + return false; + } +} +