Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Move Zero To The End Of Array : Input => Array [0, 1, 0, 3, 12] Output => Array [1, 3, 12, 0, 0].
- public class App {
- public static void moveZero(int[] myArray) {
- int index = 0;
- for(int i=0; i < myArray.length;i++) {
- if(myArray[i] != 0) {
- myArray[index++] = myArray[i]; // 1 3 12
- }
- }
- while(index < myArray.length) {
- myArray[index++] = 0; // 1 3 12 0 0
- }
- for(int i =0 ; i < myArray.length;i++) {
- System.out.print(myArray[i] + " , ");
- }
- }
- public static void main(String[] args) {
- int[] myArray = {0, 1, 0, 3, 12};
- moveZero(myArray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement