Monday, 11 January 2021

You are given an array of integers with size n .you have to form an array such that all zeros present in array will locate to the last.(move zeros to end of array)

 Input format:

 First line contain size of array 

Second line contain array of elements with separate by space 

Output format: 

print array of integers such that zeros are at last 

sample input: 10 8 0 4 0 2 0 1 3 55 1005

 Sample output: 8 4 2 1 3 55 1005 0 0 0

Program

import java.util.*;

class ZeroMove {


public static void main(String[] args) {

int size,number=0,count=0;

Scanner scanner=new Scanner(System.in);

size=scanner.nextInt();

int array[]=new int[size];

for(int i=0;i<size;i++)

{

array[i]=scanner.nextInt();

}

for(int i=0;i<size;i++)

{

if(array[i]==0)

{

number++;

}

else

{

System.out.println(array[i]+"=="+i+"=="+number);

array[i-number]=array[i];

if(number!=0)

{

array[i]=0;

i-=number;

}

number=0;

}

}

for(int i=0;i<size;i++)

{

System.out.println(array[i]);

}

}


}