0% found this document useful (0 votes)
92 views5 pages

Two Sum. (Leetcode Easy Problem) - by Sukanya Bharati - Nerd For Tech - Medium

This document summarizes the Two Sum problem from Leetcode and provides two approaches to solve it. The problem involves taking an array of integers and a target number, and returning the indices of the two numbers in the array that add up to the target. The first approach uses a brute force method with two nested for loops. The second approach uses a hashmap to store the numbers and their indices, looking up the complement target - number as it iterates once through the array.

Uploaded by

23Pratyasha Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views5 pages

Two Sum. (Leetcode Easy Problem) - by Sukanya Bharati - Nerd For Tech - Medium

This document summarizes the Two Sum problem from Leetcode and provides two approaches to solve it. The problem involves taking an array of integers and a target number, and returning the indices of the two numbers in the array that add up to the target. The first approach uses a brute force method with two nested for loops. The second approach uses a hashmap to store the numbers and their indices, looking up the complement target - number as it iterates once through the array.

Uploaded by

23Pratyasha Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Open in app Get started

Sign in to Medium with Google

Published in Nerd For Tech


23_Pratyasha Kar
[email protected]

Sukanya Bharati Follow PRATYASHA KAR


[email protected]
Sep 22, 2021 · 2 min read · Listen

Save

1. Two Sum
(Leetcode easy problem)

Given an array of integers nums and an integer target , return indices of


the two numbers such that they add up to target .

77
You may assume that each input would have exactly one solution, and
Open in app Get started
you may not use the same element twice.
Sign in to Medium with Google

You can return the answer in any order.


23_Pratyasha Kar
[email protected]
Example 1:
PRATYASHA KAR
[email protected]

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Output: Because nums[0] + nums[1] == 9, we return [0,


1].

Example 2:

Input: nums = [3,2,4], target = 6

Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6

Output: [0,1]

Approach :

Now I will share two approaches to solving this question :

1. Brute Force Method

In this approach I will iterate the array using two


loops to find if the sum is equal to the target.
for(int i=0;i<nums.size()-1;i++)

{
Open in app Get started
for(int j = i+1;j<nums.size();j++)

Sign in to Medium with Google


{

if(nums[i]+nums[j]==target)

...
23_Pratyasha Kar
}
[email protected]
}
PRATYASHA KAR
Time Complexity - O(n*n)
[email protected]
Space Complexity - O(1)

2. Using Hashmap

In this approach I will use hashmap and a vector to


return the index of the elements if the target is found
by addition of two elements.

Time Complexity - O(n) because I will tarverse the array


only once and Hasmap has a time compelexity of O(1) for
insertion.

Space Complexity - O(1)

Code is as follows :

class Solution {

public:

vector<int> twoSum(vector<int>& nums, int target) {

vector<int>ans;

unordered_map<int,int>m;

for(int i=0;i<nums.size();i++)

int val = target-nums[i];

if(m.find(val)!=m.end()) // in case the


second element is found

ans.push_back(m.find(val)->second);

ans.push_back(i);

break;

m.insert(pair<int,int>(nums[i],i)); // in
case the above criteria is not satisfied I will keep
inserting the element in the hashmap Open
in app Get started
}
Sign in to Medium with Google
return ans;

}; 23_Pratyasha Kar
[email protected]

PRATYASHA KAR
[email protected]

Hope this helps! Keep coding!

Since you enjoyed reading my blog , why not buy me a coffee and
supoort my work here!!
https://www.buymeacoffee.com/sukanyabharati ☕

Enjoy the read? Reward the writer.Beta


Your tip will go to Sukanya Bharati through a third-party platform of their choice, letting them know
you appreciate their story.
Open in app Get started
Give a tip Sign in to Medium with Google

23_Pratyasha Kar
[email protected]

PRATYASHA KAR
Sign up for NFT Weekly Digest
[email protected]
By Nerd For Tech

Subscribe to our weekly News Letter to receive top stories from the Industry Professionals around
the world Take a look.

Your email

We couldn't process your request. Try again, or contact our


support team.

Get this newsletter

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more
information about our privacy practices.

About Help Terms Privacy

Get the Medium app

You might also like