Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two-pass approach used to solve the problem #204

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

Avnee29
Copy link
Contributor

@Avnee29 Avnee29 commented Oct 24, 2024

Note:
Assume ‘0’ as the invalid number and all others as a valid number. The sequence of the valid numbers is present in the same order.

Example:
Input: arr[] = [2, 2, 0, 4, 0, 8]
Output: [4, 4, 8, 0, 0, 0]
Explanation: At index 0 and 1 both the elements are the same. So, we will change the element at index 0 to 4 and the element at index 1 is 0 then we will shift all the zeros to the end of the array. So, the array will become [4, 4, 8, 0, 0, 0].Input: arr[] = [0, 2, 2, 2, 0, 6, 6, 0, 0, 8]  Output: [4, 2, 12, 8, 0, 0, 0, 0, 0, 0]
Explanation: At index 5 and 6 both the elements are the same. So, we will change the element at index 5 to 12 and the element at index 6 is 0. We will change the element at index 1 to 4 and the element at index 2 is 0. Then we shift all the zeros to the end of the array. So, array will become [4, 2, 12, 8, 0, 0, 0, 0, 0, 0]. Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ arr.size() ≤ 105
1 ≤ arr[i] ≤ 106

Description

Please include a summary of the changes and the related issue(s) this pull request addresses. Include any relevant context or background information.

Fixes: #[issue_number] (replace with the issue number, if applicable)

Use [x] to represent a checked (ticked) box.✅
Use [ ] to represent an unchecked box.❌

Type of Change

  • Question Added
  • Solution Added
  • Other (please specify):

Checklist

  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation (if applicable).
  • My changes generate no new warnings.
  • I have added tests to cover my changes (if applicable).
  • All new and existing tests pass.

Additional Notes

Please add any other information that is relevant to this pull request, including potential risks, alternative solutions considered, or future improvements.

Note:
Assume ‘0’ as the invalid number and all others as a valid number.
The sequence of the valid numbers is present in the same order.

Example:
Input: arr[] = [2, 2, 0, 4, 0, 8] 
Output: [4, 4, 8, 0, 0, 0] 
Explanation: At index 0 and 1 both the elements are the same. So, we will change the element at index 0 to 4 and the element at index 1 is 0 then we will shift all the zeros to the end of the array. So, the array will become [4, 4, 8, 0, 0, 0].Input: arr[] = [0, 2, 2, 2, 0, 6, 6, 0, 0, 8] 
Output: [4, 2, 12, 8, 0, 0, 0, 0, 0, 0]
Explanation: At index 5 and 6 both the elements are the same. So, we will change the element at index 5 to 12 and the element at index 6 is 0. We will change the element at index 1 to 4 and the element at index 2 is 0. Then we shift all the zeros to the end of the array. So, array will become [4, 2, 12, 8, 0, 0, 0, 0, 0, 0].
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ arr.size() ≤ 105
1 ≤ arr[i] ≤ 106
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Great job, @Avnee29! Thanks for creating the pull request.
Soon the maintainers/owner will review it and provide you with feedback/suggestions.
Make sure to star this awesome repository and follow the account!

@Gyanthakur Gyanthakur merged commit 64e3a28 into Gyanthakur:main Oct 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants