From dfacffc4909b163d5cf572aee8b6719dbfe54c3a Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Fri, 1 May 2026 14:13:27 -0700 Subject: [PATCH] Twopointers 2 --- MergeSorted.java | 39 +++++++++++++++++++++++++++++++++++++++ RemoveDuplicates.java | 34 ++++++++++++++++++++++++++++++++++ Searchinmatrix.java | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 MergeSorted.java create mode 100644 RemoveDuplicates.java create mode 100644 Searchinmatrix.java diff --git a/MergeSorted.java b/MergeSorted.java new file mode 100644 index 00000000..51927621 --- /dev/null +++ b/MergeSorted.java @@ -0,0 +1,39 @@ + +// Time Complexity : O(m + n) where m and n are the lengths of nums1 and nums2 respectively +// Space Complexity : O(1) as we are using constant space +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english +// We use three pointers i, j, and k to traverse nums1, nums2, and the merged array respectively. +// We compare elements at i and j and place the larger element at position k. +// We continue this process until we have merged all elements from both arrays. +// If there are remaining elements in nums2, we copy them to nums1. This way, we merge the two sorted arrays in-place without using extra space. +// Your code here along with comments explaining your approach + +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + + int i = m - 1; + int j = n - 1; + int k = m + n - 1; + + while (i >= 0 && j >= 0) { + + if (nums1[i] >= nums2[j]) { + nums1[k] = nums1[i]; + i--; + } else { + nums1[k] = nums2[j]; + j--; + } + + k--; + } + + // copy remaining elements from nums2 + while (j >= 0) { + nums1[k] = nums2[j]; + j--; + k--; + } + } +} \ No newline at end of file diff --git a/RemoveDuplicates.java b/RemoveDuplicates.java new file mode 100644 index 00000000..b983ce5a --- /dev/null +++ b/RemoveDuplicates.java @@ -0,0 +1,34 @@ + +// Time Complexity : O(n) where n is the length of the input array +// Space Complexity : O(1) as we are using constant space +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english +// We use two pointers, i and j, to traverse the array. +// The pointer i keeps track of the position of the last unique element, +// while j iterates through the array. +// If the current element at j is not equal to the last two unique elements, +// we increment i and update the value at i to the current element at j. +// This way, we ensure that each unique element appears at most twice in the array. +// Finally, we return i + 1 as the new length of the modified array. +// Your code here along with comments explaining your approach + +class Solution { + public int removeDuplicates(int[] nums) { + + if (nums.length <= 2) { + return nums.length; + } + + int i = 1; + + for (int j = 2; j < nums.length; j++) { + + if (nums[j] != nums[i] || nums[j] != nums[i - 1]) { + i++; + nums[i] = nums[j]; + } + } + + return i + 1; + } +} \ No newline at end of file diff --git a/Searchinmatrix.java b/Searchinmatrix.java new file mode 100644 index 00000000..f3e588ac --- /dev/null +++ b/Searchinmatrix.java @@ -0,0 +1,34 @@ + +// Time Complexity :o(m+n) where m is the number of rows and n is the number of columns in the matrix +// Space Complexity :O(1) as we are using constant space +// Did this code successfully run on Leetcode :yES +// Three line explanation of solution in plain english +// We start from the top right corner of the matrix and compare the current element with the target. +// If they are equal, we return true. If the current element is greater than the target, we move left (decrease column index). +// If the current element is less than the target, we move down (increase row index). We continue this process until we find the target or go out of bounds of the matrix. +//We can also start from the bottom left corner and follow the same logic, but starting from the top right corner is more intuitive for this problem. + +// Your code here along with comments explaining your approach + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + + int row = 0; + int col = matrix[0].length - 1; + + while (row < matrix.length && col >= 0) { + + if (matrix[row][col] == target) { + return true; + } + else if (matrix[row][col] > target) { + col--; + } + else { + row++; + } + } + + return false; + } +} \ No newline at end of file