< Summary

Information
Class: LeetCode.Algorithms.FindIfArrayCanBeSorted.FindIfArrayCanBeSortedTwoPass
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfArrayCanBeSorted\FindIfArrayCanBeSortedTwoPass.cs
Line coverage
94%
Covered lines: 34
Uncovered lines: 2
Coverable lines: 36
Total lines: 75
Line coverage: 94.4%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanSortArray(...)91.66%121292.59%
GetSetBitsCount(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfArrayCanBeSorted\FindIfArrayCanBeSortedTwoPass.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.FindIfArrayCanBeSorted;
 13
 14/// <inheritdoc />
 15public class FindIfArrayCanBeSortedTwoPass : IFindIfArrayCanBeSorted
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log k)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public bool CanSortArray(int[] nums)
 324    {
 3025        for (var i = 0; i < nums.Length - 1; i++)
 1226        {
 1227            if (nums[i] <= nums[i + 1])
 628            {
 629                continue;
 30            }
 31
 632            if (GetSetBitsCount(nums[i]) == GetSetBitsCount(nums[i + 1]))
 633            {
 634                (nums[i], nums[i + 1]) = (nums[i + 1], nums[i]);
 635            }
 36            else
 037            {
 038                return false;
 39            }
 640        }
 41
 2842        for (var i = nums.Length - 1; i >= 1; i--)
 1243        {
 1244            if (nums[i] >= nums[i - 1])
 845            {
 846                continue;
 47            }
 48
 449            if (GetSetBitsCount(nums[i]) == GetSetBitsCount(nums[i - 1]))
 350            {
 351                (nums[i], nums[i - 1]) = (nums[i - 1], nums[i]);
 352            }
 53            else
 154            {
 155                return false;
 56            }
 357        }
 58
 259        return true;
 360    }
 61
 62    private static int GetSetBitsCount(int number)
 2063    {
 2064        var count = 0;
 65
 8666        while (number > 0)
 6667        {
 6668            count += number & 1;
 69
 6670            number >>= 1;
 6671        }
 72
 2073        return count;
 2074    }
 75}