| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.CountSpecialTriplets; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 3 | 26 | | { |
| | 3 | 27 | | long result = 0; |
| | | 28 | | |
| | 3 | 29 | | var numsLength = nums.Length; |
| | | 30 | | |
| | 3 | 31 | | var rightNumToCountDictionary = new Dictionary<int, int>(); |
| | | 32 | | |
| | 30 | 33 | | for (var i = 0; i < nums.Length; i++) |
| | 12 | 34 | | { |
| | 12 | 35 | | var num = nums[i]; |
| | | 36 | | |
| | 12 | 37 | | if (rightNumToCountDictionary.TryAdd(num, 1)) |
| | 7 | 38 | | { |
| | 7 | 39 | | continue; |
| | | 40 | | } |
| | | 41 | | |
| | 5 | 42 | | rightNumToCountDictionary[num]++; |
| | 5 | 43 | | } |
| | | 44 | | |
| | 3 | 45 | | var leftNumToCountDictionary = new Dictionary<int, int>(); |
| | | 46 | | |
| | 30 | 47 | | for (var j = 0; j < numsLength; j++) |
| | 12 | 48 | | { |
| | 12 | 49 | | var num = nums[j]; |
| | | 50 | | |
| | 12 | 51 | | rightNumToCountDictionary[num]--; |
| | | 52 | | |
| | 12 | 53 | | var middle = num * 2; |
| | | 54 | | |
| | 12 | 55 | | long leftCount = leftNumToCountDictionary.GetValueOrDefault(middle, 0); |
| | | 56 | | |
| | 12 | 57 | | if (leftCount != 0) |
| | 6 | 58 | | { |
| | 6 | 59 | | long rightCount = rightNumToCountDictionary.GetValueOrDefault(middle, 0); |
| | | 60 | | |
| | 6 | 61 | | if (rightCount != 0) |
| | 4 | 62 | | { |
| | 4 | 63 | | result = (result + (leftCount * rightCount % Modulo)) % Modulo; |
| | 4 | 64 | | } |
| | 6 | 65 | | } |
| | | 66 | | |
| | 12 | 67 | | if (leftNumToCountDictionary.TryAdd(num, 1)) |
| | 7 | 68 | | { |
| | 7 | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 5 | 72 | | leftNumToCountDictionary[num]++; |
| | 5 | 73 | | } |
| | | 74 | | |
| | 3 | 75 | | return (int)result; |
| | 3 | 76 | | } |
| | | 77 | | } |