Skip to content
Discussion options

You must be logged in to vote

We could also use prefix and suffix arrays to store counts of each value at each position, but that would require O(n * max_val) space which is inefficient for large values. The hash table approach is more memory efficient for sparse value distributions.

Approach:

  • Frequency Tracking: Use two frequency maps - one for elements before current index (leftFreq) and one for elements after current index (rightFreq).
  • Iterate with Middle Element: Treat each element at index j as the middle element of potential triplets.
  • Dynamic Updates: As we move j from left to right:
    • Remove current element from rightFreq (since it's no longer on the right)
    • Calculate target value as 2 * nums[j]
    • Count valid i po…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Dec 9, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Dec 9, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants