About strictly increasing sequence

Problem:
You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.

Solution:

function arrayChange($inputArray) {
    $count = 0;
        
    foreach ( $inputArray as $key => $value ) {
        $is_less = isset( $key ) && isset( $inputArray[ $key-1 ] ) && $value <= $inputArray[ $key-1 ];
        if ( $is_less ) {        
            $inputArray[ $key ] = ( $inputArray[ $key - 1 ] ) + 1;
            $count += $inputArray[ $key-1 ]- $value + 1;
        }
    }
  
    return $count;
}

Leave a Reply

Your email address will not be published. Required fields are marked *