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 an array to move all zeros to the left while maintaining the relative order of non-zero elements.

  • Iterate through the array and maintain two pointers, one for zeros and one for non-zeros.

  • Swap...read more

Anonymous
1y
l=0 for r in range(len(arr1)): if arr1[r]==0 and r!=0: l=r-1 while l!=0 or arr1[r-1]!=0 and r!=0: arr1[l], arr1[r]=arr1[r], arr1[l] print(arr1) l-=1 r-=1
Help your peers!
Select
Add answer anonymously...

Dell Software Developer interview questions & answers

A Software Developer was asked 4mo agoQ. What technologies are you familiar with?
A Software Developer was asked 11mo agoQ. What was your experience with Java?
A Software Developer was asked 12mo agoQ. Given two strings, check if one is a substring of the other.

Popular interview questions of Software Developer

A Software Developer was asked 4mo agoQ1. What technologies are you familiar with?
A Software Developer was asked 11mo agoQ2. What was your experience with Java?
A Software Developer was asked 12mo agoQ3. Given two strings, check if one is a substring of the other.
Dell Software Developer 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