Coin Flip | DSA Learning Series - Contest 1

Problem Statement ;[CodeChef: Practical coding for everyone]

My approach to this problem seems to be correct, but getting Run Time Error .

Can someone help me where the below code went wrong to throw Runtime Error.

static long playGame(int ini, int n, int q) {
long headctr = 0, tailCtr = 0;
char[] charr = new char[n];

if(ini == 1)
Arrays.fill(charr,‘H’);
else
Arrays.fill(charr,‘T’);

for(int i=0; i<n; i++) {
for(int j=0; j<=i; j++) {
if(charr[j] == ‘H’)
charr[j] = ‘T’;
else
charr[j] = ‘H’;
}
}
for(int i=0; i<charr.length; i++) {
if(charr[i] == ‘H’) headctr++;
else tailCtr++;
}
return (q==1)?headctr:tailCtr;
}

Thanks :slightly_smiling_face:

I have done some amendments. Check it here.

1 Like

Can I know what changes do you done? Because still getting the same Runtime error.

I didn’t know that it was a java code. So I just changed the initialization part of the array.
I think the code is fine, but it is not efficient as the time complexity is O(nnt*g).
Each test case can be done in O(1).
Sorry for the inconvenience.

OK thanks.