< Summary

Information
Class: LeetCode.Algorithms.CountSpecialTriplets.CountSpecialTripletsDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSpecialTriplets\CountSpecialTripletsDictionary.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 77
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SpecialTriplets(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSpecialTriplets\CountSpecialTripletsDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.CountSpecialTriplets;
 13
 14/// <inheritdoc />
 15public sealed class CountSpecialTripletsDictionary : ICountSpecialTriplets
 16{
 17    private const int Modulo = 1_000_000_007;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="nums"></param>
 24    /// <returns></returns>
 25    public int SpecialTriplets(int[] nums)
 326    {
 327        long result = 0;
 28
 329        var numsLength = nums.Length;
 30
 331        var rightNumToCountDictionary = new Dictionary<int, int>();
 32
 3033        for (var i = 0; i < nums.Length; i++)
 1234        {
 1235            var num = nums[i];
 36
 1237            if (rightNumToCountDictionary.TryAdd(num, 1))
 738            {
 739                continue;
 40            }
 41
 542            rightNumToCountDictionary[num]++;
 543        }
 44
 345        var leftNumToCountDictionary = new Dictionary<int, int>();
 46
 3047        for (var j = 0; j < numsLength; j++)
 1248        {
 1249            var num = nums[j];
 50
 1251            rightNumToCountDictionary[num]--;
 52
 1253            var middle = num * 2;
 54
 1255            long leftCount = leftNumToCountDictionary.GetValueOrDefault(middle, 0);
 56
 1257            if (leftCount != 0)
 658            {
 659                long rightCount = rightNumToCountDictionary.GetValueOrDefault(middle, 0);
 60
 661                if (rightCount != 0)
 462                {
 463                    result = (result + (leftCount * rightCount % Modulo)) % Modulo;
 464                }
 665            }
 66
 1267            if (leftNumToCountDictionary.TryAdd(num, 1))
 768            {
 769                continue;
 70            }
 71
 572            leftNumToCountDictionary[num]++;
 573        }
 74
 375        return (int)result;
 376    }
 77}

Methods/Properties

SpecialTriplets(System.Int32[])