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 such that all zero elements appear at the beginning, followed by non-zero elements, maintaining relative order of non-zero elements.
Iterate through the array and maintain two pointe...read more
Help your peers!
Add answer anonymously...
WatchGuard Technologies Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. Given an array of integers, find the number of pairs whose sum is equal to a giv...read more
A Software Developer was asked Q. 5 dsa array string
A Software Developer was asked Q. MergeSort Linked List Problem Statement You are given a Singly Linked List of in...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. 5 dsa array string
A Software Developer was asked Q2. MergeSort Linked List Problem Statement You are given a Singly Linked List of in...read more
A Software Developer was asked Q3. Distinct Elements in K-Sized Windows The task is to determine the number of dist...read more
>
WatchGuard Technologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

