982. Longest Factor Chain

Today your maths teacher has taught you to calculate the number of factors of any number. He has given you assignment to complete. The assignment is based on factor chain. Factor chain can be explained through the following example

Let say we have number 10. Number of factors for 10 are 4 (1, 2, 5, 10). Now our number becomes 4 and number of factors for 4 are 3 (1, 2, 4). Now our number becomes 3 and number of factors for 3 are 2 (1, 3). Now our number becomes 2 and number of factors for 2 are 2 (1, 2). Now here we stop because it goes into infinite loop. So our Factors chain will be

10 -> 4 -> 3 -> 2. The length of the chain is 3.

You are given an array nums of n integers and q queries will be asked to you. The queries are of three types

1) + x, You need to add element x to the array.

2) – 1, You need to remove the number that has longest length of factors chain. If two or more numbers have same length then remove the smallest integer of all that numbers.

3) ? 1, You need to output the number that has longest length of factors chain. If two or more numbers have same length then output the smallest integer of all that numbers.

It is guaranteed that the query has at least 1 query of third type. The 2nd ad 3rd query will always be valid.

Example 1:

Input : nums = [6, 10, 12, 18, 25, 40, 3, 55] , queries = [ ["?", "1"], ["-", "1"], ["?", "1"], ["-", "1"], ["-", "1"], ["-", "1"], ["?", "1"], ["-", "1"], ["+", "48"], ["?", "1"] ]

Output : [12, 18, 10 48]

Explanation :

For 1st query we have have 12, 18, 40 having same maximum factors chain length 4. So we output the 12 as it Is smallest among all.

Now after removing 1 element we have 18, 40 having same maximum factors chain length 4 for 3rd query. So we output 18 for 3rd query.

Now 7th query we have 10 , 55 having same maximum factors chain length 3. But 10 is small so we output 10 for the 7th query.

Now for the 9th query we add the 48 to the array.

For the 10th query we have only 48 that has maximum factors chain length 4. So we output 48.

Example 2:

Input : nums = [2, 4, 8, 16] , queries = [ ["?", "1"], ["-", "1"], ["-", "1"], ["+", "32"], ["?", "1"] ]

Output : [8, 32]

Explanation :

For 1st query we have have 8 having maximum factors chain length 3 (8 -> 4 - 3 -> 2). So we output the 8.

After performing the 2nd query the array becomes [2, 4, 16].

In 3rd query we have two numbers 4 (4 -> 3 -> 2) and 16 (16 -> 5 -> 2) with same factor chain of length 3. As 4 is smaller than 16, we remove 4. New array becomes [2, 16]

In 4th query we add 32 to the array -> [2, 16, 32]

Now for the 5th query we have 32 having maximum factor chain with length of 4 (32 -> 6 -> 4 -> 3 -> 2). So we output 32.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n, q <= 105
  • 1 <= nums[i] <= 105
0
class Solution{
public:
vector<int> maxFactorsChain(vector<int>& nums, vector<vector<string>>& query){
//your code goes here
}
};
Test Case

Input:

Nums
Query