#3605

Minimum Stability Factor of Array

hard · 20.3% accepted · 41 likes · top 2%

array · math · binary search · greedy · segment tree · number theory

Description

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.

Solution