Gyanthakur / GFG_POTD

Add GfG potd fcfs
24 stars 48 forks source link

Modify the Array #203

Open Avnee29 opened 2 hours ago

Avnee29 commented 2 hours ago

📝 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.
github-actions[bot] commented 2 hours ago

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!