Hi, everyone.
So today we're going to solve this question to some.
OK, so what is the problem, Mr. So but honestly, it is very simple.
You will be given in broderie indigent pottery and you will be given a target value.
So what do you have to do?
You have to find two numbers in the area.
And if he will add those two numbers, the sum of those two numbers will basically be close to the target
value.
OK, so in this case, the target will lose nine.
So we have to find to number inside this area.
Houssam, if we will add those two numbers, then we will reach the target value.
OK, so in this case, you can see if you will add two and you will add seven, then seven plus two.
OK, so two plus seven.
That is basically nine.
OK, so what we have to do then we have to return the indexes of these two numbers.
OK, so this is zero.
One, two, three, four, five, six and seven.
So basically my answer basically be it will contain index four and index seven.
OK, so my answer will be for seven.
OK, so I hope you understand the discussion I'm repeating myself.
You have to find number inside the area such that if you will add those two numbers, you will reach
the target value.
OK, so target value will be provided by the user.
OK, so this will be inpart.
The target will lose nine, if you will add two and seven, then you will reach the target value and
what we have to do then we have to return the indexers.
Of the elements of the area now have against all discussion, so the first way, this is the brute force.
Arrogance and naive approach.
So what we will do, we will just erode over the area.
OK, so pick one element.
So what I'm trying to say here is pick one element.
I am picking 11.
OK, what is the target value?
So target value is basically nine.
Now, what we have to find, we have to search for minus two inside the three.
OK, so this new approach says pick one element.
And find the other one, pick one element, let's say this element is the nail that is a let's say this
element is Alfy.
And then it is after picking that one element search for that element in the area.
OK, so the search for that element inside the heading.
And what is the other element so that element does nothing but target.
Minus Afie.
So in this case, if I will pick 11, then target is basically nine minus 11, so basically we have
to find minus two inside the area.
So what I will do, I will I dread what Daddy and I will try to find.
Minus two, obviously, minus two is not present.
Then what I will do, I will pick 15.
After picking 15, but we have to do I will search the Eddie, so nine minus 15 is basically minus six.
So I will search this area for minus six.
Obviously, minus six is not present.
Then I will pick this element one.
OK, then I will search the area for the element to it because one plus eight is nine, but it is not
present.
Then I will pick element four and then I will search the area for the element five because four plus
five is nine, but five is not present.
OK, then I will pick two.
I will pick two and then I will search daddy, I will search this area for the element seven because
two plus seven is nine and seven is present, OK, and we will get our answer.
So I will return the index of two and I will return the index of seven.
So that's how I will be able to solve this question.
OK, so the approach is very simple.
You just have to pick one element and then you have to search for that element.
OK, so what it will look like so you will have for loop, you will have two loops.
OK, so one loop is for picking the element.
So this will start from zero and lattices iList and end and I placeless.
And what do you have to do.
So pick one element and after picking.
So this outer loop is basically picking one element.
This is for picking element and then you will have in a loop and what the A loop is.
So basically what is what we have to find for let's say it's a name Miski Suki's nothing but target
value, which will be given by the user minus the current element element that we have picked.
And then you will have one loop.
So this loop, let's say the name of the variable is J.
So this will start from my plus one.
It will go till the end of the array and it will check if the array element, which is if that element
is close to the key or not.
OK, so what is the time and the space complexities.
So time, complexity.
Basically there are two loops.
So the time, complexities and squared and what is the space complexity.
So we are not using any extra space.
So the space complexity is basically big off when.
OK, so this is the brute force approach.
OK, so obviously we can do much better.
How they can do much better.
So what we are doing here is we are picking one element.
We are picking one element, so it will take outdraw and time, and then we are searching the area for
the second element.
So it will also take big oftentime.
So can we optimize this time?
Too big of one.
Can we searching over time in question time?
Yes, we can do the searching.
We can do the searching question with the help of Hashmat.
OK, in strip searching is big often, OK, we can assume in Hashmat searching this basically big Goffman,
so we are picking every element that will take lot of time, but for searching, but for searching inside
the area, we will take constant time.
So basically our time complexity will become all that often and the space complexity since we are using
a hash map so it will become more Draffin.
OK, so we are improving on the time, but we are taking extra space.
But that's not a problem again.
Space is generally not a problem.
Now how we can use hash map.
So let us consider a small example and let's see how we can take the help of Hashmat.
OK, so let's say for seven to nine, eight, five and three.
OK, and what is their target value?
So let's say the target value is, well, OK, let's say the target we lose.
Well, OK, so let's make it thirteen.
This is 13.
OK, so yes, I have nine plus three, which is total.
OK, so let's see what we can how we can use the hash map.
So I will create a hash map.
What is the key and what will be the value so key will be basically element will be element and the
value will basically be the index, because you can see because you can see in the question what we
have to write down.
So we have to return basically the indexes.
OK, we have to return the indexes.
So we have to store index.
So in this case, two plus seven is basically nine, so I have to return the index.
So this is index zero, this is an index one.
So I am returning zero one since I have to return the index in the equation.
So that's why the value will be index.
OK.
Now, how we can use hash map, so let us out right over there and we will prepare our hash map and
simultaneously we will get our answer.
Let's see how.
So I'm standing here and my hash map is empty.
So I'm standing at four.
So what I will do, what is the value?
So I have to search for so basically 12 minus four is basically it.
OK.
Now search it inside the hash map.
So search it inside.
The hash map is empty so you will not be able to find it.
OK then move ahead.
So while moving ahead, push the element inside the hash map so the element will be four and its index
is zero.
Now I'm standing at seven.
Now, what we have to do, so we have to find that element five, OK, so five is already present,
let's change its value to six.
OK, so this is basically six.
So I am standing at five now, I have to find element five now searching the map, so element five is
not there, then move ahead and when you are moving ahead, push the element inside the hash map.
So the element is seven and it's indexes one.
Now you are standing at two so far to my target values, 12 to find then obviously ten is not present
inside the map.
OK, so I am doing searching inside the hash map and is not present.
Then move ahead and push to inside the map.
So element is two and it's a nexus to.
Now, nine, OK, so far, nine would have to do I have to find three, I have to find three inside
the hash map.
So trees not present, then move ahead and push, eliminate.
So I am pushing eliminate and it's indexes three.
So this is element 13, so for element 13, obviously what we have to do, so I will search for minus
one, I will search for minus one inside the Hashmat minus one is not present.
So I will push Tartine and it's a Nexus four and then I will move ahead.
So I will come here.
So this is element six, so four element six, that that element should be six.
OK, six six Qestrel.
So search for six inside the hash map and obviously six is not present.
Then move ahead and push six.
So I have six and it's indexers Nexus five.
OK, then what I will do so I will reach here.
Three, four, three, what I will do, I have to find nine inside the Hashmat find a nine inside the
Hashmat.
So nine is present inside the ship.
And what is the index of nine.
Index of nine is basically three.
And what is the index of this element?
So the index of this element is basically six.
So our answer will be three commas.
OK, so forgetting the element of forgetting the index of Element nine, I am storing the index.
That's why this value has to be index.
OK, so after finding out nine I will get its index, which is three and the index of the current element,
which is six.
So I will prepare my answer three comma six and then I will return my answer.
OK, so one thing to note here is if you want to search inside the hash map, you can only search element.
You cannot search index.
OK, we can only basically hash map this nothing.
Does that give a little bit.
OK, it is a dual appearance.
So if you want to find something inside the hash map, you can only do searching with the help of key.
You can only search key.
You cannot search value.
OK, we cannot search value, we can only search.
OK, so our key will be element and our value will basically be index.
OK, so now let us write the gold.
OK.
So what they have to return, so we have to return that part of integers.
OK, so let's see.
I have a bit of indigestion, sir.
OK.
So now let us take our hash map, so I'm using, I don't know, the map.
So Redmap.
What will the key silkies basically integer and the value will basically be in beta also.
OK, name is Map.
Now, let us just trade our daddy so forint.
Let's change the name.
A OK.
So says.
Now, what we have to do, we have to search for.
So search what we have to search, we have to search Dogood minus Elfy inside the hash map.
OK, now let us search inside the map.
So.
My map that you can use can't function or you can find the function.
OK, so let us use common function.
So.
OK, so let us use fine function, so fine function will take and put Askey again, what is the key
keys such.
So if.
The key is not present, so that will be my daughter and.
So basically, if the key is not present inside the map, then what we have to do, we have to push
the current element inside the map.
So my map.
What is the key is the current element, Elfy, and what is the index indexes, a OK in the part we
have found of a search element inside the map, then what do we have to do?
We have to prepare our answer.
OK, so answer that.
Push back.
We have to push two elements.
We have to push two indexes.
OK, what the first index.
So first index will basically be you can see here.
So the first index is basically the index of nine.
OK, and how we can obtain index three.
It is nothing but my map of nine.
Minimum of nine is basically three.
OK.
So this is nothing but my map of nine and what is nine nine basically is is the search element search.
And let us push the current index.
So what is the current index?
It is a.
And.
We can it out on.
OK, so this is the computer, let us in our court and then we will try to submit our gold.
OK, so what is the problem here?
OK, so what we have to do for the completion purposes.
See the return type of dysfunction, what does that entail?
This is a bit of deja vu.
OK, so for the compilation purpose, I have to return answer here also.
OK, so I did a mistake here, basically.
Court should be in the effort and basically let's make it.
And OK, so if we are not able to find the search element, then I will push and if we are able to find,
then I will prepare my answer.
OK.
OK, so our goal is working fine on let's try to some real.
OK, so our goal is working fine, OK?
Our goal is hundred percent correct.
Now let's see how our goal is working.
Let us try another gold for this and put two, seven, 11 and 15.
OK, so first thing that we get out of it, nettelhorst you do because I have to return a vector of
integer.
OK, so basically if we will not rate the return statement here, you will get the compilation error
and the compiler will be basically due redo because we have to return integer and we were not returning
anything, although this line will never get executed.
OK, we will always be able to find our answer if there exist one lucky Artus that this input.
So this is my map, which is empty.
OK, so my map is initially empty now.
Well, we are retreating the.
So first element is basically to OK, so target minus two.
So Target is basically nine, so nine minus two, which is basically seven.
And then what we are doing, I'm using defined function.
So fine function is the inbuilt function and fine function will take input ascii.
OK, I told you we can only search, we can not search value, we cannot search value.
I can only search with the help of key.
OK, so I'm searching ke.
And gives basically seven.
OK, so, so lucky seven inside the map, and obviously you will not be able to find seven because the
map is empty.
So what will happen?
You will reach the end point.
OK, now, look, suppose this is a map.
And I suppose there are some countries, OK, let's say two, three and five, you are finding seven.
So seven is not pleasant inside the map.
So this is the end point.
OK, this is not and this is not.
And this is the end of the map.
So I don't know what will happen.
You will search seven and finally you will reach the end of the map.
So if you will reach the end of the map equals equals my map.
Norton, if you will reach the end, that means seven is not present.
So if the seven is not present, what I am doing, I'm inserting element and its index.
OK, so my map will look like this.
I am inserting the element, Afie, which is to and I'm inserting its index, which is zero.
OK, so comma zero.
Now move ahead, so I will come to the Element seven.
I will come to the element seven now for Element seven, which does not target value.
What is my social value?
So I have to search for two.
OK, so nine minus seven is to target, minus seven is to search to inside the map my map, not find
two.
So if you will find if you will search for two.
Yes, to his present.
So basically, this condition is false.
OK, you will not reach the end.
This is the end, so you will not reach and if you will not reach and you will prepare your answer,
answer, not push back.
So I am pushing the element so I don't push back my map of search, so my map off, too.
So if you will right my map of two, then you will get the answer to zero, which is the index.
OK, so I am writing my map of search, which is basically zero, and I will push the element zero.
Then I am pushing the current index, which is one.
So I am pushing one so zero one and then I am returning my answer.
So that's how this code is working.
OK.
OK, so this is basically the key Mitchellville integer.
This is the value which will also be integer.
Then I have to search for the element search.
If the element you are trying to find is not present inside the map, then you will reach the end of
the map, OK, if the element you want to search is not present inside the map, then you will reach
the end of the map.
If you are reaching the end of the map, that does not present.
That means push the current element.
OK, so I am pushing the current element.
And if you are not reaching map dot, and that means the element is present, OK, if you are not reaching
the end, that means the target, the search value is present.
OK, so the search value is present in the search values present, simply prepared to answer and return.
OK, so my search, it will have done me the index and this is the current index and then I'm returning
my answer.
OK, so as discussed the time complexity.
So what is the time and the space complexity.
So what is the time complexity.
So assuming there are assuming they look up in the hash map is big off once all the time, complexities
big often and the space complexities big often for the hash map.
OK.
So searching in my place over time, OK, we can resume searching, takes over and bamburgh of one time.
OK, so this is the time in the space complex waiting for this solution.
OK, so if you have any doubt in this question, you can ask me.
Thank you.