Problem · Array

Find Minimum Number of Transitions

Learn this problem
EasyTiktokOA
See Tiktok hiring insights

Problem statement

Find the minimum number of changes to make the array consisting of binary numbers with the least number of transitions from 0 -> 1 or 1 -> 0.

Function

minTransitions(arr: int[]) → int

Complete the function minTransitions in the editor.

minTransitions has the following parameter:

  1. int[] arr: an array of binary integers

Returns

int: the minimum number of transitions

Examples

Example 1

arr = [1, 0, 1, 0, 1]return = 1
The array can be transformed to [0, 0, 1, 1, 1] after swapping the index 0 and 3, resulting in only one transition from 0 to 1. Alternatively, it can also be transformed to [1, 1, 1, 0, 0] with one transition from 1 to 0. Therefore, the minimum number of transitions is 1.

Constraints

🐶

More Tiktok problems

drafts saved locally
public int minTransitions(int[] arr) {
  // write your code here
}
arr[1, 0, 1, 0, 1]
expected1
checking account