rishu110067

Untitled

Jan 27th, 2022 (edited)
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1.  
  2.     static ArrayList<Integer> segregate_evens_and_odds(ArrayList<Integer> numbers) {
  3.         // Write your code here.
  4.         segregate_evens_and_odds_helper(numbers, 0, numbers.size()-1); // pass index only
  5.         System.out.println(numbers);
  6.         return numbers;
  7.     }
  8.    
  9.     static void segregate_evens_and_odds_helper(ArrayList<Integer> numbers, int start, int end) {
  10.         // Base Case
  11.         if(start >= end) {
  12.             return;
  13.         }
  14.        
  15.         //Recursive Case
  16.         int odd = start;
  17.  
  18.         for(int even = start; even <= end-1; even++) {
  19.             if(isEven(numbers.get(even))) {
  20.                 Collections.swap(numbers, odd, even) ;
  21.                 odd ++;
  22.             }
  23.         }
  24.         Collections.swap(numbers, odd, end);
  25.     }
  26.    
  27.     static boolean isEven(int number) {
  28.         if(number%2 == 0)
  29.             return true;
  30.         return false;
  31.     }
  32.  
Add Comment
Please, Sign In to add comment