< Summary

Information
Class: LeetCode.Algorithms.KthLargestElementInStream.KthLargestElementInStreamSortedArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamSortedArray.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%66100%
Add(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamSortedArray.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.KthLargestElementInStream;
 13
 14/// <inheritdoc />
 15public sealed class KthLargestElementInStreamSortedArray : IKthLargestElementInStream
 16{
 17    private readonly int _k;
 18    private readonly int[] _nums;
 19
 420    public KthLargestElementInStreamSortedArray(int k, int[] nums)
 421    {
 422        _k = k;
 23
 424        var newNums = new int[k];
 25
 426        Array.Fill(newNums, int.MinValue);
 27
 428        if (nums.Length > 0)
 429        {
 1730            Array.Sort(nums, (a, b) => b.CompareTo(a));
 31
 432            var i = 0;
 33
 1434            while (i < nums.Length && i < k)
 1035            {
 1036                newNums[i] = nums[i];
 37
 1038                i++;
 1039            }
 440        }
 41
 442        _nums = newNums;
 443    }
 44
 45    /// <summary>
 46    ///     Time complexity - O(k)
 47    ///     Space complexity - O(k)
 48    /// </summary>
 49    /// <param name="val"></param>
 50    /// <returns></returns>
 51    public int Add(int val)
 1552    {
 11053        for (var i = 0; i < _nums.Length; i++)
 4054        {
 4055            if (_nums[i] < val)
 1556            {
 1557                (_nums[i], val) = (val, _nums[i]);
 1558            }
 4059        }
 60
 1561        return _nums[_k - 1];
 1562    }
 63}