Having trouble with my solution and suspect that the .splice() function might be in the wrong place since I keep timing out.
The issue:
You have an array of integers. Each move allows you to increase one element by one. Find the minimum number of moves needed to get a strictly increasing sequence from the input.
Example
If inputArray = [1, 1, 1], then arrayChange(inputArray) should equal 3.
My Pseudo code
First, check if the current index is greater than the next index. If not, continue checking through the loop. If yes, add one to the next index and repeat until true. Increment the "moves" variable each time you increase the next index by one. Return the total moves in the end.
function arrayChange(inputArray) {
for( var i = 0; i < inputArray.length; i++){
var addition = (inputArray[i+1]+1)
if(inputArray[i] >= inputArray[i+1]){
inputArray.splice(i,0, addition);
}
}
return inputArray;
}
My Error:
Test 1: Execution time limit exceeded. The program took too long to complete execution. Ensure it finishes quickly for all possible inputs.