Skip to content

Files

Latest commit

6a41a06 · Oct 12, 2022

History

History
34 lines (25 loc) · 901 Bytes

File metadata and controls

34 lines (25 loc) · 901 Bytes

Mindtree

Problem 1

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

vector<int> twoSum(vector<int>& nums, int target) {
    int N = nums.size();
    unordered_map<int, int> M;
    
    for(int i = 0; i < N; i++) {
        int required = target - nums[i];
        if(M.count(required)) {
            return {i, M[required]};
        }
            
        M[nums[i]] = i;
    }
    return {-1, -1};
}
About Me
  • Full Stack Web Developer
  • Competitive Programmer

kiranpalsingh