diff --git a/src/MergeSortedArray.java b/src/MergeSortedArray.java new file mode 100644 index 00000000..69aaba7f --- /dev/null +++ b/src/MergeSortedArray.java @@ -0,0 +1,39 @@ +/* +Problem - Merging of 2 arrays +Approach - We start filling nums1 from the back using two pointers from end of nums1's filled part and nums2. +At each step, we place the larger of the two elements at the end and move pointers backward. +After that, if anything is left in nums2, we copy it directly into nums1. + + */ +import java.util.Arrays; + +public class MergeSortedArray { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int p1 = m - 1; + int p2 = n - 1;// starting from end of an array because if we start from the beginning of array we will encounter second array goes unsorted + int index = m + n - 1; + while (p1 >= 0 && p2 >= 0) { + if (nums1[p1] > nums2[p2]) { + nums1[index] = nums1[p1]; + p1--; + } + else { + nums1[index] = nums2[p2]; + p2--; + } + index--; + } + while (p2 >= 0) { + nums1[index] = nums2[p2]; + p2--; + index--; + } + } + public static void main(String[] args) { + int[] nums1 = new int[]{1, 2, 3, 0, 0, 0}; + int[] nums2 = new int[]{2, 5, 6}; + MergeSortedArray mergeSortedArray = new MergeSortedArray(); + mergeSortedArray.merge(nums1, 3, nums2, 3); + System.out.println(Arrays.toString(nums1)); + } +} diff --git a/src/RemoveDuplicates.java b/src/RemoveDuplicates.java new file mode 100644 index 00000000..df864e4a --- /dev/null +++ b/src/RemoveDuplicates.java @@ -0,0 +1,36 @@ +/* +Problem - Remove Duplicates from Sorted Array II +Approach -used two pointers fast(to scan the elements) and slow(to overwrite elements) +count duplicates and only allow each element to appear at most twice. +If count is within limit, we place it in the right position using the slow pointer. +Time Complexity - O(n) +Space Complexity- O(n) + */ +public class RemoveDuplicates { + public int removeDuplicates(int[] nums) { + int k = 2; + int slow = 0, fast = 0; + + int count = 0; + + while(fast < nums.length){ + if(fast != 0 && nums[fast] == nums[fast - 1]){ + count++;//duplicate elements increment the count + }else{ + count = 1; // to indicate its the start of new number + } + if(count <= k){ + nums[slow] = nums[fast]; + slow++; + } + fast++; + } + return slow; + } + public static void main(String[] args) { + RemoveDuplicates removeDuplicates = new RemoveDuplicates(); + System.out.println(removeDuplicates.removeDuplicates(new int[]{1,1,1,2,2,3})); + System.out.println(removeDuplicates.removeDuplicates(new int[]{0,0,1,1,1,1,2,3,3})); + + } +} diff --git a/src/Search2DSortedMatrix.java b/src/Search2DSortedMatrix.java new file mode 100644 index 00000000..43998d4d --- /dev/null +++ b/src/Search2DSortedMatrix.java @@ -0,0 +1,47 @@ +/* +Problem - Search 2D sorted matrix II +Approach - Start from the bottom-left corner of the matrix or we can start from top-right corner +If the number is bigger, move up or move left; if it's smaller, move right or move down. +Keep checking until you find the target or exit the matrix bounds. +Time Complexity - O(m+n) +Space Complexity - O(1) + */ + +public class Search2DSortedMatrix { + public boolean searchMatrix(int[][] matrix, int target) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false; + int m = matrix.length, n = matrix[0].length; + int row = 0, column = n - 1; // search using top right corner as a start point + while (row =0) { + if (matrix[row][column] == target) return true; + else if (matrix[row][column] > target) column--; + else row++; + } + return false; + } + + public boolean searchMatrix2(int[][] matrix, int target) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false; + int m = matrix.length, n = matrix[0].length; + int row = m-1, column = 0; // search using bottom left corner as a start point + while (row >= 0 && column target) row--; + else column++; + } + return false; + } + public static void main(String[] args) { + Search2DSortedMatrix s2d = new Search2DSortedMatrix(); + int[][] values = { + {1,4,7,11,15}, + {2,5,8,12,19}, + {3,6,9,16,22}, + {10,13,14,21,23}, + {18,19,20,23,26} + }; + System.out.println(s2d.searchMatrix(values, 20)); + System.out.println(s2d.searchMatrix2(values, 15)); + System.out.println(s2d.searchMatrix2(values, 30)); + } +}