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

Modify the Array #203

Open
Avnee29 opened this issue Oct 24, 2024 · 1 comment
Open

Modify the Array #203

Avnee29 opened this issue Oct 24, 2024 · 1 comment

Comments

@Avnee29
Copy link
Contributor

Avnee29 commented Oct 24, 2024

📝 Description

Given an array arr. Return the modified array in such a way that if the current and next numbers are valid numbers and are equal then double the current number value and replace the next number with 0. After the modification, rearrange the array such that all 0's are shifted to the end.
Given an array arr, the task is to modify the array as follows:

  1. Merge Adjacent Equal Values: Traverse the array. If two adjacent numbers are valid (non-zero) and equal, double the current number and set the next number to zero.
  2. Shift Zeros to the End: After the modification, rearrange the array such that all zeros are moved to the end, while maintaining the relative order of the non-zero elements.

💡 Enhancement / Feature Request (if applicable)

Usefulness:
This approach is useful for merging adjacent equal numbers, similar to mechanics in games like 2048. It helps streamline and compress data, preserving the order of non-zero elements while efficiently handling zero placements.
How It Works:

  1. Traverse the array, doubling current numbers when adjacent ones are equal and setting the next to 0.
  2. Rearrange the array by moving all non-zero elements to the front, and shifting zeros to the end.
Copy link

Welcome, @Avnee29! Thanks for raising the issue.
Soon the maintainers/owner will review it and provide you with feedback/suggestions.
Make sure to star this awesome repository and follow the account!

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

No branches or pull requests

1 participant