Maximum Subarray With Equal Products
Your DSA round on this problem
The interviewer starts you on this exact problem: clarify it, explain your approach, code while you talk and answer the follow-ups. Your code runs on hidden tests, and you get a scored report.
95 coins for a 45-minute round· new accounts get 100 coins free
Practise it on your own
Write your solution, run it on the examples and submit it against the hidden tests. No interviewer, and it's free.
The problem
Suppose we have a sequence of positive integers named nums.
Consider any contiguous slice of this sequence. We can calculate three values for the numbers in this slice:
- Their total product.
- Their least common multiple (LCM).
- Their greatest common divisor (GCD).
A slice is considered valid if its total product is exactly equal to its LCM multiplied by its GCD.
Determine the maximum number of elements in any valid contiguous slice from nums. If no such slice exists, the result is 0.
Example 1
Input: nums = [1,2,1,2,1,1,1]
Output: 5
Example 2
Input: nums = [2,3,4,5,6]
Output: 3
Example 3
Input: nums = [1,2,3,1,4,5,1]
Output: 5
Example 4
Input: nums = [1]
Output: 1
Example 5
Input: nums = [2]
Output: 0
Example 6
Input: nums = [2,2]
Output: 2
Example 7
Input: nums = [3,3,3]
Output: 2
Example 8
Input: nums = [1,1,1,1]
Output: 4
Example 9
Input: nums = [8,9,5,7]
Output: 4
Example 10
Input: nums = [2,3,5,7]
Output: 4
Constraints
`1 <= nums.length <= 100``1 <= nums[i] <= 10`