Problem · Array
Find Minimum Number of Transitions
Learn this problemProblem 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:
int[] arr: an array of binary integers
Returns
int: the minimum number of transitions
Examples
Example 1
arr = [1, 0, 1, 0, 1]return = 1The 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
🐶