Hi, everyone.
So today we are going to solve this question to some part two.
OK, now this is exactly the same as the previous question.
So the only difference is basically the input that is sorted.
OK.
And potteries basically sorted, given our target value, what they have to do, you have to find two
numbers inside the area such that if you add those two numbers, you can obtain the target value.
OK, the only difference between this question and the previous question is basically the array will
be sorted.
OK, that is sorted.
OK, so basically all the solution that we discussed in the previous problem are valid here, ok.
For example, the brute force solution that we discussed pick one element and search for another element.
That solution is valid here.
The second approach, the second approach we used map.
So that solution is also valid here.
OK, so basically these solutions does not care about the array.
Whether that is sorted or not, the solution will work.
OK, so the solutions are definitely valid, but here we have to use the property of the area and what
is the property?
So the property is basically the given at is sorted.
Now, how we can use.
This property, to solve our question, OK, so how we can use this property, so basically the first
way to solve this question is what are you going to do is you pick one element.
OK, pick one element.
And then we have to search for another element.
OK, we have to search for that element.
But since the arrays sorted.
We can use the binary search for searching the other element.
OK, so for example, if you are standing at one, the target value 16, you have to search for 15 and
for searching 15, we can use binary search.
OK, so whatever the time, complexity of using this approach to the time, complexity is basically
in the worst case, what will happen?
We have to pick every element.
So a loop, basically a loop for picking every element.
So and multiply, we have to search for other elements.
So this is logoff in certain complexities and log-in and the space complexities.
Big of one.
OK, so this is the first approach.
Now, let us discuss the second approach.
The second approach, basically, this is called 2.0 approach.
OK, so what is our 2.0 approach?
So 2.0 approach says you take two pointers when at the start.
When at the end.
So I am taking two pointers.
So this is we have to take two point one at the start.
One at the end.
OK, then you will add the two as you will start placing, so they will be three conditions.
So first condition is when you will start plus and it will become close to the target value.
This is the first scenario, if this is the case, we can simply return our answer.
We can simply create a vector and we can return a start command simple.
Now, the second condition can be, if you will add the two elements, Starplex, and it can be greater
than the target value.
OK, so if basically you start plus end is getting the target value, our current numbers are greater
than the target value.
That means I have to decrease this part, OK?
This is bigger than the target value.
I have to decrease this part.
So for decreasing this part, I can do and minus minus.
OK, because let's say this is and here, if you will move in the direction, what will happen that
is sorted.
So basically the value of and will decrease the value of and will decrease.
And obviously this whole part will decrease.
OK, so will do.
And minus minus.
Now that third condition can be when you are adding these two elements.
This is less than the target element.
So if this is the scenario, what you can do, so basically after adding these two elements.
We are less than the target element, so in this case, I can write a start plus plus basically if I
will move ahead then I will start.
Value will increase and and overall, this value will increase and we will be able to reach condition
number one.
OK, so these are the very three simple cases.
If I start and is bigger, then decrease by doing and minus minus if I start, placenta's smaller,
then do start placeless to make this value bigger.
OK, now let us implement these approaches on these examples.
OK, so this is my start point.
This is my end point.
OK, so one plus forty five, this is 46 so I will do and minus minus.
So this is my new end.
OK, remove from here.
So one plus 10.
This is 11, so 11 is less than 16.
So I will start placeless.
So we will start from here but start here for blastin.
So this is 14, so 14 is smaller.
So I will move ahead.
So this is my new start.
OK, six plus 10.
This is 16.
OK, so six plus 10.
This is 16.
So I will return start comma and I will create a vector and I will start command.
OK, now let us implement this Approach 2.0 approach.
Let us implement 2.0 approach on this example.
OK, so this is my start point, this is my end point, given that is sorted, basically.
OK, so one plus six, this is seven, so I have to move ahead to seven is less than 10.
So this is my start to play six, eight.
So it is less than 10.
So I have to move ahead.
Three, six, nine, so nine is less than 10, so move ahead.
Six plus four, so 10 equals then, so we have reached our answer and I will start.
And very simple now let us implement this 2.0 approach on this example.
OK, so this is a start and this is end.
OK, so two plus 15.
So this is 17 and 17 is greater than nine.
So I will do.
And minus minus.
I have two degrees.
OK, so two plus 11, so we learned last week, basically tardiness.
So today is the deadline, so move backward.
So I will remove and from here and I will put and here now seven plus two.
So this is nine and nine equals nine.
So we got our answer and I will then start command.
OK, very simple.
So why this 2.0 approach is working.
OK, so why this is working.
This is only working because the given that is sorted every.
Since the governor is ordinary, then only this hour, these three conditions will work, then only
this 2.0 approach will work only when the governor is assaulted every.
OK, now what will be the time and space complexity?
So basically what we are doing, we are simply iterating over the area.
So the time, complexity of the two pointer approaches big off and and the space complexity, we are
just creating few variables.
So this is the time and the space complexity of the Bhupinder approach.
So you can see yourself by just taking and longer time, but this 2.0 approach is taking big oftentime.
OK, so this 2.0 approach is the best approach.
OK, this is the best approach.
Now, let us write the code, but before writing the code, see this example here, so two plus seven.
It's basically 9:00, so our answer should be zero when we have to return the index, but we have to
return one comma to OK, so we have to add plus one.
We have to return one comma, too, because in the end it says you have to return one indexed based,
OK.
So generally, our area is basically zero indexed based on what it is indexed based, but the question
now is to return the indexes.
When indexed, based, basically, OK, so after calculating the answer.
So our answer is basically zero common after calculating the answer, we will add plus one.
So I will return one to basically the indexing is basically starting from one.
OK, so if this is the question, so normally that is indexed based.
So this is zero, one, two, three.
But the problem is, is that there is one index to be.
So this is one, two, three, four.
So that's why I will return one Clamato instead of returning Zuckermann.
OK.
Now, that's right, the.
So writing code is very simple.
So first, let us create.
I would answer now let us use to point that approach for two point, that approach you need to point
out.
So the first point I start and you have another point, but let's name this area.
So this another point that is a nazis' minus one.
OK.
Annotates my one now to the dropping idea, so we'll start this list and this will be the strobing criteria
now that three situations so basically start plus and.
It goes to the target value.
So if this is the scenario, what they can do, I Sadaat push back.
Start plus one, and similarly, Sadaat, push back and plus one and then you can your answer.
OK, the second situation can be.
If the start plus end.
It's basically less than their target value, so if it is less than the target value, you have to do
start placeless so that this value can be increased.
OK, so that this will look an increased Starplex because in the Right-Hand side, all the elements
will be increasing elements.
OK, all the elements will be bigger than the current element.
And in the end, smart, who can write and minus minus.
OK, very simple.
And finally, for the purpose.
I will return on set here also, OK, for the completion purposes, because I have to return that property
just so I have to return.
I said here I have to return this line for the completion purposes.
OK, now let's bring our code and then we will try to submit our code.
OK, so our court is looking now at some.
OK, so our goal is 100 percent correct, so as discussed, the time complexity is big often and the
space complexities big often.
OK, so this approach is called 2.0 approach.
To point out approach, and this approach is working because the given that is sorted out.
OK, so if you have any doubt in the logic or in the court, you can ask me, OK, thank you.