🌟 Beat 100% | 🔥Optimized Approach 💡 | Beginner-Friendly 🐣 | Easy Solution ✅
Intuition 🧠
The goal is to find splits in the array such that the sum of the left part is greater than or equal to the sum of the right part. This involves calc
# Intuition 🧠
- The goal is to find splits in the array such that the sum of the left part is greater than or equal to the sum of the right part. This involves calculating prefix sums efficiently.

# *Brute Force Approach* 🚶‍♂️
## *Approach*:

- Compute two prefix sum arrays (left and right) for the left and right parts of the array.
- Iterate through all valid splits (i = 0 to n-2) and compare left[i] with right[i+1].
- Count the number of valid splits.
## Time Complexity (TC):

- O(n) for calculating left and right prefix sums.
- O(n) for iterating through the splits.
- Overall: O(n)
## Space Complexity (SC):

- O(n) for storing left and right prefix sum arrays.
# *Optimized Approach* 🚀
## *Approach*:

-     Calculate the total sum of the array in one pass.
- Use a single variable (leftSum) to track the sum of the left part dynamically while iterating through the array.
- For each split, calculate the right part as totalSum - leftSum on the fly and compare.
-     Count the number of valid splits.
## Time Complexity (TC):

- O(n) for calculating the total sum and iterating through the splits.
## Space Complexity (SC):

- O(1) since we don’t use any extra space for prefix sums.

![Screenshot 2025-01-03 104806.png](https://assets.leetcode.com/users/images/ed96ee51-8e0d-4909-8ea4-c5154e01bbe2_1735882879.845559.png)

# Code
```cpp []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        
        //brute force 
        int n = nums.size() ;
        vector<long long> left(n , 0) ;
        vector<long long> right(n , 0) ;

        //prefixsum left ka
        long long sum = 0 ;
        for(int i = 0 ; i < n ; i++)
        {
            sum += nums[i] ;
            left[i] = sum ; 
        }
        //prefixsum right ka
        sum = 0 ;
        for(int i = n-1 ; i >= 0 ; i--)
        {
            sum += nums[i] ;
            right[i] = sum ; 
        }
        int ans = 0 ;
        for(int i = 0 ; i < left.size()-1 ; i++ )
        {
            if(left[i] >= right[i+1])
            {
                ans++ ;
            }
        }
        return ans ;   


        //optimised solution 
        long long leftSum = 0 ;
        long long totalSum = 0 ;

        for(auto num : nums)
        {
            totalSum += num ;
        }
        int ans = 0 ;
        for(int i = 0 ; i < nums.size() -1 ; i++ )
        {
            leftSum += nums[i] ;
            long long rightSum = totalSum - leftSum  ;
            if(leftSum >= rightSum)
            {
                ans++ ;
            }
        }
        return ans ;
    }
};
```
```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        long totalSum = 0, leftSum = 0;
        for (int num : nums) {
            totalSum += num;
        }
        int ans = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            leftSum += nums[i];
            long rightSum = totalSum - leftSum;
            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```python []
class Solution:
    def waysToSplitArray(self, nums: list[int]) -> int:
        totalSum = sum(nums)
        leftSum = 0
        ans = 0
        for i in range(len(nums) - 1):
            leftSum += nums[i]
            rightSum = totalSum - leftSum
            if leftSum >= rightSum:
                ans += 1
        return ans

```
```ruby []
def ways_to_split_array(nums)
    total_sum = nums.sum
    left_sum = 0
    ans = 0

    (0...nums.size - 1).each do |i|
        left_sum += nums[i]
        right_sum = total_sum - left_sum
        ans += 1 if left_sum >= right_sum
    end

    ans
end

```
```typescript []
function waysToSplitArray(nums: number[]): number {
    let totalSum = nums.reduce((a, b) => a + b, 0);
    let leftSum = 0, ans = 0;

    for (let i = 0; i < nums.length - 1; i++) {
        leftSum += nums[i];
        let rightSum = totalSum - leftSum;
        if (leftSum >= rightSum) {
            ans++;
        }
    }
    return ans;
}

```
```C# []
public class Solution {
    public int WaysToSplitArray(int[] nums) {
        long totalSum = 0, leftSum = 0;
        foreach (int num in nums) {
            totalSum += num;
        }
        int ans = 0;
        for (int i = 0; i < nums.Length - 1; i++) {
            leftSum += nums[i];
            long rightSum = totalSum - leftSum;
            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```Go []
func waysToSplitArray(nums []int) int {
    totalSum, leftSum := 0, 0
    for _, num := range nums {
        totalSum += num
    }
    ans := 0
    for i := 0; i < len(nums)-1; i++ {
        leftSum += nums[i]
        rightSum := totalSum - leftSum
        if leftSum >= rightSum {
            ans++
        }
    }
    return ans
}

```
![Screenshot 2025-01-03 102600.png](https://assets.leetcode.com/users/images/d3a95ee7-3f4b-4e51-8df7-f6276e31e2af_1735882904.8415322.png)



['Prefix Sum', 'Python', 'C++', 'Java', 'Go', 'Rust', 'Ruby', 'JavaScript', 'C#', 'Dart']