Skip to content

2811. Check if it is Possible to Split Array 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  bool canSplitArray(vector<int>& nums, int m) {
    if (nums.size() < 3)
      return true;

    for (int i = 1; i < nums.size(); ++i)
      if (nums[i] + nums[i - 1] >= m)
        return true;

    return false;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public boolean canSplitArray(List<Integer> nums, int m) {
    if (nums.size() < 3)
      return true;

    for (int i = 1; i < nums.size(); ++i)
      if (nums.get(i) + nums.get(i - 1) >= m)
        return true;

    return false;
  }
}
1
2
3
class Solution:
  def canSplitArray(self, nums: list[int], m: int) -> bool:
    return len(nums) < 3 or any(a + b >= m for a, b in itertools.pairwise(nums))