Move Zeros Left By Maintaining Order Of Digits: This Java program achieves the goal of moving all zeros in the input array to the beginning while maintaining the order of other numbers. The program takes the input array int a[]={5,0,4,6,0,7,0}, and the output array should be {0,0,0,5,4,6,7}.
Move Zeros Left By Maintaining the Order Of Digits
package com.softwaretestingo.interviewprograms; public class InterviewPrograms21 { /* * Input int a[]={5,0,4,6,0,7,0} * Output = {0,0,0,5,4,6,7} * Goal is to print all zero first and then the rest of other numbers as it is. */ public static void main(String[] args) { int[] array = {5,0,4,6,0,7,0}; int current = array.length - 1; for (int i = array.length - 1; i >= 0; i--) { if (array[i] != 0) { array[current] = array[i]; current--; } } while (current >= 0) { array[current] = 0; current--; } for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } }
Output
0 0 0 5 4 6 7
Let’s break down the program section by section for Java beginners:
- Class and main() Method:
- The program defines a class called InterviewPrograms21.
- It contains a main method, the entry point of the program, which will be executed first.
- Input Array:
- int[] array = {5,0,4,6,0,7,0};: This line declares and initializes the input integer array with the values {5,0,4,6,0,7,0}.
- Moving Non-Zero Elements to the End:
- The program uses a for loop to iterate over the input array in reverse order (from the end to the beginning).
- It checks if the current element is not zero (array[i] != 0).
- If it’s not zero, it moves that non-zero element to the current position indicated by the current variable.
- The current variable keeps track of the index in the array where the next non-zero element will be placed.
- Setting Zeros at the Beginning:
- After moving all non-zero elements to their correct positions, the program uses another while loop to set zeros at the beginning of the array for the remaining positions.
- It decrements the current variable and sets the value at that index to zero until it reaches index 0.
- Printing the Resultant Array:
- Finally, the program uses a for loop to iterate over the array and prints each element to the console, resulting in the output {0,0,0,5,4,6,7}.
In summary, this Java program reorders the elements in the input array such that all zeros are moved to the beginning, and the non-zero elements maintain their original order. The output array after executing the program will be {0,0,0,5,4,6,7} for the given input {5,0,4,6,0,7,0}.
Alternative 1:
This Java program achieves the goal of moving all zeros in the input array to the beginning while maintaining the order of other numbers. The program takes the input array int a[]={5,0,4,6,0,7,0}, and the output should be {0,0,0,5,4,6,7}.
package com.softwaretestingo.interviewprograms; import java.util.LinkedList; public class InterviewPrograms21_1 { /* * Input int a[]={5,0,4,6,0,7,0} * Output = {0,0,0,5,4,6,7} * Goal is to print all zero first and then the rest of other numbers as it is. */ public static void main(String[] args) { int z[]= {5,0,4,6,0,7,0}; LinkedList<Integer> zeroFirst = new LinkedList<>(); for(int i:z) { if(i==0) zeroFirst.addFirst(i); else zeroFirst.add(i); } System.out.println(zeroFirst); } }
Output
[0, 0, 0, 5, 4, 6, 7]
Let’s break down the program section by section for Java beginners:
- Class and main() Method:
- The program defines a class called InterviewPrograms21_1.
- It contains a main method, the entry point of the program, which will be executed first.
- Input Array:
- int z[]= {5,0,4,6,0,7,0};: This line declares and initializes the input integer array z with the values {5,0,4,6,0,7,0}.
- Creating a LinkedList:
- LinkedList<Integer> zeroFirst = new LinkedList<>();: A LinkedList called zeroFirst is created to store the reordered elements.
- Moving Zeroes to the Beginning:
- The program uses an enhanced for loop to iterate over the input array z.
- If the current element is zero (i==0), it is added to the beginning of the zeroFirst list using addFirst(i).
- If the current element is non-zero, it is added to the end of the zeroFirst list using add(i).
- Printing the Resultant List:
- Finally, the program prints the zeroFirst list, which now contains the elements with all zeros at the beginning and the non-zero elements maintaining their original order.
In summary, this Java program Examples reorders the elements in the input array such that all zeros are moved to the beginning, and the non-zero elements maintain their original order. The output list after executing the program will be [0, 0, 0, 5, 4, 6, 7] for the given input array {5, 0, 4, 6, 0, 7, 0}.
Alternative 2:
This Java program achieves the goal of moving all zeros in the input array to the beginning while maintaining the order of other numbers. The program takes the input array int a[]={5,0,4,6,0,7,0}, and the output should be {0,0,0,5,4,6,7}.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms21_2 { /* * Input int a[]={5,0,4,6,0,7,0} * Output = {0,0,0,5,4,6,7} * Goal is to print all zero first and then the rest of other numbers as it is. */ public static void main(String[] args) { int [] a = {5, 0, 4, 6, 0, 7, 0}; String s = ""; for (int i : a) { if (i == 0) s = i + "," + s; else s = s + i + ","; } for (String i : s.split(",")) System.out.print(i+" "); } }
Output
0 0 0 5 4 6 7
Let’s break down the program section by section for Java beginners:
- Class and main() Method:
- The program defines a class called InterviewPrograms21_2.
- It contains a main method, the entry point of the program, which will be executed first.
- Input Array:
- int [] a = {5, 0, 4, 6, 0, 7, 0};: This line declares and initializes the input integer array a with the values {5,0,4,6,0,7,0}.
- Converting Array to String:
- The program creates an empty string s to store the elements of the array in string format.
- Moving Zeroes to the Beginning:
- The program uses an enhanced for loop to iterate over the elements of the input array a.
- If the current element is zero (i == 0), it adds the value and a comma to the beginning of the string s.
- If the current element is non-zero, it appends the value and a comma to the end of the string s.
- Printing the Resultant Array:
- After processing the input array, the program uses another enhanced for loop to iterate over the elements of the split string s (using comma as a delimiter).
- It prints each element followed by a space to the console, resulting in the output {0,0,0,5,4,6,7}.
In summary, this Java program reorders the elements in the input array such that all zeros are moved to the beginning, and the non-zero elements maintain their original order. The output after executing the program will be “0,0,0,5,4,6,7” for the given input array {5, 0, 4, 6, 0, 7, 0}.
Alternative 3:
This Java program achieves the goal of moving all zeros in the input array to the beginning while maintaining the order of other numbers. The program takes the input array int a[]={5,0,4,6,0,7,0}, and the output should be {0,0,0,5,4,6,7}.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms21_3 { /* * Input int a[]={5,0,4,6,0,7,0} * Output = {0,0,0,5,4,6,7} * Goal is to print all zero first and then the rest of other numbers as it is. */ public static void main(String[] args) { int [] givenArray = { 5 , 0 , 4 , 6 , 0 , 7 , 0 }; //reqArray [] = { 0,0,0,5,4,6,7 }; int reqArray [] = new int[givenArray.length]; int reqArrayIndex = 0 ; // to add zeroes in the beginning for (int j=0; j<givenArray.length ; j++ ) { if (givenArray[j]==0) { reqArray[reqArrayIndex]=0; reqArrayIndex++ ; } } // to add digits without changing order for ( int l=0 ; l<givenArray.length; l++ ) { if (givenArray[l]!=0) { reqArray[reqArrayIndex]=givenArray[l]; reqArrayIndex++; } } for ( int arr=0; arr<reqArray.length; arr++ ) { System.out.print(reqArray[arr]+" "); } } }
Output
0 0 0 5 4 6 7
Let’s break down the program section by section for Java beginners:
- Class and main() Method:
- The program defines a class called InterviewPrograms21_3.
- It contains a main method, the entry point of the program, which will be executed first.
- Input Array:
- int [] givenArray = { 5 , 0 , 4 , 6 , 0 , 7 , 0 };: This line declares and initializes the input integer array givenArray with the values {5,0,4,6,0,7,0}.
- Resultant Array:
- int reqArray [] = new int[givenArray.length];: A new integer array reqArray is created with the same length as givenArray to store the reordered elements.
- Adding Zeroes to the Beginning:
- The program uses a for loop to iterate over the elements of the givenArray.
- If the current element is zero (givenArray[j]==0), it is added to the reqArray at the current reqArrayIndex, and the reqArrayIndex is incremented.
- Adding Non-Zero Digits:
- Another for loop is used to iterate over the elements of the givenArray.
- If the current element is not zero (givenArray[l]!=0), it is added to the reqArray at the current reqArrayIndex, and the reqArrayIndex is incremented.
- Printing the Resultant Array:
- After processing the input array, the program uses a for loop to iterate over the elements of the reqArray.
- It prints each element followed by a space to the console, resulting in the output {0,0,0,5,4,6,7}.
In summary, this Java program reorders the elements in the input array such that all zeros are moved to the beginning, and the non-zero elements maintain their original order. The output after executing the program will be “0 0 0 5 4 6 7” for the given input array {5, 0, 4, 6, 0, 7, 0}.
Alternative 4:
This Java program achieves the goal of moving all zeros in the input array to the beginning while maintaining the order of other numbers. The program takes the input array int a[]={5,0,4,6,0,7,0}, and the output should be {0,0,0,5,4,6,7}.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms21_4 { /* * Input int a[]={5,0,4,6,0,7,0} * Output = {0,0,0,5,4,6,7} * Goal is to print all zero first and then the rest of other numbers as it is. * WITH OUT BUILT IN FUNCTION */ public static void main(String[] args) { int zeroCount = 0 ; int [ ] arr = { 5 , 0 , 4 , 6 , 0 , 7 , 0 }; for (int i =0 ; i<arr.length ; i++ ) { if (arr[i] == 0) { zeroCount ++ ; } } System.out.println ("Zero count is : " +zeroCount); int arr1 [] = new int[arr.length]; for ( int i=0 ; i<zeroCount; i++) { arr1[i]=0; } int newIndex=0; for (int i=0 ; i<arr.length; i++ ) { if ( arr[i]>0 ) { arr1[zeroCount+newIndex]= arr[i]; newIndex ++ ; } } for ( int i=0 ; i<arr1.length; i++ ) { System.out.print(arr1[i]+","); } } }
Output
Zero count is : 3 0,0,0,5,4,6,7,
Let’s break down the program step by step for Java beginners:
- Class and main() Method:
- The program defines a class called InterviewPrograms21_4.
- It contains a main method, the entry point of the program, which will be executed first.
- Input Array and Zero Count:
- int zeroCount = 0;: Initialize a variable zeroCount to count the number of zeros in the input array.
- int [] arr = { 5 , 0 , 4 , 6 , 0 , 7 , 0 };: This line declares and initializes the input integer array arr with the values {5,0,4,6,0,7,0}.
- Counting Zeros:
- The program uses a for loop to iterate over the elements of the input array arr.
- If the current element is zero (arr[i] == 0), the zeroCount is incremented.
- Creating a Resultant Array:
- A new integer array arr1 is created with the same length as arr to store the reordered elements.
- Adding Zeroes to the Beginning:
- Another for loop is used to iterate zeroCount times to add zeros at the beginning of arr1.
- Adding Non-Zero Digits:
- The program uses a for loop to iterate over the elements of the input array arr again.
- If the current element is greater than zero (arr[i] > 0), it is added to arr1 at the position indicated by zeroCount + newIndex, and newIndex is incremented.
- Printing the Resultant Array:
- After processing the input array, the program uses another for loop to iterate over the elements of the arr1.
- It prints each element followed by a comma to the console, resulting in the output {0,0,0,5,4,6,7}.
In summary, this Java program reorders the elements in the input array such that all zeros are moved to the beginning, and the non-zero elements maintain their original order. The output after executing the program will be “0,0,0,5,4,6,7” for the given input array {5, 0, 4, 6, 0, 7, 0}. The program achieves this without using built-in functions, demonstrating a manual approach to achieve the desired result.
Leave a Reply