Skip to content

Missing Test Case - 1013. Partition Array Into Three Parts With Equal Sum #28416

@sagarpatel288

Description

@sagarpatel288

LeetCode Username

srdpatel

Problem Number, Title, and Link

Problem Number: 1013
Title: Partition Array Into Three Parts With Equal Sum
Link:
https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/

Bug Category

Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)

Bug Description

Bug Description:
Wrong solutions are getting accepted.

Problem statement:
Given an array of integers arr, return true if we can partition the array into three non-empty parts with equal sums.

Input: arr = [10, 5, 8, 2, 7, 4]
Expected Output: True, because we can have 3 non-empty parts with equal sums as: [10, 2], [5, 7], and [8, 4].
Actual Output: False
Bug: The bug is that even an incorrect/inefficient code is getting successfully accepted because of missing test cases.

I saw many accepted solutions there that have used a simple forward iteration, count, and comparison with the target sum.

For example: One of the Java solutions (Seems like the most upvoted solution):

    public boolean canThreePartsEqualSum(int[] A) {
        int sum = Arrays.stream(A).sum(), part = 0, average = sum / 3, cnt = 0;
        for (int a : A) {
            part += a;
            if (part == average) { // find the average: sum / 3
                ++cnt; // find an average, increase the counter.
                part = 0; // reset part.
            }
        }
        return cnt >= 3 && sum % 3 == 0;
    } 

The solution returns false instead of true, but it is accepted!

Similarly, One of the Kotlin solutions:

class Solution {
    fun canThreePartsEqualSum(nums: IntArray): Boolean {
        val totalSum = nums.sum()
        if (totalSum % 3 != 0) return false

        var count = 0
        var sum = 0
        val targetSum = totalSum / 3

        for (num in nums) {
            sum += num
            if (sum == targetSum) {
                sum = 0
                count++
            }
        }

        return count >= 3
    }
}

The kotlin solution also returns false instead of true, but it is still accepted.

Language Used for Code

Kotlin

Code used for Submit/Run operation

class Solution {
    fun canThreePartsEqualSum(nums: IntArray): Boolean {
        val totalSum = nums.sum()
        if (totalSum % 3 != 0) return false

        var count = 0
        var sum = 0
        val targetSum = totalSum / 3

        for (num in nums) {
            sum += num
            if (sum == targetSum) {
                sum = 0
                count++
            }
        }

        return count >= 3
    }
}

Expected behavior

The given code returns false instead of true for the given input arr = [10 5 8 2 7 4], but it is still getting accepted.

Why should it return true for the given input?

Because, we can have 3 non-empty parts with equal sums as: [10, 2], [5, 7], and [8, 4]. So, it satisfies the problem requirements.

Hence, only the code that returns true should be accepted.

Screenshots

Image

Additional context

Only the solution that returns true for the given input [10, 5, 8, 2, 7, 4] should be accepted.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions