-1

The 2 corners coin game receive an array. The goal of the game is accumulate the number of points (values of the elements in the array) the most. you can take points only from the 2 corners of the array.

There are 2 conditions for the game:

1) The first player (amir) would never lose (He will win or finish with a tie) and he will not necessarily choose the biggest edge.

2) The second player (Tamara) will always take the points at the highest corner.

My Output:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72

Expected Output:

Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73

The problem:

-amir will select 16 so tamara will must select the 23 so in the next turn he will select 30 (becuase tamara will always select the biggest corner).

-amir in the last turn will select 13 and not 14 because he is already won so he do not care about the point/coin value.

The reasons why amir choose 13 and not 14: 1. because tamara will always select the biggest "end" 2. because he is not loosing this game (65 vs 59) so he will choose 13 and not 14 this is strategy - just not loosing (can finish with tie or win the game) he can plan all his move because he can see the array from the begging and he want to not loose this game with less move

-amir knows from the first turn what is next move because he can not lose in this game (he can finish the game as a winner or finish with the same score of tamara)

-Amir can calculate in advance the full moves tree of the game and how Tamar can respond to each move and then how he will respond to each move. The problem with such a solution could be a huge tree (the number of different games that Amir and Tamara can play is 2 ^ K - and if K is bigger then the powerful computer will take trillions of years). Therefore, an effective solution such as this is required in this game. and requires Amir to carry out a few actions for work out a strategy for himself

The array:

int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();

My code:

public static void coingame(int[] arr)
{ 
   int n = arr.length;  
   int i = 0, j = n-1,p1=0,p2=0,totalP2=0,totalP1=0; 

   while(j > i){

      if(arr[j]+arr[j-1]>arr[i]+arr[i+1]){
      p1 = arr[j];--j;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }else{ 
         p1 = arr[i];++i;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }

      System.out.println ("amir took "+p1);totalP1+=p1;
      System.out.println ("tamara took "+p2);totalP2+=p2;
   }

   System.out.println ("Final Score:");
   System.out.println ("amir total "+totalP1);
   System.out.println ("tamara total "+totalP2);
} 

Edit: (the answer of Akshay Batra)

public static int[] pickByAmir(int[] coins, int amirTook, int tamaraTook, int start, int end) {
    if(start>end) {
        int[] res = new int[2];
        res[0] = amirTook;
        res[1] = tamaraTook;
        return res;
    }
    int[] a = new int[2];
    a[0] = amirTook;
    a[1] = tamaraTook;
    if(coins.length==0)
        return a;
    amirTook = coins[start];
    coins = pickByTamara(coins, ++start , end);
    tamaraTook = coins[start];
    a = pickByAmir(coins, amirTook+a[0], tamaraTook+a[1], ++start, end);
    int[] b = new int[2];
    b[0] = amirTook;
    b[1] = tamaraTook;
    if(a[0]<a[1]){
        amirTook = coins[end];
        coins = pickByTamara(coins, start, --end);
        b = pickByAmir(coins, amirTook+b[0], tamaraTook+b[1], ++start, end);

        if(a[0]<b[0])
            return b;
    }
        System.out.println ("Amir took "+amirTook);
        System.out.println ("Tamara took "+tamaraTook);
    return a;
}
public static int[] pickByTamara(int[] coins, int start, int end){
    return coins[start] > coins[end] ? coins : swapArray(coins, start, end);
}

public static int[] swapArray(int[] coins, int start, int end) {
    int temp = coins[start];
    coins[start] = coins[end];
    coins[end] = temp;
    return coins;
}

public static void coingame(int[] arr) {
    int[] a = pickByAmir(arr, 0, 0, 0, arr.length-1);
    System.out.println ("Final Score: ");
    System.out.println ("Amir total: "+a[0]);
    System.out.println ("Tamara total: "+a[1]);
    }
