Menu Close

Find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).

A chocolate factory is packing chocolates into the packets. The chocolate packets here represent an array arrt of N number of integer values. The task is to find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).

For Example:
N=7 and arr = [4,5,0,1.9,0,5,0].
There are 3 empty packets in the given set. These 3 empty packets represented as O should be pushed towards the end of the array.

Example 1:
Input:
7  – Value of N
[4,5,0,1,0,0,5] – Element of arr[O] to arr[N-1],While input each element is separated by newline
Output:
4 5 1 9 5 0 0

Example 2:
Input:
6
— Value of N.
[6,0,1,8,0,2] – Element of arr[0] to arr[N-1], While input each element is separated by newline
Output:
6 1 8 2 0 0
L=list(map(int,input('Enter the orders : ').split(' ')))# or you can get the N value and get the input using for loop
L1=[]
temp=0
for i in L:
    if i!=0:
        L1.append(i)
    else:
        temp+=1

if temp!=0:
    for i in range(temp):
        L1.append(0)
print(L1)

Input_1:
4 5 0 1 0 0 5

Output:
[4, 5, 1, 5, 0, 0, 0]


Input_2:
6 0 1 8 0 2

Output:
[6, 1, 8, 2, 0, 0]


Input_3:
1 2 3 0 3 4 0 1

Output:
[1, 2, 3, 3, 4, 1, 0, 0]


Input_4:
1 0 2 0 4 0 6 6

Output:
[1, 2, 4, 6, 6, 0, 0, 0]


executed using linux terminal python3

More Q