• 2 days ago
Transcript
00:00Sorted Array List, I will use the two-pointer approach which I have seen in the previous question.
00:04We are going to use the same two-pointer approach.
00:07I will choose two pointers.
00:09One pointer will start from the beginning or zero.
00:12One pointer will start from last or n-1.
00:16I will analyze every point that my pointer is 1
00:21or my left pointer's value
00:25plus my right pointer's value.
00:28I can say that my left pointer's number stored
00:34plus my right pointer's number stored.
00:39So my left pointer is 0.
00:41My left pointer is an index which is 0.
00:44But the point is 1, so 1 is the number.
00:48Right pointer is 6, so 6 is the number.
00:51So 1 plus 6 value, if target is equal,
00:56then what did I get?
00:57Then I got a pair sum.
00:59What do I want to do in that case?
01:01I will return true.
01:03This is my case 1.
01:05Case 1 means, when my left pointer and right pointer
01:08stored value, the sum of both,
01:11that becomes equal to my target,
01:14I will return true.
01:15What do I want to do in that case 2?
01:17Case 2 comes when the sum is less than target.
01:23If my left pointer's sum and right pointer's sum is less than target,
01:27then what do I want to do in that case?
01:30I want to do in that case 2.
01:31If I want the number of left pointer
01:34plus right pointer's number,
01:39the sum of the sum of the sum becomes less than target.
01:43What do I want to do in that case?
01:45I want to do in that case 2.
01:47so, I want to add 1 to 1.
01:49No, then case 3 will come,
01:52if I want the number of left pointer
01:55plus right pointer
01:57and the number of left pointer
01:58this is my case.
02:01If it is greater than target,
02:04then what do I want to do in that case?
02:05I want to minus the right pointer.
02:07This situation and this situation
02:10are very similar to our last question,
02:12which was the container with most water.
02:13What happened here?
02:14What happened here? If this value is small then increase the left pointer. If this value is bigger then increase
02:19the right pointer.
02:21The logic behind it is that I can say that the left pointer and the right pointer have a sum.
02:27Now I know that if my value is small, the right pointer and the left pointer have a small target.
02:36Let's say that my target is equal to 8. If my target is equal to 8 then I can say
02:43that if I take my left pointer to the right pointer,
02:46then it won't be a problem. Why? Because my left pointer is 1 stored and my right pointer is 6
02:54stored.
02:55Left pointer is 1 and right pointer is 6. What is the sum of both? I have 7 equal to
03:017.
03:017 is less than my target, meaning less than 8.
03:05Now what do I have to do? If I can increase the right pointer, if I take a right pointer,
03:11then my sum will increase. This sum will not increase. Why?
03:14Because if the right pointer comes back, then the value is less.
03:18Because it is a sorted array list. In the sorted array list, if the right pointer is minus,
03:23then the total value will go down. Here it was 6. If I have 5, then it will be 1
03:29plus 5 which is equal to 6.
03:316 is less than 7. Then I will not have a solution.
03:35In this case, what do I have to do? I have to take a left pointer.
03:39If the left pointer is higher, then I will go to the other numbers.
03:43If my total sum is lower, then I have to go to the other numbers.
03:47If my total sum is lower, then I will go to the other numbers.
03:50Because my total sum is lower, then I will try to go to the other numbers.
03:55Which number will go to the other numbers?
03:56The left pointer will go to the other numbers.
03:58So the left pointer will go to the other 1.
04:00And now the value will go to the other numbers.
04:02And now who will point to the left pointer?
04:052.
04:06Who will point to the right pointer?
04:086.
04:08What's the sum of both?
04:098.
04:10This is equal to my target.
04:12So what happened? I have a right answer.
04:14Why is the right answer?
04:15Because if I have a combined sum is less,
04:18then I will go to the other numbers.
04:19And who will take the other numbers?
04:21Left pointer.
04:22Now let's consider another case.
04:23Let's consider this.
04:25Let's consider this.
04:28My target is equal to 4.
04:34If the target is equal to 4,
04:36and the left pointer is here,
04:37and the right pointer is here.
04:39What will the sum of both?
04:40The sum will be equal to 7.
04:437 is greater than 4.
04:46So I need 4.
04:48I don't need 7.
04:49So what I want to do?
04:50I want to reduce the sum.
04:51How will the sum be less?
04:52If I increase the left pointer,
04:54then I will go to the other numbers.
04:56So the sum of 2 plus 6 will be 8.
04:59What will I do?
04:59I want to reduce the sum.
05:00How will the sum be less?
05:02When I take the right pointer
05:02to the other numbers.
05:04So the right pointer will come here.
05:06What will the sum come here?
05:09The new sum will be equal to 1 plus 5.
05:13That is equal to 6.
05:156 is greater than 4.
05:16So what will I do?
05:17I will take the right pointer again.
05:19The right pointer will come to 4.
05:21Now what will the sum be?
05:23The sum will be equal to 1 plus 4 is equal to 5.
05:26So what can I say?
05:275 is still greater than 4.
05:29So I will take the right pointer again.
05:31Now my right pointer is here.
05:33What will the sum be?
05:35The sum will be equal to 1 plus 3.
05:37Which is equal to 4.
05:38Now the value is equal to target.
05:41What will I do here?
05:42What will I do here?
05:43What will I do here?
05:44I will take the right pointer again.
05:46Until I do not reach the right pointer again.
05:48Until I do not reach the sum.
05:49And if my sum does not exist,
05:52I will take the right pointer again.
05:54I will take the right pointer again.
05:55So what will I do here?
05:57It will go back again.
05:58Now what can I do here?
05:59What can I do here?
06:00I cannot go back again.
06:01Because I have seen all possible sums here.
06:03I have seen it here.
06:04This one is single element.
06:05This one is equal to 1.
06:06Never the sum of pair is equal to 1.
06:09The sum of pair will always increase from 1.
06:11Because the smallest element is 1.
06:13You will add something to this.
06:14Then there will be a big answer.
06:16If there are negative numbers,
06:18it will be possible.
06:19But because there are negative numbers,
06:20the target will not be equal to 1.
06:23So what did I get learning from this?
06:25I got this learning from the question.
06:28I have proved that if my combined sum,
06:32this is my sum.
06:34If the sum of value is less than target,
06:36then I will increase the left pointer.
06:37If my sum of value is greater than target,
06:40then I will take the right pointer
06:41and take the right pointer.
06:44So I will use these three cases.
06:46I will write my code.
06:47How will the structure of the code be?
06:49The structure of the code is like that
06:50I will initialize the left pointer
06:51with the right pointer
06:53with the right pointer
06:53with the right pointer
06:54with the n-1.
06:55When will my loop go?
06:56My loop will go until
06:57until the right pointer is not equal to
06:59left pointer is not equal to
07:01my right pointer.
07:04Or I can also write this here
07:05that the left pointer is less than
07:07my right pointer.
07:08That means when the value of both
07:09is equal,
07:09then we will leave it.
07:11So here is case 1
07:12that the target is equal.
07:14This is my case 1.
07:15In case 1,
07:16I have to know what I want to do.
07:17In case 1,
07:18I have to return true.
07:19The other case will come
07:21in which the sum of both
07:22is less than target.
07:24In that case,
07:25I have to update the left pointer.
07:27The third case will come
07:28in which the sum is greater than target.
07:30In that case,
07:30I have to update the right pointer.
07:32So I will update the values
07:35and in the last case,
07:35I will return false.
07:38If I have not found the sum here,
07:40then the case 1 is not true.
07:42In that case,
07:43I have to return false.
07:44In that case,
07:44I have to return false.
07:46This approach is
07:47that I had discussed before.
07:48If the left pointer
07:49covers d area,
07:50d distance.
07:51If the right pointer covers
07:52n-d distance.
07:53Before meeting,
07:54what will happen?
07:55The total distance will be equal to n.
07:58So this is my two pointer approach.
08:01It will give me linear time complexity
08:02which is big of n.
08:04Now let's write code.
08:05In that case,
08:06my pair sum function
08:08I will do comment out.
08:10And I will write a new function
08:11based on two pointer approach.
08:16Public static int
08:19or return type boolean
08:22pair sum 1
08:24Now I will copy the parameters
08:26I will copy the parameters
08:28which is my list and my target.
08:32What will I do?
08:33First of all,
08:35I will make my left pointer
08:36from 0
08:37and my right pointer
08:39from list.size-1
08:45Until my left pointer
08:47is not equal to my right pointer
08:51What do I have to do?
08:52First of all,
08:53my case 1
08:54Case 1
08:57says
08:58that if
08:59list.get
09:02left pointer
09:04plus
09:06list.get
09:07right pointer
09:09is equal to my target
09:11In that case,
09:13I will return true
09:14Case 2
09:16says
09:18true return
09:20list
09:20or
09:21I will copy the same thing
09:22here
09:25If
09:27the sum of these two
09:28is less than my target
09:28than my target
09:31then
09:31In that case,
09:32I will make
09:32left pointer
09:33update
09:35Not
09:35then
09:37this is my case 3
09:39If there is no case
09:41then
09:41write pointer
09:42update
09:42So I have satisfied
09:43three cases
09:45and
09:45there is no true return
09:47so I will return
09:48false
09:48meaning my sum
09:49doesn't exist
09:50I will save
09:52and run
09:54So now
09:55the value
09:55that is equal
09:57to 50
09:57I have printed
09:58for 50
09:59I have printed
09:59this value
10:00again
10:01so I will print
10:03true
10:04So my two pointer
10:06approach works
10:06with my
10:07pairsum code
10:09In that case
10:09I will use
10:09this
10:09tool
10:09.
10:09If