Skip to content

Submit Two-Pointers-2 PR - #1799

Open
agk-s30 wants to merge 3 commits into
super30admin:masterfrom
agk-s30:master
Open

Submit Two-Pointers-2 PR#1799
agk-s30 wants to merge 3 commits into
super30admin:masterfrom
agk-s30:master

Conversation

@agk-s30

@agk-s30 agk-s30 commented Aug 19, 2026

Copy link
Copy Markdown

No description provided.

Implement merge function to combine two sorted arrays in-place.
Implement a solution to remove duplicates from a sorted array, allowing at most two occurrences of each element.
Implement searchMatrix method to find target in 2D matrix.
@super30admin

Copy link
Copy Markdown
Owner

Merge Sorted Array (Problem_1.py)

It appears there has been a significant mix-up. You have submitted a solution for "Remove Duplicates from Sorted Array II" instead of "Merge Sorted Array".

To solve "Merge Sorted Array" correctly, you should:

  1. Use the two-pointer technique from the end: Start with pointers p1 = m - 1 (last valid element in nums1) and p2 = n - 1 (last element in nums2), and an index idx = m + n - 1 (last position in nums1).
  2. Compare and place the larger element at position idx, then decrement the corresponding pointer and idx.
  3. Handle remaining elements: After the main loop, if p2 >= 0, copy remaining elements from nums2 to nums1. (No need to handle remaining nums1 elements since they're already in place.)
  4. Achieve O(m + n) time and O(1) space by working in-place from the back.

Here's a skeleton to get you started:

class Solution:
    def merge(self, nums1, m, nums2, n):
        p1, p2 = m - 1, n - 1
        idx = m + n - 1
        
        while p1 >= 0 and p2 >= 0:
            if nums2[p2] > nums1[p1]:
                nums1[idx] = nums2[p2]
                p2 -= 1
            else:
                nums1[idx] = nums1[p1]
                p1 -= 1
            idx -= 1
        
        while p2 >= 0:
            nums1[idx] = nums2[p2]
            p2 -= 1
            idx -= 1

Please re-submit with the correct solution for "Merge Sorted Array".

VERDICT: NEEDS_IMPROVEMENT


Search a 2D Matrix II (Problem_2.py)

VERDICT REASON: [Brief reason for verdict]

VERDICT appears to be NEEDS_IMPROVEMENT

VERDICT: NEEDS_IMPROVEMENT


Remove Duplicates from Sorted Array II (Problem_3.py)

It appears that you have submitted a solution to the wrong problem. The problem you need to solve is "Remove Duplicates from Sorted Array II," which involves modifying a sorted array in-place so that each unique element appears at most twice. Your submitted code solves "Search a 2D Matrix II" instead.

To correctly solve the assigned problem, you should:

  • Use a two-pointer approach (slow and fast pointers) to traverse the sorted array.
  • Keep track of how many times the current element has appeared (a count).
  • If the count is ≤ 2, copy the element to the slow pointer position and advance the slow pointer.
  • Always advance the fast pointer.
  • Return the slow pointer as the new length k.

For example, a correct approach would look like:

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow = 0
        count = 0
        for fast in range(len(nums)):
            if fast == 0 or nums[fast] != nums[fast - 1]:
                count = 1
            else:
                count += 1
            if count <= 2:
                nums[slow] = nums[fast]
                slow += 1
        return slow

Please re-submit with the correct solution to the assigned problem.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants