< Summary

Information
Class: LeetCode.Algorithms.FindThePivotInteger.FindThePivotIntegerIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindThePivotInteger\FindThePivotIntegerIterative.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 63
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
PivotInteger(...)100%66100%
Sum(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindThePivotInteger\FindThePivotIntegerIterative.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.FindThePivotInteger;
 13
 14/// <inheritdoc />
 15public class FindThePivotIntegerIterative : IFindThePivotInteger
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <returns></returns>
 23    public int PivotInteger(int n)
 424    {
 425        var left = 1;
 426        var right = n;
 27
 1228        while (left <= right)
 1029        {
 1030            var mid = left + ((right - left) / 2);
 1031            var leftSum = Sum(1, mid);
 1032            var rightSum = Sum(mid, n);
 33
 1034            if (leftSum == rightSum)
 235            {
 236                return mid;
 37            }
 38
 839            if (leftSum < rightSum)
 640            {
 641                left = mid + 1;
 642            }
 43            else
 244            {
 245                right = mid - 1;
 246            }
 847        }
 48
 249        return -1;
 450    }
 51
 52    private static int Sum(int left, int right)
 2053    {
 2054        var sum = 0;
 55
 21456        for (var i = left; i <= right; i++)
 8757        {
 8758            sum += i;
 8759        }
 60
 2061        return sum;
 2062    }
 63}