< Summary

Information
Class: LeetCode.Algorithms.ShortestSubarrayToBeRemovedToMakeArraySorted.ShortestSubarrayToBeRemovedToMakeArraySortedTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayToBeRemovedToMakeArraySorted\ShortestSubarrayToBeRemovedToMakeArraySortedTwoPointers.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 65
Line coverage: 100%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindLengthOfShortestSubarray(...)87.5%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayToBeRemovedToMakeArraySorted\ShortestSubarrayToBeRemovedToMakeArraySortedTwoPointers.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.ShortestSubarrayToBeRemovedToMakeArraySorted;
 13
 14/// <inheritdoc />
 15public class ShortestSubarrayToBeRemovedToMakeArraySortedTwoPointers : IShortestSubarrayToBeRemovedToMakeArraySorted
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <returns></returns>
 23    public int FindLengthOfShortestSubarray(int[] arr)
 324    {
 325        var left = 0;
 26
 827        while (left < arr.Length - 1 && arr[left] <= arr[left + 1])
 528        {
 529            left++;
 530        }
 31
 332        if (left == arr.Length - 1)
 133        {
 134            return 0;
 35        }
 36
 237        var right = arr.Length - 1;
 38
 439        while (right > 0 && arr[right - 1] <= arr[right])
 240        {
 241            right--;
 242        }
 43
 244        var result = Math.Min(arr.Length - left - 1, right);
 45
 246        var i = 0;
 247        var j = right;
 48
 949        while (i <= left && j < arr.Length)
 750        {
 751            if (arr[i] <= arr[j])
 352            {
 353                result = Math.Min(result, j - i - 1);
 54
 355                i++;
 356            }
 57            else
 458            {
 459                j++;
 460            }
 761        }
 62
 263        return result;
 364    }
 65}