Key Generation Vulnerabilities: Finding the Least Vulnerability Factor with Modifications
2. Code Question 2
The developers at Amazon IAM are working on identifying vulnerabilities in their key generation process. The key is represented by an array of integers, where the h 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 any 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.
Example
key = [2, 2, 4, 9, 6]
maxChange = 1
The length of key, n = 5. Here are some possible changes to make:
1. Change the first element to 3. The array becomes 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].
2. Change the third element of the array to 5. The array becomes 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.
Function Description
Complete the function findVulnerabilityFactor in the editor below.
findVulnerabilityFactor has the following parameters: int key[n]: the original encryption key int maxChange: the maximum number of elements that can be changed
Returns int: the least possible vulnerability factor of the key
Constraints
. 1 ≤ n ≤ 105
. 0 ≤ maxChange ≤ n
. 1 ≤ key[i] ≤ 109
Function
key[] size n = 6
key = [5, 10, 20, 10, 15, 5]
Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
STDIN
10
20
Asked in:
AMAZON