< Summary

Information
Class: LeetCode.Algorithms.ValidPerfectSquare.ValidPerfectSquareBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidPerfectSquare\ValidPerfectSquareBinarySearch.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 51
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsPerfectSquare(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidPerfectSquare\ValidPerfectSquareBinarySearch.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.ValidPerfectSquare;
 13
 14/// <inheritdoc />
 15public class ValidPerfectSquareBinarySearch : IValidPerfectSquare
 16{
 17    /// <summary>
 18    ///     Time complexity - O(log n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="num"></param>
 22    /// <returns></returns>
 23    public bool IsPerfectSquare(int num)
 324    {
 325        long left = 1;
 326        long right = num;
 27
 3928        while (left <= right)
 3729        {
 3730            var mid = left + ((right - left) / 2);
 31
 3732            var sqrt = mid * mid;
 33
 3734            if (sqrt == num)
 135            {
 136                return true;
 37            }
 38
 3639            if (sqrt < num)
 740            {
 741                left = mid + 1;
 742            }
 43            else
 2944            {
 2945                right = mid - 1;
 2946            }
 3647        }
 48
 249        return false;
 350    }
 51}

Methods/Properties

IsPerfectSquare(System.Int32)