FastPrepFind Minimum Number of Transitions

Find Minimum Number of Transitions

TikTok logoTikTok● EasyOA

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

See TikTok hiring insights
Saved
public int minTransitions(int[] arr) {
  // write your code here
}
arr[1, 0, 1, 0, 1]
expected1
Checking account…