Hi everyone.
Let's now take a test case and walk through the code that we have written.
For this.
Let me take the array as two, minus one, five and three and let's take the target value as four.
All right.
Now we are inside the function.
Let's say we have called the function and the this array and the target value is passed to it.
Now we are over here for let I is equal to zero I less than array dot length minus one I plus plus.
Now before that let me just write the indices of the given array.
So that's 012 and three.
All right.
Now we are in this for loop.
Now initially the value of I is equal to zero.
All right.
So let's write that over here I is equal to zero.
And now we come into the next for loop.
So over here we have initialized j as I plus one.
Now because I is equal to zero j will be equal to zero plus one which is equal to one.
All right.
Now the values at these two indices is two and minus one.
Right at index zero we have two.
And at index one we have minus one.
So the values are two and minus one.
And over here we are checking whether these two add up to the target value.
Now two plus minus one is equal to one.
That's not equal to four.
So this is not true right.
So we are not returning I j now again j is incremented.
So j becomes two.
Now the values are two and five right.
So the value at the second index is five.
So two and five.
When we add these we get seven.
We don't get four.
So again this does not become true.
Again j is incremented and j becomes three.
And at this point at this point the values are two and three.
Right at index three the value is three.
Now two plus three is equal to five, which is again not equal to four.
Now we have reached the limit.
Now our j is less than array dot length.
That's uh three is less than four.
Array dot length in this case is four.
Right now when j is incremented and j becomes four this loop breaks.
So we come out and we increment the value of I from 0 to 1.
All right.
Now I is equal to one.
Now j again we come to the second for loop j is equal to I plus one.
So j is equal to two at this point.
And the values at these two indices are one minus one and five right.
So minus one and five.
When we add them we get the required value which is equal to four.
So we have found our answer.
And because the question asks us to return the indices we will return these two.
That is one comma two.
And that's our answer.
Now again we have seen the time complexity of this solution is equal to o of n square.
Why is that so over here we can see we have a nested for loop right?
For every element in I we are again traversing the array to the second for loop over here.
Right.
So we have a nested for loop.
So in the first for loop we have n operations.
And for each operation in the first for loop we are doing n operations again.
So that's why the time complexity of this solution is O of n square.
And the space complexity is O of one.
Because we are not taking any additional space, we are not storing anything.
So again, we have seen that this is not the best way.
This is the brute force solution.
Now let's see in the next video how to optimize this.