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
20d

Rearrange an unsorted array by moving all zeroes to the end while maintaining the order of non-zero elements.

  • Iterate through the array and keep track of the count of non-zero elements encountered.

  • Whil...read more

Help your peers!
Add answer anonymously...
Borderfree Mern Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter