< Summary

Information
Class: LeetCode.Algorithms.GreatestSumDivisibleByThree.GreatestSumDivisibleByThreeGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GreatestSumDivisibleByThree\GreatestSumDivisibleByThreeGreedy.cs
Line coverage
97%
Covered lines: 48
Uncovered lines: 1
Coverable lines: 49
Total lines: 99
Line coverage: 97.9%
Branch coverage
88%
Covered branches: 23
Total branches: 26
Branch coverage: 88.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxSumDivThree(...)88.46%262697.95%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GreatestSumDivisibleByThree\GreatestSumDivisibleByThreeGreedy.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.GreatestSumDivisibleByThree;
 13
 14/// <inheritdoc />
 15public sealed class GreatestSumDivisibleByThreeGreedy : IGreatestSumDivisibleByThree
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int MaxSumDivThree(int[] nums)
 324    {
 325        var sum = 0;
 26
 327        var smallestNumberWithRemainderOne = int.MaxValue;
 328        var secondSmallestNumberWithRemainderOne = int.MaxValue;
 329        var smallestNumberWithRemainderTwo = int.MaxValue;
 330        var secondSmallestNumberWithRemainderTwo = int.MaxValue;
 31
 2832        for (var i = 0; i < nums.Length; i++)
 1133        {
 1134            var num = nums[i];
 35
 1136            sum += num;
 37
 1138            var remainder = num % 3;
 39
 1140            switch (remainder)
 41            {
 542                case 1 when num < smallestNumberWithRemainderOne:
 343                    secondSmallestNumberWithRemainderOne = smallestNumberWithRemainderOne;
 344                    smallestNumberWithRemainderOne = num;
 45
 346                    break;
 47                case 1:
 248                    {
 249                        if (num < secondSmallestNumberWithRemainderOne)
 150                        {
 151                            secondSmallestNumberWithRemainderOne = num;
 152                        }
 53
 254                        break;
 55                    }
 356                case 2 when num < smallestNumberWithRemainderTwo:
 257                    secondSmallestNumberWithRemainderTwo = smallestNumberWithRemainderTwo;
 258                    smallestNumberWithRemainderTwo = num;
 59
 260                    break;
 61                case 2:
 162                    {
 163                        if (num < secondSmallestNumberWithRemainderTwo)
 164                        {
 165                            secondSmallestNumberWithRemainderTwo = num;
 166                        }
 67
 168                        break;
 69                    }
 70            }
 1171        }
 72
 373        var sumRemainder = sum % 3;
 74
 375        switch (sumRemainder)
 76        {
 77            case 0:
 078                return sum;
 79            case 1:
 180                {
 181                    var remove2 = smallestNumberWithRemainderTwo != int.MaxValue &&
 182                                  secondSmallestNumberWithRemainderTwo != int.MaxValue
 183                        ? smallestNumberWithRemainderTwo + secondSmallestNumberWithRemainderTwo
 184                        : int.MaxValue;
 85
 186                    return sum - Math.Min(smallestNumberWithRemainderOne, remove2);
 87                }
 88            default:
 289                {
 290                    var remove2 = smallestNumberWithRemainderOne != int.MaxValue &&
 291                                  secondSmallestNumberWithRemainderOne != int.MaxValue
 292                        ? smallestNumberWithRemainderOne + secondSmallestNumberWithRemainderOne
 293                        : int.MaxValue;
 94
 295                    return sum - Math.Min(smallestNumberWithRemainderTwo, remove2);
 96                }
 97        }
 398    }
 99}

Methods/Properties

MaxSumDivThree(System.Int32[])