Move Zeros In Place while Preserving Nonzero Order

Quick Overview

Move Zeros In Place while Preserving Nonzero Order. Modify nums so all nonzero values stay in their original relative order and all zeros follow them. Do this in place without copying the array. The original func

Move Zeros In Place while Preserving Nonzero Order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Modify nums so all nonzero values stay in their original relative order and all zeros follow them. Do this in place without copying the array. The original function returns void; the resulting nums is the observation. ```cpp void moveZeroes(vector<int>& nums); ``` ### Examples ```text nums = [0,4,0,-2,5] -> nums becomes [4,-2,5,0,0] nums = [0] -> nums remains [0] ``` ### Constraints - Length is 1 through 10000. - Values span the signed 32-bit domain. ### Optional follow-up How can you reduce unnecessary operations?

Overview: Move Zeros In Place while Preserving Nonzero Order. Modify nums so all nonzero values stay in their original relative order and all zeros follow them. Do this in place without copying the array. The original func

Amazon logo
Amazon
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Modify nums so all nonzero values stay in their original relative order and all zeros follow them. Do this in place without copying the array. The original function returns void; the resulting nums is the observation.

void moveZeroes(vector<int>& nums);

Examples

nums = [0,4,0,-2,5] -> nums becomes [4,-2,5,0,0]
nums = [0] -> nums remains [0]

Constraints

  • Length is 1 through 10000.
  • Values span the signed 32-bit domain.

Optional follow-up

How can you reduce unnecessary operations?