1. Two Sum

1. Two Sum

function twoSum(nums: number[], target: number): number[] {

    // Created a hash map to store the value, index pairs
    let hash = new Map()

    // Iterating over the length of nums
    for(let i = 0; i < nums.length ; i++){

        // Checking if the value already present in the hashmap
        if(hash.has(nums[i])){

            // If the value is already present in the hashmap then we will return the
            // current index and the index which was set to the hashmap of the respective
            // current value
            return [i, hash.get(nums[i])]
        }

        // Calculated the value of x and then set the key, value pair in the form of value
        // index
        const x = target - nums[i]
        hash.set(x, i)
    }

    return
}        

Time Complexity: The time complexity of the twoSum function is O(n), where n is the number of elements in the nums array. This complexity arises from the single pass through the array to iterate over its elements. Within each iteration, the function performs constant-time operations such as hash map lookups and arithmetic calculations. Therefore, the overall time complexity is linear with respect to the size of the input array.

Space Complexity: The space complexity of the twoSum function is O(n), where n is the number of elements in the nums array. This complexity is determined by the usage of a hash map (hash) to store value-index pairs. In the worst-case scenario, where no pair of elements sum up to the target, the hash map will store all elements of the array along with their corresponding indices. Additionally, the function uses a constant amount of extra space for variables like i, x, and for the returned array containing the indices.

Overall, the twoSum function provides an efficient solution to the two-sum problem with linear time complexity and linear space complexity, making it suitable for handling large input arrays.

要查看或添加评论,请登录

Raghav Saini的更多文章

  • 15. 3Sum

    15. 3Sum

    Time Complexity Sorting: The initial sort operation has a time complexity of O(nlogn), where n is the number of…

  • 238. Product of Array Except Self

    238. Product of Array Except Self

    Time Complexity First loop (Initializing the result array): This loop runs once for each element in the input array…

  • 347. Top K Frequent Elements

    347. Top K Frequent Elements

    Total Time Complexity: The total time complexity is the sum of these parts, which is O(n) for the hash map creation…

  • 49. Group Anagrams

    49. Group Anagrams

    Time Complexity: The new hash function has a time complexity of O(m) for each string since we're just counting the…

  • 242. Valid Anagram

    242. Valid Anagram

    Time Complexity: The time complexity of the function is O(n), where n is the length of the longer input string between…

    2 条评论
  • TypeScript and GraphQL Unleashed!

    TypeScript and GraphQL Unleashed!

    ?? Excited to share insights into the dynamic duo transforming web development! ???? In the ever-evolving world of web…

社区洞察

其他会员也浏览了