Advertisement
saurav_kalsoor

Game with Thor and Loki - JAVA

May 16th, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  2. // Game with Thor and Loki - JAVA
  3.  
  4. import java.util.*;
  5.  
  6. public class Test {
  7.    
  8.     static Scanner sc = new Scanner(System.in);
  9.     public static void main(String[] args) {
  10.         int n = sc.nextInt();
  11.  
  12.         ArrayList<Integer> arr = new ArrayList<>();
  13.  
  14.         for(int i=0; i < n; i++) {
  15.             int input = sc.nextInt();
  16.             arr.add(input);
  17.         }
  18.  
  19.         gameWithThorAndLoki(arr, n);
  20.  
  21.     }
  22.  
  23.     public static void gameWithThorAndLoki(ArrayList<Integer> arr, int n){
  24.         int start = 0, end = n - 1;
  25.         boolean isThorsTurn = true;
  26.        
  27.         ArrayList<Integer> res = recurr(arr, start, end, isThorsTurn);
  28.  
  29.         System.out.println(res.get(0) + " " + res.get(1));
  30.     }
  31.  
  32.     public static ArrayList<Integer> recurr(ArrayList<Integer> arr, int start, int end, boolean isThorsTurn){
  33.         if(start > end){
  34.             ArrayList<Integer> res = new ArrayList<Integer>();
  35.             res.add(0);
  36.             res.add(0);
  37.             return res;
  38.         }
  39.  
  40.         int left = arr.get(start);
  41.         int right = arr.get(end);
  42.         int currThor = 0, currLoki = 0;
  43.  
  44.         if(isThorsTurn){
  45.             currThor = Math.max(left, right);
  46.             currLoki = Math.min(left, right);
  47.         }else{
  48.             currLoki = Math.max(left, right);
  49.             currThor = Math.min(left, right);
  50.         }
  51.  
  52.         start++;
  53.         end--;
  54.         ArrayList<Integer> res = recurr(arr, start, end, !isThorsTurn);
  55.  
  56.         res.set(0, res.get(0) + currThor);
  57.         res.set(1, res.get(1) + currLoki);
  58.         return res;
  59.     }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement