A_Marinov

Untitled

Apr 21st, 2021
603
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×