SHARE
TWEET

Untitled

a guest Jan 20th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static int[] moveZerosToEnd(int[] arr) {
  2.     if(arr == null || arr.length ==0){
  3.       return arr;
  4.     }
  5.         // your code goes here
  6.     int zero = 0;
  7.     int nonZero =0;
  8.     while(zero<arr.length){
  9.       while(zero<arr.length && arr[zero]!=0){
  10.         zero++;
  11.       }
  12.       if(zero == arr.length){
  13.         break;
  14.       }
  15.       nonZero = zero;
  16.       while(nonZero<arr.length && arr[nonZero]==0){
  17.           nonZero++;
  18.       }
  19.  
  20.       if(nonZero == arr.length){
  21.         break;
  22.       }
  23.      
  24.       arr[zero] = arr[nonZero];
  25.       arr[nonZero] = 0;
  26.       zero = zero + 1;
  27.     }
  28.     return arr;
  29.    
  30.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top