Skip to content

1712. Ways to Split Array Into Three Subarrays 👍

  • Time: $O(n\log n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
 public:
  int waysToSplit(vector<int>& nums) {
    constexpr int kMod = 1'000'000'007;
    const int n = nums.size();
    int ans = 0;
    vector<int> prefix(n);

    partial_sum(nums.begin(), nums.end(), prefix.begin());

    // Finds the first index j s.t.
    // mid = prefix[j] - prefix[i] >= left = prefix[i].
    auto firstGreaterEqual = [&](int i) {
      int l = i + 1;
      int r = n - 1;
      while (l < r) {
        const int m = (l + r) / 2;
        if (prefix[m] - prefix[i] >= prefix[i])
          r = m;
        else
          l = m + 1;
      }
      return l;
    };

    // Finds the first index k s.t.
    // mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k].
    auto firstGreater = [&](int i) {
      int l = i + 1;
      int r = n - 1;
      while (l < r) {
        const int m = (l + r) / 2;
        if (prefix[m] - prefix[i] > prefix.back() - prefix[m])
          r = m;
        else
          l = m + 1;
      }
      return l;
    };

    for (int i = 0; i < n - 2; ++i) {
      const int j = firstGreaterEqual(i);
      if (j == n - 1)
        break;
      const int mid = prefix[j] - prefix[i];
      const int right = prefix.back() - prefix[j];
      if (mid > right)
        continue;
      const int k = firstGreater(i);
      ans = (ans + k - j) % kMod;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
  public int waysToSplit(int[] nums) {
    final int kMod = 1_000_000_007;
    final int n = nums.length;
    int ans = 0;
    int[] prefix = nums.clone();

    for (int i = 1; i < n; ++i)
      prefix[i] += prefix[i - 1];

    for (int i = 0; i < n - 2; ++i) {
      final int j = firstGreaterEqual(prefix, i);
      if (j == n - 1)
        break;
      final int mid = prefix[j] - prefix[i];
      final int right = prefix[prefix.length - 1] - prefix[j];
      if (mid > right)
        continue;
      final int k = firstGreater(prefix, i);
      ans = (ans + k - j) % kMod;
    }

    return ans;
  }

  // Finds the first index j s.t.
  // mid = prefix[j] - prefix[i] >= left = prefix[i].
  private int firstGreaterEqual(int[] prefix, int i) {
    int l = i + 1;
    int r = prefix.length - 1;
    while (l < r) {
      final int m = (l + r) / 2;
      if (prefix[m] - prefix[i] >= prefix[i])
        r = m;
      else
        l = m + 1;
    }
    return l;
  };

  // Finds the first index k s.t.
  // mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k].
  private int firstGreater(int[] prefix, int i) {
    int l = i + 1;
    int r = prefix.length - 1;
    while (l < r) {
      final int m = (l + r) / 2;
      if (prefix[m] - prefix[i] > prefix[prefix.length - 1] - prefix[m])
        r = m;
      else
        l = m + 1;
    }
    return l;
  };
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution:
  def waysToSplit(self, nums: list[int]) -> int:
    kMod = 1_000_000_007
    n = len(nums)
    ans = 0
    prefix = list(itertools.accumulate(nums))

    def firstGreaterEqual(i: int) -> int:
      """Finds the first index j s.t.
         Mid = prefix[j] - prefix[i] >= left = prefix[i]
      """
      l = i + 1
      r = n - 1
      while l < r:
        m = (l + r) // 2
        if prefix[m] - prefix[i] >= prefix[i]:
          r = m
        else:
          l = m + 1
      return l

    def firstGreater(i: int) -> int:
      """Finds the first index k s.t.
         mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k]
      """
      l = i + 1
      r = n - 1
      while l < r:
        m = (l + r) // 2
        if prefix[m] - prefix[i] > prefix[-1] - prefix[m]:
          r = m
        else:
          l = m + 1
      return l

    for i in range(n - 2):
      j = firstGreaterEqual(i)
      if j == n - 1:
        break
      mid = prefix[j] - prefix[i]
      right = prefix[-1] - prefix[j]
      if mid > right:
        continue
      k = firstGreater(i)
      ans = (ans + k - j) % kMod

    return ans

Approach 2: Prefix Sum

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
 public:
  int waysToSplit(vector<int>& nums) {
    constexpr int kMod = 1'000'000'007;
    const int n = nums.size();
    int ans = 0;
    vector<int> prefix(n);

    partial_sum(nums.begin(), nums.end(), prefix.begin());

    for (int i = 0, j = 0, k = 0; i < n - 2; ++i) {
      // Find the first index j s.t.
      // left = prefix[i] <= mid = prefix[j] - prefix[i]
      j = max(j, i + 1);
      while (j < n - 1 && prefix[i] > prefix[j] - prefix[i])
        ++j;
      // Find the first index k s.t.
      // mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k]
      k = max(k, j);
      while (k < n - 1 && prefix[k] - prefix[i] <= prefix.back() - prefix[k])
        ++k;
      ans += k - j;
      ans %= kMod;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
  public int waysToSplit(int[] nums) {
    final int kMod = 1_000_000_007;
    final int n = nums.length;
    int ans = 0;
    int[] prefix = nums.clone();

    for (int i = 1; i < n; ++i)
      prefix[i] += prefix[i - 1];

    for (int i = 0, j = 0, k = 0; i < n - 2; ++i) {
      // Find the first index j s.t.
      // left = prefix[i] <= mid = prefix[j] - prefix[i]
      j = Math.max(j, i + 1);
      while (j < n - 1 && prefix[i] > prefix[j] - prefix[i])
        ++j;
      // Find the first index k s.t.
      // mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k]
      k = Math.max(k, j);
      while (k < n - 1 && prefix[k] - prefix[i] <= prefix[prefix.length - 1] - prefix[k])
        ++k;
      ans += k - j;
      ans %= kMod;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
  def waysToSplit(self, nums: list[int]) -> int:
    kMod = 1_000_000_007
    n = len(nums)
    ans = 0
    prefix = list(itertools.accumulate(nums))

    j = 0
    k = 0
    for i in range(n - 2):
      # Find the first index j s.t.
      # left = prefix[i] <= mid = prefix[j] - prefix[i]
      j = max(j, i + 1)
      while j < n - 1 and prefix[i] > prefix[j] - prefix[i]:
        j += 1
      # Find the first index k s.t.
      # mid = prefix[k] - prefix[i] > right = prefix[-1] - prefix[k]
      k = max(k, j)
      while k < n - 1 and prefix[k] - prefix[i] <= prefix[-1] - prefix[k]:
        k += 1
      ans += k - j
      ans %= kMod

    return ans