Move Zeroes to End

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

Input:

The first line contains an integer ‘T’, the number of test cases. For each test case: The first line contains an integer ‘N’, the size of the array. The second line contains ‘N’ integers representing the elements of the array.

Output:

For each test case, rearrange the array in-place and provide the modified array on a new line.

Example:

Input:
T = 1
N = 10
array = [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
  • Time Limit: 1 sec

Note:

No need to print in the function; just perform the required operations to modify the array.
AnswerBot
4mo

Given an unsorted array of integers, move all zeroes to the end while maintaining the order of non-zero elements.

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

Help your peers!
Select
Add answer anonymously...
Senior Software Engineer 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