Skip to content

completed competitive coding - 3#1190

Open
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master
Open

completed competitive coding - 3#1190
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master

Conversation

@yashhh-23

Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings July 8, 2026 12:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Java two-pointer solution for the “remove duplicates from sorted array (allow at most two occurrences)” style problem to the competitive-coding repository.

Changes:

  • Introduces twopoint.java implementing in-place duplicate removal with O(n) time and O(1) extra space.
  • Adds a small main method to demonstrate usage and prints the resulting length.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread twopoint.java
// Any problem you faced while coding this : the iteration was a bit tricky to come up with the approach of using two pointers to remove duplicates from the sorted array.

// Your code here along with comments explaining your approach: use two pointers, one for iterating through the array and another for keeping track of the position of the next unique element. We start with the second pointer at index 2, and for each element in the array, we check if it is different from the element at the position of the first pointer minus 2. If it is different, we copy that element to the position of the first pointer and increment both pointers. This way, we can remove duplicates in place without using extra space.
import java.util.Arrays;
Comment thread twopoint.java
Comment on lines +8 to +28
class Main {
    public static int removedups(int[]nums){
        if(nums==null) return 0;
        if(nums.length<=2) return nums.length;
        int i=2;
        for(int j=2;j<nums.length;j++)
        {
            if(nums[j]!=nums[i-2])
            {
                nums[i]=nums[j];
                i++;
            }
        }
        return i;
    }
    public static void main(String[] args) {
        int [] nums={1,1,1,2,2,3};
        int len= removedups(nums);
        System.out.println("length="+len);
    }
} No newline at end of file
Comment thread twopoint.java

// Your code here along with comments explaining your approach: use two pointers, one for iterating through the array and another for keeping track of the position of the next unique element. We start with the second pointer at index 2, and for each element in the array, we check if it is different from the element at the position of the first pointer minus 2. If it is different, we copy that element to the position of the first pointer and increment both pointers. This way, we can remove duplicates in place without using extra space.
import java.util.Arrays;
class Main {
@super30admin

Copy link
Copy Markdown
Owner

Pascal's Triangle (twopoint.java)

You have submitted a solution for the wrong problem. Your code solves "Remove Duplicates from Sorted Array II" but the assignment requires generating Pascal's Triangle. To solve Pascal's Triangle:

  • Start with the first row: [1]
  • For each subsequent row, the first and last elements are 1
  • Each middle element is the sum of the two elements directly above it
  • Build each row based on the previous row

Please submit a solution that actually addresses the Pascal's Triangle problem as specified.

VERDICT: NEEDS_IMPROVEMENT


K-diff Pairs in an Array

Strengths:

  • The code is well-structured with clear variable names
  • Good use of comments explaining the approach
  • Handles edge cases (null check, length <= 2)
  • The two-pointer technique for removing duplicates is correctly implemented for its intended problem

Areas for Improvement:

  1. Wrong Problem: This solution solves a completely different problem. The student needs to implement the actual K-diff Pairs algorithm.
  2. Missing Core Logic: The solution needs to:
    • Use a HashMap to count element frequencies
    • Iterate through unique elements and check if element + k exists
    • Handle k=0 specially (count elements with frequency > 1)
    • Use a set to track unique pairs
  3. Time Complexity: For the K-diff problem, the reference solution achieves O(n) using a HashMap. The student's approach (even if adapted) would need proper data structures.

Recommendation: The student should:

  1. Re-read the problem statement carefully
  2. Study the two-pointer approach for K-diff pairs (using HashMap + Set)
  3. Handle the special k=0 case where pairs require same elements with frequency > 1
  4. Ensure the function name and logic match the problem being solved

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.

3 participants