< Summary

Information
Class: LeetCode.Algorithms.ArithmeticSubarrays.ArithmeticSubarraysHashSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ArithmeticSubarrays\ArithmeticSubarraysHashSet.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 71
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CheckArithmeticSubarrays(...)91.66%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ArithmeticSubarrays\ArithmeticSubarraysHashSet.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.ArithmeticSubarrays;
 13
 14/// <inheritdoc />
 15public class ArithmeticSubarraysHashSet : IArithmeticSubarrays
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="l"></param>
 23    /// <param name="r"></param>
 24    /// <returns></returns>
 25    public IList<bool> CheckArithmeticSubarrays(int[] nums, int[] l, int[] r)
 626    {
 627        var result = new bool[l.Length];
 28
 4829        for (var i = 0; i < l.Length; i++)
 1830        {
 3631            int start = l[i], end = r[i];
 3632            int minVal = int.MaxValue, maxVal = int.MinValue;
 1833            var elements = new HashSet<int>();
 34
 15635            for (var j = start; j <= end; j++)
 6036            {
 6037                minVal = Math.Min(minVal, nums[j]);
 6038                maxVal = Math.Max(maxVal, nums[j]);
 6039                elements.Add(nums[j]);
 6040            }
 41
 1842            var subArrayLength = end - start + 1;
 43
 1844            if (subArrayLength < 2 || (maxVal - minVal) % (subArrayLength - 1) != 0)
 445            {
 446                result[i] = false;
 47
 448                continue;
 49            }
 50
 1451            var diff = (maxVal - minVal) / (subArrayLength - 1);
 1452            var isArithmetic = true;
 53
 10654            for (var j = 0; j < subArrayLength; j++)
 4155            {
 4156                if (elements.Contains(minVal + (j * diff)))
 3957                {
 3958                    continue;
 59                }
 60
 261                isArithmetic = false;
 62
 263                break;
 64            }
 65
 1466            result[i] = isArithmetic;
 1467        }
 68
 669        return result;
 670    }
 71}