< Summary

Information
Class: LeetCode.Algorithms.MaximumNumberOfDistinctElementsAfterOperations.MaximumNumberOfDistinctElementsAfterOperationsSortingGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfDistinctElementsAfterOperations\MaximumNumberOfDistinctElementsAfterOperationsSortingGreedy.cs
Line coverage
82%
Covered lines: 19
Uncovered lines: 4
Coverable lines: 23
Total lines: 62
Line coverage: 82.6%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxDistinctElements(...)75%8882.6%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfDistinctElementsAfterOperations\MaximumNumberOfDistinctElementsAfterOperationsSortingGreedy.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.MaximumNumberOfDistinctElementsAfterOperations;
 13
 14public sealed class MaximumNumberOfDistinctElementsAfterOperationsSortingGreedy :
 15    IMaximumNumberOfDistinctElementsAfterOperations
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(log n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int MaxDistinctElements(int[] nums, int k)
 225    {
 226        var numsLength = nums.Length;
 27
 228        if (k >= numsLength)
 029        {
 030            return numsLength;
 31        }
 32
 233        Array.Sort(nums);
 34
 235        if (nums[0] == nums[^1])
 136        {
 137            return Math.Min(numsLength, (2 * k) + 1);
 38        }
 39
 140        var previous = nums[0] - k;
 41
 142        var distinctCount = 1;
 43
 1244        for (var i = 1; i < numsLength; i++)
 545        {
 546            var num = nums[i];
 47
 548            var current = Math.Min(Math.Max(previous + 1, num - k), num + k);
 49
 550            if (current <= previous)
 051            {
 052                continue;
 53            }
 54
 555            distinctCount++;
 56
 557            previous = current;
 558        }
 59
 160        return distinctCount;
 261    }
 62}