Given an input integer array arr, find all zeros and move the same to end indices of the array.
| The java method should accept following input parameters: arr (int array).
|
| Start a for loop that iterates backwards from last index of arr to first index using loop variable idx:
|
| If value at arr[idx] is 0, we need to move it to end of arr:
Initialize currIndex int variable to idx.
Start a while loop that iterates forward from currIndex to arr.length-1, or till we reach a zero at currIndex+1:
Keep interchanging the value of array element at index currentIdx and currentIdx+1. This way the zero we found above in if statement starts moving towards end of arr.
|
| After the while loop and for loop end, return arr, it now has all zeros moved to end indices.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayMoveZerosToEndOfArray {
public int[] arrayMoveZerosToEndOfArray(int[] arr) throws Exception{
int idx=0;
for (idx=arr.length-1; idx>=0; idx--) {
if (arr[idx] == 0) {
int currIndex=idx;
while ((currIndex !=arr.length-1) && (arr[currIndex+1]!=0)){
int temp=arr[currIndex+1];
arr[currIndex+1]=arr[currIndex];
arr[currIndex]=temp;
currIndex++;
}
}
}
return arr;
}
public static void main(String[] args) {
ArrayMoveZerosToEndOfArray ap= new ArrayMoveZerosToEndOfArray();
int[] arr;
try {
int[] intArray1 = {5,0,7,0,9,0};
printArraySummary(intArray1, "Original Array:");
arr=ap.arrayMoveZerosToEndOfArray(intArray1);
printArraySummary(arr, "Array with Zeros moved to end of Array: ");
int[] intArray2 = {0,0,7,0,0,0};
printArraySummary(intArray2, "Original Array:");
arr=ap.arrayMoveZerosToEndOfArray(intArray2);
printArraySummary(arr, "Array with Zeros moved to end of Array: ");
int[] intArray3 = {0,0,0,0,0,0};
printArraySummary(intArray3, "Original Array:");
arr=ap.arrayMoveZerosToEndOfArray(intArray3);
printArraySummary(arr, "Array with Zeros moved to end of Array: ");
int[] intArray4 = {9,8,7,6,5,4};
printArraySummary(intArray4, "Original Array:");
arr=ap.arrayMoveZerosToEndOfArray(intArray4);
printArraySummary(arr, "Array with Zeros moved to end of Array: ");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}