< Summary

Information
Class: LeetCode.Algorithms.PartitionArrayIntoThreePartsWithEqualSum.PartitionArrayIntoThreePartsWithEqualSumIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PartitionArrayIntoThreePartsWithEqualSum\PartitionArrayIntoThreePartsWithEqualSumIterative.cs
Line coverage
95%
Covered lines: 39
Uncovered lines: 2
Coverable lines: 41
Total lines: 92
Line coverage: 95.1%
Branch coverage
94%
Covered branches: 17
Total branches: 18
Branch coverage: 94.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanThreePartsEqualSum(...)94.44%181895.12%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PartitionArrayIntoThreePartsWithEqualSum\PartitionArrayIntoThreePartsWithEqualSumIterative.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.PartitionArrayIntoThreePartsWithEqualSum;
 13
 14/// <inheritdoc />
 15public class PartitionArrayIntoThreePartsWithEqualSumIterative : IPartitionArrayIntoThreePartsWithEqualSum
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <returns></returns>
 23    public bool CanThreePartsEqualSum(int[] arr)
 1524    {
 1525        var totalSum = arr.Sum();
 26
 1527        if (totalSum % 3 != 0)
 428        {
 429            return false;
 30        }
 31
 1132        var singleSum = totalSum / 3;
 33
 1134        var firstSum = 0;
 35
 1136        var index = 0;
 37
 8638        for (var i = 0; i < arr.Length; i++)
 4039        {
 4040            firstSum += arr[i];
 41
 4042            if (firstSum != singleSum)
 3243            {
 3244                continue;
 45            }
 46
 847            index = i + 1;
 48
 849            break;
 50        }
 51
 1152        if (index >= arr.Length)
 053        {
 054            return false;
 55        }
 56
 1157        var secondSum = 0;
 58
 10059        for (var i = index; i < arr.Length; i++)
 4760        {
 4761            secondSum += arr[i];
 62
 4763            if (secondSum != singleSum)
 3964            {
 3965                continue;
 66            }
 67
 868            index = i + 1;
 69
 870            break;
 71        }
 72
 1173        if (index >= arr.Length)
 174        {
 175            return false;
 76        }
 77
 1078        var thirdSum = 0;
 79
 10480        for (var i = index; i < arr.Length; i++)
 4981        {
 4982            thirdSum += arr[i];
 83
 4984            if (thirdSum == singleSum)
 785            {
 786                break;
 87            }
 4288        }
 89
 1090        return totalSum - firstSum - secondSum - thirdSum == 0;
 1591    }
 92}