diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..9145831e --- /dev/null +++ b/Problem1.java @@ -0,0 +1,24 @@ +class Solution { + public int removeDuplicates(int[] nums) { + int slow=0; + int fast=0; + int count=0; + + while(fast=0 && p2>=0){ + //if value at pointer1 is greater than value in 2nd array of pointer 2 + //then we put that value at 1st array at last idx + if(nums1[p1]>=nums2[p2]){ + nums1[idx]=nums1[p1]; + p1--; + } + else{ + //if value at pointer2 is greater than value in 1st array of pointer 1 + //then we put that value at 1st array at last idx + nums1[idx]=nums2[p2]; + p2--; + } + //move the idx pointer inwards + idx--; + } + //if p2 is not out of bonds, just copy the elements from p2 to idx pointer + while(p2>=0){ + nums1[idx]=nums2[p2]; + p2--; + idx--; + } + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..237a0a75 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,21 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m=matrix.length; + int n= matrix[0].length; + //traversing through top-right corner + int i=0; int j=n-1; + while(i=0){ + + if(matrix[i][j]==target) return true; + //traverse to left columns + else if(matrix[i][j]>target){ + j--; + } + else{ + //traverse down rows + i++; + } + } + return false; + } +} \ No newline at end of file