Move Zeroes to End

Given an unsorted array of integers, adjust the array such that all zeroes are moved to the end while preserving the order of non-zero elements.

Input:

The input provided consists of multiple test cases. For each test case:

  • An integer ‘N’ is provided, denoting the size of the array.
  • Following this, there are ‘N’ integers which represent the elements of the array.

Output:

For each test case, after performing the required modification, output the resulting array.

Example:

Input:
1
10
0 1 -2 3 4 0 5 -27 9 0
Output:
1 -2 3 4 5 -27 9 0 0 0

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 106
  • -10000 <= A[i] <= 10000

Note:

Implement the function as required; no need to handle the input/output operations explicitly.

AnswerBot
4mo

Move all zeroes in an unsorted array to the end while preserving the order of non-zero elements.

  • Iterate through the array and maintain two pointers - one for the current position and one for the posit...read more

Help your peers!
Select
Add answer anonymously...
BlueStacks Member Technical Staff Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits