< Summary

Information
Class: LeetCode.Algorithms.FindKthSmallestPairDistance.FindKthSmallestPairDistanceBinarySearchWithSlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindKthSmallestPairDistance\FindKthSmallestPairDistanceBinarySearchWithSlidingWindow.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SmallestDistancePair(...)100%44100%
CountPairsWithDistanceLessOrEqual(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindKthSmallestPairDistance\FindKthSmallestPairDistanceBinarySearchWithSlidingWindow.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.FindKthSmallestPairDistance;
 13
 14/// <inheritdoc />
 15public class FindKthSmallestPairDistanceBinarySearchWithSlidingWindow : IFindKthSmallestPairDistance
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n + n log (max(nums)))
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int SmallestDistancePair(int[] nums, int k)
 325    {
 326        Array.Sort(nums);
 27
 328        var left = 0;
 329        var right = nums[^1] - nums[0];
 30
 731        while (left < right)
 432        {
 433            var mid = left + ((right - left) / 2);
 434            var count = CountPairsWithDistanceLessOrEqual(nums, mid);
 35
 436            if (count >= k)
 237            {
 238                right = mid;
 239            }
 40            else
 241            {
 242                left = mid + 1;
 243            }
 444        }
 45
 346        return left;
 347    }
 48
 49    private static int CountPairsWithDistanceLessOrEqual(IReadOnlyList<int> nums, int target)
 450    {
 451        var count = 0;
 452        var left = 0;
 53
 2454        for (var right = 1; right < nums.Count; right++)
 855        {
 1656            while (nums[right] - nums[left] > target)
 857            {
 858                left++;
 859            }
 60
 861            count += right - left;
 862        }
 63
 464        return count;
 465    }
 66}