|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-subarrays-in-a-valid-split">2607. Minimum Subarrays in a Valid Split</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>Splitting of an integer array <code>nums</code> into <strong>subarrays</strong> is <strong>valid</strong> if:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>the <em>greatest common divisor</em> of the first and last elements of each subarray is <strong>greater</strong> than <code>1</code>, and</li> |
| 7 | + <li>each element of <code>nums</code> belongs to exactly one subarray.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return <em>the <strong>minimum</strong> number of subarrays in a <strong>valid</strong> subarray splitting of</em> <code>nums</code>. If a valid subarray splitting is not possible, return <code>-1</code>.</p> |
| 11 | + |
| 12 | +<p><strong>Note</strong> that:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li>The <strong>greatest common divisor</strong> of two numbers is the largest positive integer that evenly divides both numbers.</li> |
| 16 | + <li>A <strong>subarray</strong> is a contiguous non-empty part of an array.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> nums = [2,6,3,4,3] |
| 24 | +<strong>Output:</strong> 2 |
| 25 | +<strong>Explanation:</strong> We can create a valid split in the following way: [2,6] | [3,4,3]. |
| 26 | +- The starting element of the 1<sup>st</sup> subarray is 2 and the ending is 6. Their greatest common divisor is 2, which is greater than 1. |
| 27 | +- The starting element of the 2<sup>nd</sup> subarray is 3 and the ending is 3. Their greatest common divisor is 3, which is greater than 1. |
| 28 | +It can be proved that 2 is the minimum number of subarrays that we can obtain in a valid split. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 2:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> nums = [3,5] |
| 35 | +<strong>Output:</strong> 2 |
| 36 | +<strong>Explanation:</strong> We can create a valid split in the following way: [3] | [5]. |
| 37 | +- The starting element of the 1<sup>st</sup> subarray is 3 and the ending is 3. Their greatest common divisor is 3, which is greater than 1. |
| 38 | +- The starting element of the 2<sup>nd</sup> subarray is 5 and the ending is 5. Their greatest common divisor is 5, which is greater than 1. |
| 39 | +It can be proved that 2 is the minimum number of subarrays that we can obtain in a valid split. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong class="example">Example 3:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input:</strong> nums = [1,2,1] |
| 46 | +<strong>Output:</strong> -1 |
| 47 | +<strong>Explanation:</strong> It is impossible to create valid split. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 55 | + <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> |
| 56 | +</ul> |
0 commit comments