plz tell my mistake
My code has failed 1 test case
if the input is 1, your code gives an Array out of bounds exception which means you are accessing an invalid array index. Please correct your code for input N = 1.
what will be the expected output if input is 1, Iām still not able to figure it out
@sanchit.bansal06
@Sadaf_khan0107,
https://ide.codingblocks.com/s/234920 corrected code.
Use need to use else if instead of if in the highlighted line.
Say the input is N=1.
if (N == 1)
f[0] = 0;
if (N == 2) { //USE ELSE IF INSTEAD OF IF.
f[0] = 0;
f[1] = 1;
} else {
f[0] = 0;
f[1] = 1;
for (int i = 2; i < N; i++)
f[i] = f[i - 1] + f[i - 2];
}
Now in the above code if N =1, if(N==1) this block of code is executed.
We go to the next block, it says if(N==2), this is false. Hence it will go to the else block (since the if block is not executed, it will execute the else block).
In the else block you will get an array out of bounds exception in this line: f[1] = 1;
Because f has a size of 1.