BackeasyArrays Infosys Cognizant

Zero Shifter Solution

Problem Statement

Rearrange the readings so all dead sensors (zeros) are pushed to the end, preserving the order of valid readings.

Example 1
Input
[7, 11, 4, 0, 0, 0]
Output
[7, 11, 4, 0, 0, 0]

Explanation: Step-by-step: Given input [7, 11, 4, 0, 0, 0], we first separate the valid readings (7, 11, 4) from the dead sensors (0, 0, 0). Then we preserve the order of valid readings by keeping them in the same order and appending the dead sensors at the end, resulting in output [7, 11, 4, 0, 0, 0].

Example 2
Input
[1, 2, 3, 0, 0, 0]
Output
[1, 2, 3, 0, 0, 0]

Explanation: Step-by-step: Given input [1, 2, 3, 0, 0, 0], we first separate the valid readings (1, 2, 3) from the dead sensors (0, 0, 0). Then we preserve the order of valid readings by keeping them in the same order and appending the dead sensors at the end, resulting in output [1, 2, 3, 0, 0, 0].

Constraints

  • 1 <= n <= 10^5
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free