algorithmtime-complexitycomplement

What's the relationship between time complexity and complement?


What's the relationship between time complexity and complement?

I do not understand what complement means to be used.

I do not understand the sentence below.

Approach #2 (Two-pass Hash Table) [Accepted]

To improve our run time complexity, we need a more efficient way to check if the complement exists in the array. If the complement exists, we need to look up its index. What is the best way to maintain a mapping of each element in the array to its index? A hash table.

https://leetcode.com/problems/two-sum/solution/


Solution

  • Complement refers to the other number that when added to the current number will give you the value of the target.

    If (for all) a + b = target, then a's complement is b.

    In order to see if a complement of a number is there, instead of looping through the array (which is O(n)), they are storing it (the elements in the source array) is a hash map.