[LeetCode] Check if it is Possible to Split Array

2811. Check if it is Possible to Split Array

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 non-empty arrays by performing a series of steps.

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 subarrays, if, for each resulting subarray, at least one of the following holds:

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

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

Note: A subarray is a contiguous non-empty sequence of elements within an array.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool canSplitArray(vector<int>& nums, int m) {
if(nums.size() <= 2) return true;
for(int i = 0; i < nums.size() - 1; i++) {
if(nums[i] + nums[i+1] >= m) return true;
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/06/PS/LeetCode/check-if-it-is-possible-to-split-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.