Check if it is Possible to Split Array - Problem

You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n arrays of size 1 by performing a series of steps.

An array is called good if:

  • The length of the array is one, or
  • The sum of the elements of the array is greater than or equal to m.

In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two arrays, if both resulting arrays are good.

Return true if you can split the given array into n arrays, otherwise return false.

Input & Output

Example 1 — Basic Splittable Case
$ Input: nums = [2,1,2,6], m = 4
Output: true
💡 Note: We can split [2,1,2,6] → [2,1,2] and [6]. Both are good: [2,1,2] has sum 5 ≥ 4, and [6] has length 1. Continue splitting [2,1,2] until all elements are separated.
Example 2 — Non-splittable Case
$ Input: nums = [2,1,1,1], m = 4
Output: false
💡 Note: No two adjacent elements sum to 4 or more: 2+1=3, 1+1=2, 1+1=2. We cannot split any subarray of length > 2 while keeping both parts good.
Example 3 — Small Array
$ Input: nums = [1,1], m = 10
Output: true
💡 Note: Arrays of length ≤ 2 can always be split into individual elements since single elements are always considered good.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 1000
  • 1 ≤ m ≤ 200

Visualization

Tap to expand
Split Array Problem INPUT nums array: 2 i=0 1 i=1 2 i=2 6 i=3 Parameters: nums = [2,1,2,6] m = 4, n = 4 Good Array: 1. Length = 1 OR 2. Sum of elements >= m (4) ALGORITHM STEPS 1 Check Adjacent Pairs Find pairs with sum >= m 2 Greedy Observation If any pair >= m, OK! 3 Calculate Sums Check each adjacent pair 4 Return Result true if any pair works Adjacent Pair Sums: nums[0]+nums[1] = 2+1 = 3 < 4 nums[1]+nums[2] = 1+2 = 3 < 4 nums[2]+nums[3] = 2+6 = 8 >= 4 Found valid pair! OK FINAL RESULT Possible Split Sequence: [2, 1, 2, 6] [2, 1, 2] [6] [2, 1] [2] [2] [1] All arrays of size 1: [2], [1], [2], [6] Output: true Key Insight: If n <= 2, always return true. Otherwise, we only need to find ONE adjacent pair with sum >= m. This pair can always be kept together until the end, ensuring all splits are valid (greedy approach). TutorialsPoint - Check if it is Possible to Split Array | Greedy Observation Approach
Asked in
Google 12 Amazon 8 Microsoft 6
12.5K Views
Medium Frequency
~15 min Avg. Time
247 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen