condition is Input: arr = [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
public void duplicateZeros(int[] arr) {
int length = arr.length;
for(int i=0;i<length;i++){
if(arr[i]==0){
arr[i+1]=arr[i];
}
}
}
}
error is
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
at line 6, Solution.duplicateZeros
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
why i am getting this error?