While doing the coding problem "https://codeforces.com/problemset/problem/1472/D"
as i was getting the error during evaluation
"Time limit exceeded on test 10"
for the code
static void solve(long a[]){
Arrays.sort(a);
long al=0,bo=0;
int t=0;
for(int i=a.length-1;i>=0;i--){
if(t==0 && a[i]%2==0) al+=a[i];
else if(t==1 && a[i]%2!=0) bo+=a[i];
t=(t+1)%2;
}
if(al>bo) System.out.println("Alice");
else if(al<bo) System.out.println("Bob");
else System.out.println("Tie");
}
I went to the leaderboards to check out how others had answered the question in my programming language (java) and i stumbled upon the solution "https://codeforces.com/problemset/submission/1472/103564414"
Taking out the relevant part in this code :
Arrays.sort(arr);
Collections.reverse(Arrays.asList(arr));
long ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && arr[i] % 2 == 0) {
ans += arr[i];
} else if (i % 2 == 1 && arr[i] % 2 == 1) {
ans -= arr[i];
}
}
if (ans > 0) {
System.out.println("Alice");
} else if (ans == 0) {
System.out.println("Tie");
} else {
System.out.println("Bob");
}
I am a bit confused as to how the other person person's code is passing the tests even though even my approach is almost identical(as per my knowledge).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…