The key is represented by an array of n integers, where the ith integer is denoted by key[i]. The vulnerability factor of the array (key) is defined as the maximum length of a subarray that has a Greatest Common Divisor (GCD) greater than 1.
You are allowed to make a maximum of maxChange modifications to the array, where each modification consists of changing one element in the array to any other value.
Given an integer array key and an integer maxChange, find the least possible vulnerability factor of the key after making at most maxChange changes.
Note: The length of an empty subarray is considered to be zero.
key = [2, 2, 4, 9, 6]
maxChange = 1
The length of key, n = 5. Here are some possible changes to make:
key = [3, 2, 4, 9, 6]. The length of the longest subarray with a GCD greater than 1 is 2 for the subarrays [2, 4] and [9, 6].key = [2, 2, 5, 9, 6]. In this case, the length of the longest subarray with a GCD greater than 1 is 2 for the subarrays [2, 2] and [9, 6].Since no operation can reduce the maximum length of any subarray with a GCD greater than 1 to less than 2, the vulnerability factor of the key is 2.
Complete the function findVulnerabilityFactor in the editor below.
findVulnerabilityFactor has the following parameters:
int key[n]: the original encryption keyint maxChange: the maximum number of elements that can be changedReturns
int: the least possible vulnerability factor of the keyint findVulnerabilityFactor(int[] key, int maxChange)
key = [2, 4, 6, 8]
maxChange = 1
Output: 2
With one change, you can break the long even segment, but you cannot eliminate all adjacent even pairs. The minimum possible longest subarray with GCD > 1 has length 2.
key = [6, 10, 15]
maxChange = 1
Output: 1
Change the middle value (e.g., to 7) so no length-2 subarray has GCD > 1. Single-element subarrays like [6] still have GCD > 1, so the best possible vulnerability factor is 1.
key = [2, 2, 2]
maxChange = 3
Output: 0
Change all elements to 1. Then no subarray has GCD greater than 1, so the maximum length is the empty subarray length 0.