labscst
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
Straightforward. Iterate over the array keeping track of all numbers except zeros. This will overwrite the zeroes in between with the non-zeroes. Fill the rest with Zeroes.
public class Test
{
public static void main(String ...args)
{
int arr[] = {1,3,0,8,1,2,0,4,0,7};
moveZeroes(arr);
for(int i =0;i<arr.length;i++)
{
System.out.print(arr[i]);
}
}
private static void moveZeroes(int[] arr)
{
int pos=0;
for(int i =0;i<arr.length;i++)
{
if(arr[i]!=0)
{
arr[pos++] = arr[i];
}
}
for(int i=pos;i<arr.length;i++)
{
arr[i]=0;
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
}
- labscst November 08, 2016