Move Zeroes to End Problem Statement

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

Example:

Input:
array = [0, 1, -2, 3, 4, 0, 5, -27, 9, 0]
Output:
[1, -2, 3, 4, 5, -27, 9, 0, 0, 0]
Explanation:

The non-zero elements [1, -2, 3, 4, 5, -27, 9] are moved to the front maintaining their relative order, followed by zeroes shifted to the end.

Constraints:

  • 1 <= T <= 50 - Number of test cases.
  • 1 <= N <= 10^6 - Size of the array.
  • -10000 <= A[i] <= 10000 - Value of each element in the array.
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, the elements of the array.
Output:
Print the modified array for each test case on a new line.
Note:
You are not required to print anything; focus on implementing the function to achieve the required transformation.
Be the first one to answer
Add answer anonymously...
SAP Software Developer Intern 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