< Summary

Information
Class: LeetCode.Algorithms.SumOfSquareNumbers.SumOfSquareNumbersBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfSquareNumbers\SumOfSquareNumbersBinarySearch.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
JudgeSquareSum(...)100%44100%
BinarySearch(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfSquareNumbers\SumOfSquareNumbersBinarySearch.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.SumOfSquareNumbers;
 13
 14/// <inheritdoc />
 15public class SumOfSquareNumbersBinarySearch : ISumOfSquareNumbers
 16{
 17    /// <summary>
 18    ///     Time complexity - O(Sqrt(c) * log c)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="c"></param>
 22    /// <returns></returns>
 23    public bool JudgeSquareSum(int c)
 1124    {
 1416625        for (long a = 0; a * a <= c; a++)
 708026        {
 708027            var b = c - (int)(a * a);
 28
 708029            if (BinarySearch(0, b, b))
 830            {
 831                return true;
 32            }
 707233        }
 34
 335        return false;
 1136    }
 37
 38    private static bool BinarySearch(long s, long e, int n)
 708039    {
 22594540        while (true)
 22594541        {
 22594542            if (s > e)
 707243            {
 707244                return false;
 45            }
 46
 21887347            var mid = s + ((e - s) / 2);
 48
 21887349            if (mid * mid == n)
 850            {
 851                return true;
 52            }
 53
 21886554            if (mid * mid > n)
 15763355            {
 15763356                e = mid - 1;
 57
 15763358                continue;
 59            }
 60
 6123261            s = mid + 1;
 6123262        }
 708063    }
 64}