How To Move Zeros To End Of An Array?
Problem :
Write a Java program to separate zeros from non-zeros in the given array. You have to move zeros either to end of the array or bring them to beginning of the array. For example, if{14, 0, 5, 2, 0, 3, 0} is the given array, then moving zeros to end of the array will result {14, 5, 2, 3, 0, 0, 0}
import
java.util.Arrays;
public class
{
static void moveZerosToEnd(int inputArray[])
{
//Initializing counter to 0
int counter = 0;
//Traversing inputArray from left to right
for (int i = 0; i < inputArray.length; i++)
{
//If
inputArray[i] is non-zero
if(inputArray[i] != 0)
{
//Assigning
inputArray[i] to inputArray[counter]
inputArray[counter] = inputArray[i];
//Incrementing
the counter by 1
counter++;
}
}
//Assigning zero to remaining elements
while (counter < inputArray.length)
{
inputArray[counter] = 0;
counter++;
}
System.out.println(Arrays.toString(inputArray));
}
public static void main(String[] args)
{
moveZerosToEnd(new int[] {12, 0, 7, 0,
8, 0, 3});
moveZerosToEnd(new int[] {1, -5, 0, 0,
8, 0, 1});
moveZerosToEnd(new int[] {0, 1, 0, 1,
-5, 0, 4});
moveZerosToEnd(new int[] {-4, 1, 0, 0,
2, 21, 4});
}
}
Output :
[12, 7, 8, 3, 0, 0, 0]
[1, -5, 8, 1, 0, 0, 0]
[1, 1, -5, 4, 0, 0, 0]
[-4, 1, 2, 21, 4, 0, 0]
Comments
Post a Comment