After every iteration, you are not resetting the values. You must reset the arrays arr, narr and variable j. Whats happening is, after giving correct output for iteration 1, j is still having value of 14 or something depending on size of array of previous TC, and this is causing you to get immediate WA/
while(arr[j]==i){
What if the rainbow array is of size 100? Your code will then try to check the â100thâ (0 based indexing) index, which will either cause it to go into undefined behavior (WA) or runtime error. Either add a check for j<100 && arr[j]==1 or increase array size to 105 or something (whose default value will be 0, causing the loop to exit).
@vijju123 I have now added the condition that array elements should be between 1 to 7(both included) and the output for above changes to yes still it showing wrong answer.