Minimum Stability Factor of Array

You are given an integer array nums and an integer maxC.

A subarray is called stable if the highest common factor (HCF) of all its elements is greater than or equal to 2.

The stability factor of an array is defined as the length of its longest stable subarray.

You may modify at most maxC elements of the array to any integer.

Return the minimum possible stability factor of the array after at most maxC modifications. If no stable subarray remains, return 0.

Note:

  • The highest common factor (HCF) of an array is the largest integer that evenly divides all the array elements.
  • A subarray of length 1 is stable if its only element is greater than or equal to 2, since HCF([x]) = x.
Example 1
Inputnums = [3,5,10], maxC = 1
Output1
Changing nums[1] to 7 gives nums = [3, 7, 10], so no subarray of length greater than 1 has HCF >= 2 and the minimum possible stability factor is 1.
Example 2
Inputnums = [2,6,8], maxC = 2
Output1
Changing nums[1] to 3 and nums[2] to 5 gives nums = [2, 3, 5], so no subarray of length greater than 1 has HCF >= 2 and the minimum possible stability factor is 1.

Constraints

  • 1 <= n == nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 0 <= maxC <= n

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate