Skip to content

3920. Minimum Stability Factor Of Array


3920. Minimum Stability Factor of Array

Hard


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

Solution

class Solution {
    public int minStable(int[] nums, int maxC) {
        int n = nums.length;
        if (maxC == 0)
            return find(nums);
        int low = 1, high = n, ans = -1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (ok(mid, maxC, nums)) {
                ans = mid;
                high = mid - 1;
            } else
                low = mid + 1;
        }
        if (ans == -1)
            return 1;
        return ans - 1;
    }
    private boolean ok(int mid, int maxC, int arr[]) {
        int n = arr.length;
        int count = 0, i = 0;
        SparseGcd gcd = new SparseGcd(arr);
        while (i < n) {
            if (i + mid - 1 >= n)
                break;
            if (gcd.query(i, i + mid - 1) >= 2) {
                count++;
                i = i + mid;
            } else
                i++;
        }
        return count <= maxC;
    }
    private int find(int arr[]) {
        int n = arr.length;
        int count1 = 0;
        for (int ele : arr)
            if (ele == 1)
                count1++;
        if (count1 == n)
            return 0;
        int low = 1, high = n, ans = -1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (check(mid, arr)) {
                ans = mid;
                low = mid + 1;
            } else
                high = mid - 1;
        }
        return ans;
    }
    private boolean check(int mid, int arr[]) {
        SparseGcd gcd = new SparseGcd(arr);
        for (int i = 0; i < arr.length; i++) {
            if (i + mid - 1 < arr.length) {
                if (gcd.query(i, i + mid - 1) >= 2)
                    return true;
            }
        }
        return false;
    }
    static class SparseGcd {
        private int[][] sparseTable;
        private int n;

        private int gcd(int a, int b) {
            return b == 0 ? a : gcd(b, a % b);
        }

        public SparseGcd(int[] arr) {
            this.n = arr.length;
            int maxLog = (int)(Math.log(n) / Math.log(2)) + 1;
            this.sparseTable = new int[n][maxLog];

            for (int i = 0; i < n; i++)
                sparseTable[i][0] = arr[i];

            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j)) <= n; i++)
                    sparseTable[i][j] = gcd(sparseTable[i][j - 1], sparseTable[i + (1 << (j - 1))][j - 1]);
            }
        }

        public int query(int left, int right) {
            if (left < 0 || right >= n || left > right)
                throw new IllegalArgumentException("Invalid range");
            int j = (int)(Math.log(right - left + 1) / Math.log(2));
            return gcd(sparseTable[left][j], sparseTable[right - (1 << j) + 1][j]);
        }
    }


}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Explanation

[Add detailed explanation here]