FastPrepMaximum Consecutive Ones in a Circular Array

Maximum Consecutive Ones in a Circular Array

LinkedIn logoLinkedIn● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a circular binary array nums, you may change at most k zeros to ones. Return the maximum number of consecutive ones on the circle after those changes.

A circular segment may wrap from the end to the beginning, but it may contain each array position at most once, so its length never exceeds nums.length.

Function

longestCircularOnes(nums: int[], k: int) → int

Examples

Example 1

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

Flipping the final zero joins the suffix and prefix for four consecutive ones.

Example 2

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

One zero can be flipped, producing a circular run of two but not three.

Constraints

  • 1 <= nums.length <= 200000
  • Each value is 0 or 1.
  • 0 <= k <= nums.length

More LinkedIn problems

See LinkedIn hiring insights
public int longestCircularOnes(int[] nums, int k) {
    // Write your code here.
}
nums[1,0,1,1,0]
k1
expected4
Checking account…