Skip to main content

Minimum Stability Factor of Array - Solution & Explanation

HardArrayMathBinary SearchGreedy4 min readAsked at: Amazon
Practice this problem

Problem Statement

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:

Input: nums = [3,5,10], maxC = 1

Output: 1

Explanation:

  • The stable subarray [5, 10] has HCF = 5, which has a stability factor of 2.
  • Since maxC = 1, one optimal strategy is to change nums[1] to 7, resulting in nums = [3, 7, 10].
  • Now, no subarray of length greater than 1 has HCF >= 2. Thus, the minimum possible stability factor is 1.

Example 2:

Input: nums = [2,6,8], maxC = 2

Output: 1

Explanation:

  • The subarray [2, 6, 8] has HCF = 2, which has a stability factor of 3.
  • Since maxC = 2, one optimal strategy is to change nums[1] to 3 and nums[2] to 5, resulting in nums = [2, 3, 5].
  • Now, no subarray of length greater than 1 has HCF >= 2. Thus, the minimum possible stability factor is 1.

Example 3:

Input: nums = [2,4,9,6], maxC = 1

Output: 2

Explanation:

  • The stable subarrays are:
    • [2, 4] with HCF = 2 and stability factor of 2.
    • [9, 6] with HCF = 3 and stability factor of 2.
  • Since maxC = 1, the stability factor of 2 cannot be reduced due to two separate stable subarrays. Thus, the minimum possible stability factor is 2.

 

Constraints:

  • 1 <= n == nums.length <= 105
  • 1 <= nums[i] <= 109
  • 0 <= maxC <= n

Approach Overview

Problem Overview: You are given an array and must compute the minimum possible stability factor such that the array satisfies a stability condition defined over its elements. The goal is to minimize this factor while ensuring the array remains valid according to the constraints. The challenge comes from large value ranges and the need to efficiently validate candidate stability values.

Approach 1: Brute Force Stability Check (O(n * V), Space O(1))

The most direct approach is to try every possible stability factor from the smallest feasible value up to the maximum element range. For each candidate factor, iterate through the array and verify whether the stability condition holds for all elements or segments. This involves repeated scans of the array and recomputation of constraints such as pair differences or divisibility relationships. While straightforward, this approach becomes infeasible when the value range V is large.

Approach 2: Binary Search + Greedy Validation (O(n log V), Space O(1))

A key observation is that if a stability factor S works, any larger factor will also work. This monotonic property allows you to apply binary search on the answer. For each candidate value, perform a greedy validation pass through the array. The validation checks whether the array can satisfy the stability constraint using local adjustments or allowed operations while keeping differences within the candidate bound. Each check runs in linear time, so the total complexity becomes O(n log V), which is efficient for large inputs.

Approach 3: Binary Search with Segment Tree Optimization (O(n log n log V), Space O(n))

When validation requires frequent range queries such as computing minimums, maximums, or gcd values across subarrays, a segment tree can speed up checks. Preprocess the array into a segment tree so each query runs in O(log n). During the binary search feasibility test, query ranges to verify whether the stability constraint holds across segments. This approach trades extra memory for faster constraint checks when the validation logic depends on aggregated range information.

Approach 4: Greedy + Number Theory Observations (O(n log V), Space O(1))

Some variations of the stability constraint rely on divisibility or gcd relationships between elements. Using insights from number theory, you can simplify validation by computing gcd transitions or allowable reductions directly while scanning the array. The greedy step keeps track of the minimal feasible value for each position while ensuring the candidate stability bound is not violated.

Recommended for interviews: Binary search on the answer combined with a greedy feasibility check is the approach most interviewers expect. The brute force approach demonstrates understanding of the constraint being tested, but the binary search solution shows you recognize the monotonic structure of the problem and can reduce the complexity from linear-in-range to logarithmic-in-range.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Stability CheckO(n * V)O(1)Small value ranges where testing every factor is feasible
Binary Search + Greedy ValidationO(n log V)O(1)General case with monotonic feasibility condition
Binary Search + Segment TreeO(n log n log V)O(n)When validation requires fast range queries
Greedy with Number Theory InsightsO(n log V)O(1)When stability constraints involve gcd or divisibility rules

Video Solution

3605. Minimum Stability Factor of Array | Biweekly Contest 160 • Amit Dhyani • 983 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Minimum Stability Factor of Array easy or hard?
Minimum Stability Factor of Array is considered a Hard problem because it requires recognizing a monotonic property, applying binary search on the answer, and implementing an efficient validation strategy. Some variants also involve segment trees or number theory optimizations.
Minimum Stability Factor of Array Python/Java solution
Most implementations follow the same structure across languages: binary search over the possible stability factor and a helper function that checks if the array can satisfy the constraint. The validation step runs in linear time, so the full solution remains O(n log V) in Python, Java, C++, or Go.
How to solve Minimum Stability Factor of Array in O(n log V)?
Use binary search to guess the minimum stability factor. For each candidate value, run a greedy validation pass through the array to ensure the stability constraint is satisfied. Because each validation takes O(n) time and binary search performs log V iterations, the overall complexity becomes O(n log V).
What is the best approach for Minimum Stability Factor of Array?
The most effective approach is binary search on the stability factor combined with a greedy feasibility check. The stability factor has a monotonic property: if a value works, any larger value will also work. Binary search reduces the search space while the greedy pass validates the constraint in O(n) time, giving an overall complexity around O(n log V).
Is Minimum Stability Factor of Array asked at Google/Amazon/Meta?
Problems combining binary search on answers with greedy validation frequently appear in interviews at companies like Google, Amazon, and Meta. Variants involving range queries, segment trees, or number theory are especially common in harder interview rounds and competitive programming settings.
What data structure is used in Minimum Stability Factor of Array?
The core solution uses arrays with binary search and greedy validation. For advanced implementations, a segment tree can be used to support fast range queries such as minimum, maximum, or gcd calculations during feasibility checks.
What is the time complexity of Minimum Stability Factor of Array?
The optimal solution typically runs in O(n log V), where n is the array size and V is the range of possible stability factors. The binary search contributes the log V factor, while each feasibility check scans the array once. If range queries are required, segment trees may increase complexity to O(n log n log V).

Ready to solve this problem?

Practice Minimum Stability Factor of Array with our built-in code editor and test cases.

Practice on FleetCode