3583. Count Special Triplets
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Problem Overview
Count Special Triplets (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Math pattern in coding interviews. Prefix and suffix maps; for each middle element count left*right where nums[l]*nums[r]=nums[m]².
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Prefix and suffix maps; for each middle element count left*right where nums[l]*nums[r]=nums[m]².
3583.cs
C#
// Approach: Prefix and suffix maps; for each middle element count left*right where nums[l]*nums[r]=nums[m]².
// Time: O(n) Space: O(n)
public class Solution
{
public int SpecialTriplets(int[] nums)
{
// Dictionary to count occurrences of elements to the left of current position
Dictionary<int, int> leftCounts = new Dictionary<int, int>();
// Dictionary to count occurrences of elements to the right of current position
// Initially contains all elements
Dictionary<int, int> rightCounts = new Dictionary<int, int>();
// Initialize rightCounts with all elements from the array
foreach (int num in nums)
{
if (rightCounts.ContainsKey(num))
rightCounts[num]++;
else
rightCounts[num] = 1;
}
// Variable to store the total count of special triplets
long totalCount = 0;
// Modulo value for preventing integer overflow
const int MOD = (int)1e9 + 7;
// Iterate through each element as the potential middle element of a triplet
foreach (int currentNum in nums)
{
// Remove current element from right counts (moving it from right to current position)
if (rightCounts.ContainsKey(currentNum))
{
rightCounts[currentNum]--;
if (rightCounts[currentNum] == 0)
rightCounts.Remove(currentNum);
}
// Calculate the target value that should appear on both sides
// For this to be a special triplet, we need elements equal to 2 * currentNum
int targetValue = currentNum * 2;
// Count triplets where left and right elements both equal targetValue
// and current element is in the middle
long leftCount = leftCounts.ContainsKey(targetValue) ? leftCounts[targetValue] : 0;
long rightCount = rightCounts.ContainsKey(targetValue) ? rightCounts[targetValue] : 0;
long tripletCount = (leftCount * rightCount) % MOD;
// Add the count of triplets with current element as middle
totalCount = (totalCount + tripletCount) % MOD;
// Add current element to left counts (moving it from current position to left)
if (leftCounts.ContainsKey(currentNum))
leftCounts[currentNum]++;
else
leftCounts[currentNum] = 1;
}
// Return the final count as an integer
return (int)totalCount;
}
}Was this solution helpful?
Related Problems
- 3623. Count Number of Trapezoids I(Unknown)
- 3625. Count Number of Trapezoids II(Unknown)
- 66. Plus One(Easy)
- 179. Largest Number(Medium)
- 189. Rotate Array(Medium)
- 368. Largest Divisible Subset(Medium)