Duplicate Zeros Runtime Error
class Solution {
    public void duplicateZeros(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            //if an element is zero, move every other element over once.
            if(arr[i] == 0) {
                for(int j = arr.length-2; j >= i+1; j--) {
                    arr[j+1] = arr[j];
                }
                //insert 0 at i+1 index
                arr[i+1] = 0;
                //skip the zero you just duplicated.
                i++;
            }
            
        }
        
    }
}

When I run this code I get a runtime error, but it works fine in IntelliJ. Anyone know why? Is it leetcode's problem or the code? Thanks

Comments (1)