Problem · Array

Array Transformation Steps (Early Career)

Learn this problem
MediumGoogleFULLTIMEOA
See Google hiring insights

Problem statement

You are given an array, and the task is to determine how many steps it takes to transform an array of all zeros into the given array. In each step, you can add a contiguous sequence of 1's to the array.

Function

transformationSteps(array: int[]) → int

Examples

Example 1

array = [2, 1, 0, 2]return = 4

The transformation can be done in 4 steps as follows:

  1. 0000 -> 1100: Add a contiguous sequence of 1's covering the first two elements.
  2. 1100 -> 2100: Add a contiguous sequence of 1's covering the first element.
  3. 2100 -> 2101: Add a contiguous sequence of 1's covering the fourth element.
  4. 2101 -> 2102: Add a contiguous sequence of 1's covering the fourth element again.

Constraints

🐻‍❄️🐻‍❄️

More Google problems

drafts saved locally
public int transformationSteps(int[] array) {
  // write your code here
}
array[2, 1, 0, 2]
expected4
checking account