Move Zeros to Left Problem Statement

Your task is to rearrange a given array ARR such that all zero elements appear at the beginning, followed by non-zero elements, while maintaining the relative order of non-zero elements.

Example:

Input:
ARR = [1, 1, 0, 2, 0]
Output:
[0, 0, 1, 1, 2]
Explanation:

The zero elements should be at the start, hence the rearranged array should be [0, 0, 1, 1, 2]. Arrays such as [0, 0, 1, 2, 1] and [0, 0, 2, 1, 1] are incorrect as they do not maintain the relative order of non-zero elements.

Follow Up:
Attempt to solve this in linear time and constant space.

Input:

The first line contains an integer 'T' for the number of test cases.
Each test case consists of an integer 'N', the number of elements in the array 'ARR', followed by 'N' space-separated integers.

Output:

For each test case, output the modified array on a new line.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • -109 <= ARR[i] <= 109
  • Time limit: 1 second
Note:
Implement the function without printing output directly. The output will be managed by the system.
AnswerBot
4mo

Rearrange array to move zeros to the left while maintaining relative order of non-zero elements.

  • Iterate through the array and maintain two pointers, one for zero elements and one for non-zero elements...read more

Help your peers!
Select
Add answer anonymously...

MAQ Software Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Given an array of integers nums and an integer target, return indices of the two...read more
A Software Developer Intern was asked Q. Write an SQL query to find the top 2 candidates.
A Software Developer Intern was asked Q. Minimum Travel Time Problem Statement Mr. X plans to explore Ninja Land, which c...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Given an array of integers nums and an integer target, return indices of the two...read more
A Software Developer Intern was asked Q2. Write an SQL query to find the top 2 candidates.
A Software Developer Intern was asked Q3. Minimum Travel Time Problem Statement Mr. X plans to explore Ninja Land, which c...read more
MAQ Software Software Developer Intern 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