Advertisement
A_Marinov

Untitled

Apr 21st, 2021
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class test {
  2.     public static void main(String[] args) {
  3.         int arr[] = {8, 5, 11, 4, 6};
  4.         System.out.println(moves(arr));
  5.     }
  6.  
  7.     public static int moves(int arr[]) {
  8.         int moves = 0;
  9.  
  10.         int left = 0;
  11.         int right = arr.length - 1;
  12.         for (int i = 0; i < arr.length; i++) {
  13.  
  14.             while (arr[left] % 2 == 0) {
  15.                 left++;
  16.             }
  17.             while (arr[right] % 2 == 1) {
  18.                 right--;
  19.             }
  20.  
  21.             if (left < right) {
  22.                 int temp = arr[left];
  23.                 arr[left] = arr[right];
  24.                 arr[right] = temp;
  25.                 moves++;
  26.             }
  27.         }
  28.         return moves;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement