Last active
January 10, 2020 10:53
-
-
Save rfaisal/5950616 to your computer and use it in GitHub Desktop.
Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the pla…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem Link: http://codeforces.com/problemset/problem/6/C | |
* Alice = getDivideIndex(..)+1; | |
* Bob = arrayLength - Alice; | |
*/ | |
public class Codeforces6C { | |
private static int getDivideIndexRec(int[] arr, int i, int j, boolean isLeft){ | |
if(i==j){ | |
if(isLeft) return i-1; | |
else return i; | |
} | |
if(arr[i]<arr[j]){ | |
arr[j]-=arr[i]; | |
return getDivideIndexRec(arr,i+1,j,true); | |
} | |
else if(arr[i]>arr[j]){ | |
arr[i]-=arr[j]; | |
return getDivideIndexRec(arr,i,j-1,false); | |
} | |
else{ | |
return getDivideIndexRec(arr,i+1,j-1,false); | |
} | |
} | |
public static int getDivideIndex(int[] arr){ | |
if(arr.length<1) return -1;//error | |
return getDivideIndexRec(arr,0,arr.length-1,false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Codeforces6CTest { | |
@Test | |
public void testGetDivideIndex() { | |
assertEquals(1,Codeforces6C.getDivideIndex(new int[]{2,9,8,2,7})); | |
assertEquals(0,Codeforces6C.getDivideIndex(new int[]{2})); | |
assertEquals(2,Codeforces6C.getDivideIndex(new int[]{2,2,2,2,2})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment