LeetCode solutions

1062. Partition Array Into Three Parts With Equal Sum

My accepted C++ solution to LeetCode problem 1062, Partition Array Into Three Parts With Equal Sum, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 36.5MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 36.5MB, accepted 2025-12-28.

cpp
class Solution {
public:
    bool canThreePartsEqualSum(vector<int>& arr) {
        int total = 0;
        for (int x : arr) total += x;
        if (total % 3 != 0) return false;
        int target = total / 3;
        int sum = 0, count = 0;
        for (int i = 0; i < (int)arr.size() - 1; i++) {
            sum += arr[i];
            if (sum == target * (count + 1)) count++;
            if (count == 2) return true;
        }
        return false;
    }
};

Source