SHARE
TWEET

Fair rations

Douma37 May 19th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8.  
  9. public class Solution {
  10.  
  11.     // Complete the fairRations function below.
  12.     static int fairRations(int[] B) {
  13.         int loaves = 0;
  14.         for (int i = 0; i < B.length - 1; i++) {
  15.             if (B[i] % 2 == 0) {
  16.                 continue;
  17.             }
  18.             B[i]++;
  19.             B[i+1]++;
  20.             loaves += 2;
  21.         }
  22.         if (B[B.length - 1] % 2 == 1) {
  23.             System.out.println("NO");
  24.         }
  25.         return loaves;
  26.  
  27.     }
  28.  
  29.     private static final Scanner scanner = new Scanner(System.in);
  30.  
  31.     public static void main(String[] args) throws IOException {
  32.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  33.  
  34.         int N = scanner.nextInt();
  35.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  36.  
  37.         int[] B = new int[N];
  38.  
  39.         String[] BItems = scanner.nextLine().split(" ");
  40.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  41.  
  42.         for (int i = 0; i < N; i++) {
  43.             int BItem = Integer.parseInt(BItems[i]);
  44.             B[i] = BItem;
  45.         }
  46.  
  47.         int result = fairRations(B);
  48.  
  49.         bufferedWriter.write(String.valueOf(result));
  50.         bufferedWriter.newLine();
  51.  
  52.         bufferedWriter.close();
  53.  
  54.         scanner.close();
  55.     }
  56. }
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