Simple and Easy Solution | ✅Beats 100% | C++| Java | Python | JavaScript
⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]Solution in C++, Python, Java, and JavaScriptIntuitionThe goal is to split the array such that th
# ⬆️Upvote if it helps ⬆️

---

## Connect with me on Linkedin [Bijoy Sing]

---

### Solution in C++, Python, Java, and JavaScript

```cpp []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        long long tot = 0, left = 0;
        int ans = 0;

        for (int num : nums) {
            tot += num;
        }

        for (int i = 0; i < nums.size() - 1; ++i) {
            left += nums[i];
            if (left >= tot - left) {
                ++ans;
            }
        }

        return ans;
    }
};
```

```python []
class Solution:
    def waysToSplitArray(self, nums):
        tot = 0
        left = 0
        ans = 0
        
        for num in nums:
            tot += num
        
        for i in range(len(nums) - 1):
            left += nums[i]
            if left >= tot - left:
                ans += 1
        
        return ans
```

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

```javascript []
class Solution {
    waysToSplitArray(nums) {
        let tot = 0, left = 0;
        let ans = 0;

        for (let num of nums) {
            tot += num;
        }

        for (let i = 0; i < nums.length - 1; i++) {
            left += nums[i];
            if (left >= tot - left) {
                ans++;
            }
        }

        return ans;
    }
}
```

---

# Intuition
The goal is to split the array such that the sum of the left part is greater than or equal to the sum of the right part. We iterate through the array, calculating the total sum and then comparing the running sum (left side) to the remaining sum (right side).

# Approach
1. Calculate the total sum of the array.
2. Traverse the array, maintaining a running sum of the left part.
3. For each split, check if the left sum is greater than or equal to the right sum.
4. Count the number of valid splits.

# Complexity
- Time complexity: \(O(n)\)
  - We iterate over the array twice, once to calculate the total sum and once to check each possible split.
  
- Space complexity: \(O(1)\)
  - Only a few variables are used to store sums and the result.

### *If you have any questions or need further clarification, feel free to drop a comment! 😊*
['Array', 'C', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']