From 806562b21c42aada02003ecafdc69fb0c12c2fb8 Mon Sep 17 00:00:00 2001 From: sneha-tambade Date: Sat, 10 Jan 2026 23:27:45 -0800 Subject: [PATCH] TwoPointers-2 Implemented --- MergeSortedArray.cs | 33 ++++++++++++++++++++++ RemoveDuplicatesFromSortedArray.cs | 45 ++++++++++++++++++++++++++++++ SearchIn2DMatrix2.cs | 34 ++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 MergeSortedArray.cs create mode 100644 RemoveDuplicatesFromSortedArray.cs create mode 100644 SearchIn2DMatrix2.cs diff --git a/MergeSortedArray.cs b/MergeSortedArray.cs new file mode 100644 index 00000000..24ad75da --- /dev/null +++ b/MergeSortedArray.cs @@ -0,0 +1,33 @@ +//Time Complexity: O(m + n) +//Space Complexity: O(1) + +//Approach + +//Use three pointers (p1, p2, i) starting from the end of nums1, nums2 and the merged array. +//While p2 >= 0, place the larger of nums1[p1] and nums2[p2] at nums1[i] (guarding p1 >= 0). +//Decrement pointers accordingly; remaining nums1 elements are already in correct position. +public class Solution +{ + public void Merge(int[] nums1, int m, int[] nums2, int n) + { + int p1 = m - 1; + int p2 = n - 1; + int i = m + n - 1; + while (p2 >= 0) //Because nums2 must be fully merged + { + if (p1 >= 0 && nums1[p1] > nums2[p2]) //Because p1 might be exhausted, but p2 not be ignored + { + nums1[i--] = nums1[p1--]; + } + else + { + nums1[i--] = nums2[p2--]; + } + } + } +} + + + + + diff --git a/RemoveDuplicatesFromSortedArray.cs b/RemoveDuplicatesFromSortedArray.cs new file mode 100644 index 00000000..f6f8c02b --- /dev/null +++ b/RemoveDuplicatesFromSortedArray.cs @@ -0,0 +1,45 @@ +// Time Complexity: +// O(n) + +// Space Complexity: +// O(1) + +//Approach +// Keep slow pointer at index 0 , for loop Starts with index 1 , +// if the previous and the current elements are same increment count. +// if the the elements are unequal reset the count to 1. +// if count is less than 0 equal to 2 or k , update slow pointer with current vals. +// Increment slow pointer. +public class Solution +{ + public int RemoveDuplicates(int[] nums) + { + int slow = 1; + int count = 1; + for (int i = 1; i < nums.Length; i++) + { + if (nums[i] == nums[i - 1]) + { + count++; + } + else + { + count = 1; + } + if (count <= 2) + { + nums[slow] = nums[i]; + slow++; + } + + } + return slow; + } +} + + + + + + + diff --git a/SearchIn2DMatrix2.cs b/SearchIn2DMatrix2.cs new file mode 100644 index 00000000..f054841a --- /dev/null +++ b/SearchIn2DMatrix2.cs @@ -0,0 +1,34 @@ +public class Solution { + + // Time Complexity: + // O(m + n) + + // Space Complexity: + // O(1) + + // Start from the top-right corner of the matrix. + // If the number is bigger, move left; if smaller, move down. + // Keep checking until you find the target or exit the matrix bounds. + public bool SearchMatrix(int[][] matrix, int target) + { + int m = matrix.Length; + int n = matrix[0].Length; + int r = 0; int c = n - 1; + while (r < m && c >= 0) + { + if (matrix[r][c] == target) + { + return true; + } + else if (target < matrix[r][c]) + { + c--; + } + else + { + r++; + } + } + return false; + } +} \ No newline at end of file