AnnaLA
  • 155
  • 1
  • 11
  • What makes Amir pick 13 instead of 14? Aren't both OK? – Dawood ibn Kareem Jan 09 '19 at 19:44
  • 1
    The least you can do is indent and format your code properly. – Joakim Danielson Jan 09 '19 at 19:44
  • you are considering this problem is to be very easy. what if before last iteration amir had 71 and tamara had 73, then no matter which one amir take among 13 and 14, he is going to loose. hint would be to try creating a recursive method so you can backtrack if condition fails. re-post the question again if still in doubt – Akshay Batra Jan 09 '19 at 19:48
  • 1
    I don't understand what the criteria is for choosing numbers. Why pick 16 if 23 or 30 are available? – markspace Jan 09 '19 at 19:53
  • @markspace he must to choose one of the 2 corner of the array... so in his first turn he can choose just 15 or 16. but amir will not choose 16 just because it bigger then 15. – AnnaLA Jan 09 '19 at 19:55
  • @AkshayBatra amir can not lose (but he can finish the game as winner or finish with the same score of tamara) – AnnaLA Jan 09 '19 at 19:56
  • 1
    @AnnaLA I understand the problem, but I think this is a practice question and i can give you a code which will work but you need to understand the idea behind it. If you still want the code, then I can post an answer – Akshay Batra Jan 09 '19 at 20:01
  • @AkshayBatra n the last turn is 65 vs 59 so amir already won (so he will choose 13 and not 14). the thing amir know because he play first he will do everything for not loosing the game (he can finish the game as winner or the same score)... please post the answer. – AnnaLA Jan 09 '19 at 20:07
  • @DawoodibnKareem in the last turn is 65 vs 59 so amir already won (so he will choose 13 and not 14). the thing amir know because he play first he will do everything for not loosing the game (he can finish the game as winner or the same score) – AnnaLA Jan 09 '19 at 20:12
  • I still don't understand. Why doesn't he just pick the larger of the two ends of the array anyway? Why does he choose 13? – Dawood ibn Kareem Jan 09 '19 at 20:17
  • 1
    @DawoodibnKareem After thinking about this, I think the OP means that the entire array is visible, and it's up to the chooser to pick the one that will best lead to victory in the long run. This may result in not picking the largest value at each step. Also, I was confused by corner = end. Like a line segment, one dimensional arrays have ends, not corners. – markspace Jan 09 '19 at 20:27
  • @markspace you right – AnnaLA Jan 09 '19 at 20:29
  • @markspace Yes, I understood that OP meant "end" when they wrote "corner". But I still don't understand. 14 and 13 are both winning moves for Amir. So they are both acceptable moves for him to choose. Now OP's algorithm chooses 14 in this instance. I don't see why this is a bug. Why is it necessary to have an algorithm that chooses 13 for Amir, if both moves are equally good? – Dawood ibn Kareem Jan 09 '19 at 20:46
  • 1. because tamara will always select the biggest "end" 2. because he is not loosing this game (65 vs 59) so he will choose 13 and not 14 this is strategy - just not loosing (can finish with tie or win the game) he can plan all his move because he can see the array from the begging and he want to not loose this game with less move @DawoodibnKareem – AnnaLA Jan 09 '19 at 20:52

1 Answers1

1

this code works with your inputs, try it with different inputs and see if it breaks, if it does, optimise your solution

call this way from caller method int[] a = pickByAmir(array1, 0, 0, 0, array1.length-1);

a[0] will have amir's total and a[1] will have tamara's

int[] pickByAmir(int[] coins, int amirTook, int tamaraTook, int start, int end) {
    if(start>end) {
        int[] res = new int[2];
        res[0] = amirTook;
        res[1] = tamaraTook;
        return res;
    }
    int[] a = new int[2];
    a[0] = amirTook;
    a[1] = tamaraTook;
    if(coins.length==0)
        return a;
    amirTook = coins[start];
    coins = pickByTamara(coins, ++start , end);
    tamaraTook = coins[start];
    a = pickByAmir(coins, amirTook+a[0], tamaraTook+a[1], ++start, end);
    int[] b = new int[2];
    b[0] = amirTook;
    b[1] = tamaraTook;
    if(a[0]<a[1]){
        amirTook = coins[end];
        coins = pickByTamara(coins, start, --end);
        b = pickByAmir(coins, amirTook+b[0], tamaraTook+b[1], ++start, end);
        if(a[0]<b[0])
            return b;
    }
    return a;
}
int[] pickByTamara(int[] coins, int start, int end){
    return coins[start] > coins[end] ? coins : swapArray(coins, start, end);
}

int[] swapArray(int[] coins, int start, int end) {
    int temp = coins[start];
    coins[start] = coins[end];
    coins[end] = temp;
    return coins;
}

GOOD LUCK

Akshay Batra
  • 137
  • 7
  • thank you for your answer... i tried your answer but i am getting a good total score... but not in the good order "results: Amir took 19 tamara took 14 Amir took 13 tamara took 21 Amir took 30 tamara took 15 Amir took 16 tamara took 23 Final Score: Amir total: 78 Tamar total: 73 " – AnnaLA Jan 10 '19 at 05:24
  • I just added your code with my text addition in my question (last part).. if you can see pls what wrong with that... what about do even and odd strategy? – AnnaLA Jan 10 '19 at 07:10
  • @AnnaLa I think instead of swapping first and last elements you can reverse the array. better to use list, will be alot easier – Akshay Batra Jan 10 '19 at 07:39
  • could you edit your answer please? with even and odd strategy so the order will be "Amir took 16 Tamara took 23 Amir took 30 Tamara took 15 Amir took 19 Tamara took 21 Amir took 13 Tamara took 14 Final Score: Amir total 78 Tamara total 73" TIA – AnnaLA Jan 10 '19 at 08:27