bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 548. Split Array with Equal Sum

548. Split Array with Equal Sum

Leetcode Solutions

Using Cumulative Sum and HashSet

  1. Compute the cumulative sum array sum such that sum[i] holds the sum of elements from nums[0] to nums[i].
  2. Iterate over the possible positions for the middle cut j.
  3. For each j, create a new HashSet set.
  4. Iterate over the possible positions for the left cut i that are less than j.
  5. If sum[i-1] (sum of the first part) is equal to sum[j-1] - sum[i] (sum of the second part), add sum[i-1] to set.
  6. Iterate over the possible positions for the right cut k that are greater than j.
  7. If sum[n-1] - sum[k] (sum of the fourth part) is equal to sum[k-1] - sum[j] (sum of the third part) and the sum exists in set, return true.
  8. If no such triplet is found, return false.
UML Thumbnail

Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...