Python Two Pointer with Prefix Sum
Intuition\nSplitting array into three subarrays, but mainly from lzl124631x, Explanation.\n\nCheck out above solution, and check code below for commented code e
# Intuition\nSplitting array into three subarrays, but mainly from [lzl124631x, Explanation.](https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/solutions/999795/c-3-pointers-o-n-time-o-1-space/)\n\n***Check out above solution, and check code below for commented code explanation.***\n\n# Approach: *Two Pointer with Prefix Sum*\n\n# Complexity: \n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n    def waysToSplit(self, nums: List[int]) -> int:\n        i = 0\n        j = 0\n        k = 0\n        mod = (10 ** 9) + 7\n        result = 0\n\n        # Things to remember, \n        # i will be pointing to index of end of the left subarray\n        # j will be pointing to index of start of the mid subarray\n        # k will be pointing to index of end of the right subarray\n\n        # Now, How to get subarray sum for left, mid and right subarray,\n        # we can create prefix sum array\n\n        prefixSum = nums.copy()\n        for i in range(1, len(nums)):\n            prefixSum[i] += prefixSum[i - 1]\n\n        # Now how to get left subarray sum,\n        # Simple, since i points to end of left subarray\n        # left subarray sum = prefixSum[i]\n\n        # Now, how to get mid subarray sum\n        # mid subarray sum = prefixSum[i] - left subarray sum\n        \n        # How, to get right subarray sum\n        # right subarray sum = prefixSum[-1] - prefixSum[k], since k points to end of mid subarray\n\n        # Whole purpose is to expand left subarray and mid subarray\n        for i in range(len(prefixSum)):\n            # left subarray sum\n            left = prefixSum[i]\n            # Where to start, j, either from its current position but it must be greater than i, as it is start of mid subarray\n            j = max(i + 1, j)\n            # Now, we will increase j pointer, until we get a mid subarray sum greater than left subarray sum\n            # Logic:? j points to start of the mid, leftSum <= midSum\n            # prefixSum[j] - left(left subarray sum) should be less than leftSum\n            while j < len(prefixSum) and prefixSum[j] - left < left:\n                j += 1\n            # No num left for right\n            if j >= len(prefixSum):\n                break\n            # Need to find right subarray sum\n            # Logic, Since, k is the end of mid subarray,\n            # right subarray sum should be greater than mid subarray sum\n            # right > mid\n            # prefixSum[-1] - prefixSum[k] >= prefixSum[k] - prefixSum[i]\n            k = max(j, k)\n            while k < len(prefixSum) - 1 and prefixSum[-1] - prefixSum[k] >= prefixSum[k] - prefixSum[i]:\n                k += 1\n            # What will be the result, Number of mid subarrays we can create\n            # So, it will be number of elements in midSubarray, i,e end point of midSubarray (k) - start point of the midSubarray\n            # Number of possible middle subarrays is k - j\n            result = (result + k - j) % mod\n        return result\n```
['Two Pointers', 'Prefix Sum', 'Python3']