Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active January 10, 2020 10:53
Show Gist options
  • Save rfaisal/5950616 to your computer and use it in GitHub Desktop.
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…
/**
* 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);
}
}
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