DEV Community

Prashant Mishra
Prashant Mishra

Posted on

No of ways to split Array

Problem

TC: O(n) for calculating prefix sum, and O(n) for iterating over the prefix sum for calculating valid splits

class Solution {
    public int waysToSplitArray(int[] nums) {
        long prefix[] = new long[nums.length];
        long current = 0;
        for(int i=0;i<nums.length;i++){
            current+=nums[i];
            prefix[i] = current;
        } 

        int count =0;
        for(int i =0;i<nums.length-1;i++){
            long left = prefix[i];
            long right = prefix[nums.length-1];
            if(left>=right-left) count++;
        }
        return count;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay