code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

013 Array Change

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
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.

Example

For inputArray = [1, 1, 1], the output should be arrayChange(inputArray) = 3. */

function arrayChange(arr) {
  let count = 0
  for (let index = 0; index < arr.length; index++) {
    if (arr[index] >= arr[index + 1]) {
      const difference = arr[index] + 1 - arr[index + 1]
      arr[index + 1] = arr[index] + 1
      count += difference
    }
  }
  return count
}

test('array Change', () => {
  expect(arrayChange([1, 1, 1])).toEqual(3)
})

Created 2019-12-07T17:23:15+05:18 · Edit