Class Solution
-
- All Implemented Interfaces:
public final class Solution2584 - Split the Array to Make Coprime Products.
Hard
You are given a 0-indexed integer array
numsof lengthn.A split at an index
iwhere0 <= i <= n - 2is called valid if the product of the firsti + 1elements and the product of the remaining elements are coprime.For example, if
nums = [2, 3, 3], then a split at the indexi = 0is valid because2and9are coprime, while a split at the indexi = 1is not valid because6and3are not coprime. A split at the indexi = 2is not valid becausei == n - 1.
Return the smallest index
iat which the array can be split validly or-1if there is no such split.Two values
val1andval2are coprime ifgcd(val1, val2) == 1wheregcd(val1, val2)is the greatest common divisor ofval1andval2.Example 1:
Input: nums = 4,7,8,15,3,5
Output: 2
Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. The only valid split is at index 2.
Example 2:
Input: nums = 4,7,15,8,3,5
Output: -1
Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. There is no valid split.
Constraints:
n == nums.length<code>1 <= n <= 10<sup>4</sup></code>
<code>1 <= numsi<= 10<sup>6</sup></code>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classSolution.Companion
-
Field Summary
Fields Modifier and Type Field Description public final static Solution.CompanionCompanion
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerfindValidSplit(IntArray nums)-
-
Method Detail
-
findValidSplit
final Integer findValidSplit(IntArray nums)
-
-
-
-