Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions MergeSorted.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
34 changes: 34 additions & 0 deletions RemoveDuplicates.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions Searchinmatrix.java
Original file line number Diff line number Diff line change
@@ -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;
}
}