FastPrepMinimum Swaps to Group Circular Ones

Minimum Swaps to Group Circular Ones

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a circular binary array nums, return the minimum number of swaps between any two positions needed to group all 1s into one contiguous circular block.

Function

minSwapsCircularOnes(nums: int[]) → int

Examples

Example 1

nums = [0,1,0,1,1,0,0]return = 1

Example 2

nums = [1,1,0,0,1]return = 0

Constraints

  • 1 <= nums.length <= 100000.
  • Every value is 0 or 1.

More Motive problems

See Motive hiring insights
public int minSwapsCircularOnes(int[] nums) {
  // write your code here
}
nums[0,1,0,1,1,0,0]
expected1
Checking account…