Hi everyone, I hope you have tried to code the brute force solution which we discussed in the previous
video on your own.
Now in this video, let's try to code it out together.
Now first let's create a function and let me call this function find indices some.
All right.
So this is find indices some.
And as mentioned in the question the input to the function is an array and the target value.
Now let's have a for loop so that we can iterate through the array.
So let's use a for loop for that.
For let I equal to zero I less than array dot length.
Minus one because I need two values, right?
So let me just show that over here.
So let's say our sample array.
Let me have a sample array over here which is like 12345.
Now let's say we are trying to find the target value.
Let let it be six.
So we are trying to find two values right.
So the I which is the first for loop over here needs only to go up to the previous element, up to the
element previous to the last element in the array.
Right.
Because I will be searching matches for a particular.
I value the value in the array at an ith index with something that is coming after it, right?
So I just need to check I up to I less than array dot length minus one.
All right.
Now let's increment I now.
I plus plus.
Now inside this let's have a for loop again because this is the brute force solution right.
So let's have j let j equal to I plus one.
So I won't be checking for every element right.
If I is equal to one I will check for j values for all the other values which are coming after I.
So when I is equal to one, let me show that over here, if n I is equal to one, I will try j values
two, three, four and five.
Now in the next iteration, when I is equal to two, I will try for j values 345, right?
So I won't check values which are coming before two, right.
Because the value before two is one.
And I, in whatever combination is possible with one, is already checked in the first iteration over
here.
So I don't need to check that over here.
Right.
So that's why we are initializing I as J as I plus one over here.
And this will go on till j less than array dot length.
Now in the previous case we had array dot length minus one.
Because as we saw we will go for I values only up to four over here.
So I can take one I can take the value two.
I can take the value three in the next iteration and four in the next iteration.
And we'll stop over there.
But when it comes to J we will go till the end.
So j less than array dot length.
And let's increment j j plus plus.
Now inside this we are going to check if the value at the ith and jth index of the given array will
add up to the target value.
So that's what we are going to check.
So if target value.
Equal to.
Array at the ith index.
So value at the ith index plus array j.
So the value of the given array at the jth index.
Now if this is true then we have found the answer.
So we just need to return I and j.
So let's return it as an array.
Now, if this is not true, then as per the question, we just need to return an empty array.
And that would happen over here.
Right.
So this is outside the for loop.
So this is outside the uh initial the first for loop.
So when we are coming out of it and if we have not yet returned anything till this point, it means
that we were not able to find a combination of two elements in the array, which adds up to the target
value.
And we are done.
Now let's write a test case to check whether this algorithm is working.
So let's take an example.
Let's say array is equal to 12345.
And let's say the target value is equal to let's say six.
All right.
So one plus five should give us six.
Now let's console log what we get when we call the function console dot log.
Find indices sum and we'll pass the array.
And the target value.
And let's see what we are going to get.
Now I'm going to run this.
And you can see that we are getting two zero comma four, right.
This is zero.
So that's the first element that which is one.
And then the element at the fourth index that's 01234 which is five.
So one plus five is equal to six.
So yes, our, uh code is working.
Now let's try a few more cases.
What will happen if the input array is empty?
Let's try that.
So when that's happening, we are getting an empty array as the output.
So that's correct as per the question.
Now let's say we have uh three values.
But the no combination adds up to the given target value.
So what happens again we are getting an empty array.
And what happens if I have only one element in the given array.
And the target value is also ten.
Now in this case also we are getting an empty array because we need two values that add up to ten as
per the given question.
So yes, our code is working.
Now let's take a relook at the code and see what's happening over here.