completed competitive coding - 3#1190
Open
yashhh-23 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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.javaimplementing in-place duplicate removal with O(n) time and O(1) extra space. - Adds a small
mainmethod to demonstrate usage and prints the resulting length.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // 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 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 |
|
|
||
| // 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 { |
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:
Please submit a solution that actually addresses the Pascal's Triangle problem as specified. VERDICT: NEEDS_IMPROVEMENT K-diff Pairs in an ArrayStrengths:
Areas for Improvement:
Recommendation: The student should:
VERDICT: NEEDS_IMPROVEMENT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.