Challenge 1: Gathering Zeros to the Start

In this challenge, you'll implement the solution to gather all the zeros stored in an ArrayList to the starting indices.

Problem statement

In this problem, you have to implement the zerosToStart() function which will sort the elements of an Integer ArrayList such that all the zeros appear at left and other elements appear at the right.

Function prototype

void zerosToStart(ArrayList<Integer> arrList)

Output

A sorted ArrayList with zeros at the left i.e. starting indices and positive elements at the right.

Sample input

arrList = [5,0,20,4,0,0,9]

Sample output

arrList = [0,0,0,5,20,4,9]

Note: All non zero elements must appear in the original order.

Get hands-on with 1400+ tech skills courses.