Skip to content

Modify the Array #203

@ambitiouss22

Description

@ambitiouss22

📝 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.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions