1
00:00:00,790 --> 00:00:01,690
Hi everyone.

2
00:00:01,690 --> 00:00:05,890
Let's now take a test case and walk through the code that we have written.

3
00:00:05,890 --> 00:00:06,550
For this.

4
00:00:06,550 --> 00:00:13,030
Let me take the array as two, minus one, five and three and let's take the target value as four.

5
00:00:13,540 --> 00:00:14,020
All right.

6
00:00:14,020 --> 00:00:16,090
Now we are inside the function.

7
00:00:16,090 --> 00:00:20,890
Let's say we have called the function and the this array and the target value is passed to it.

8
00:00:20,890 --> 00:00:27,520
Now we are over here for let I is equal to zero I less than array dot length minus one I plus plus.

9
00:00:27,580 --> 00:00:30,730
Now before that let me just write the indices of the given array.

10
00:00:30,730 --> 00:00:32,800
So that's 012 and three.

11
00:00:32,800 --> 00:00:33,400
All right.

12
00:00:33,430 --> 00:00:34,960
Now we are in this for loop.

13
00:00:34,960 --> 00:00:37,690
Now initially the value of I is equal to zero.

14
00:00:37,690 --> 00:00:38,020
All right.

15
00:00:38,020 --> 00:00:40,450
So let's write that over here I is equal to zero.

16
00:00:40,450 --> 00:00:43,900
And now we come into the next for loop.

17
00:00:43,900 --> 00:00:47,260
So over here we have initialized j as I plus one.

18
00:00:47,260 --> 00:00:52,060
Now because I is equal to zero j will be equal to zero plus one which is equal to one.

19
00:00:52,450 --> 00:00:53,080
All right.

20
00:00:53,080 --> 00:00:56,920
Now the values at these two indices is two and minus one.

21
00:00:56,920 --> 00:00:58,690
Right at index zero we have two.

22
00:00:58,690 --> 00:01:00,850
And at index one we have minus one.

23
00:01:00,850 --> 00:01:02,830
So the values are two and minus one.

24
00:01:02,830 --> 00:01:07,240
And over here we are checking whether these two add up to the target value.

25
00:01:07,240 --> 00:01:09,520
Now two plus minus one is equal to one.

26
00:01:09,520 --> 00:01:10,870
That's not equal to four.

27
00:01:10,870 --> 00:01:12,640
So this is not true right.

28
00:01:12,640 --> 00:01:16,210
So we are not returning I j now again j is incremented.

29
00:01:16,210 --> 00:01:17,770
So j becomes two.

30
00:01:17,800 --> 00:01:21,130
Now the values are two and five right.

31
00:01:21,130 --> 00:01:24,040
So the value at the second index is five.

32
00:01:24,040 --> 00:01:25,150
So two and five.

33
00:01:25,150 --> 00:01:27,040
When we add these we get seven.

34
00:01:27,040 --> 00:01:28,030
We don't get four.

35
00:01:28,030 --> 00:01:30,340
So again this does not become true.

36
00:01:30,340 --> 00:01:33,370
Again j is incremented and j becomes three.

37
00:01:33,370 --> 00:01:36,730
And at this point at this point the values are two and three.

38
00:01:36,730 --> 00:01:39,100
Right at index three the value is three.

39
00:01:39,100 --> 00:01:42,550
Now two plus three is equal to five, which is again not equal to four.

40
00:01:42,550 --> 00:01:45,250
Now we have reached the limit.

41
00:01:45,280 --> 00:01:47,410
Now our j is less than array dot length.

42
00:01:47,410 --> 00:01:49,390
That's uh three is less than four.

43
00:01:49,390 --> 00:01:50,890
Array dot length in this case is four.

44
00:01:50,890 --> 00:01:55,240
Right now when j is incremented and j becomes four this loop breaks.

45
00:01:55,240 --> 00:01:59,680
So we come out and we increment the value of I from 0 to 1.

46
00:02:00,010 --> 00:02:00,430
All right.

47
00:02:00,430 --> 00:02:01,780
Now I is equal to one.

48
00:02:01,780 --> 00:02:05,830
Now j again we come to the second for loop j is equal to I plus one.

49
00:02:05,830 --> 00:02:07,960
So j is equal to two at this point.

50
00:02:07,960 --> 00:02:13,030
And the values at these two indices are one minus one and five right.

51
00:02:13,030 --> 00:02:14,470
So minus one and five.

52
00:02:14,470 --> 00:02:17,980
When we add them we get the required value which is equal to four.

53
00:02:17,980 --> 00:02:19,840
So we have found our answer.

54
00:02:19,840 --> 00:02:24,640
And because the question asks us to return the indices we will return these two.

55
00:02:24,670 --> 00:02:25,780
That is one comma two.

56
00:02:25,780 --> 00:02:27,100
And that's our answer.

57
00:02:27,100 --> 00:02:32,170
Now again we have seen the time complexity of this solution is equal to o of n square.

58
00:02:32,170 --> 00:02:35,950
Why is that so over here we can see we have a nested for loop right?

59
00:02:35,950 --> 00:02:41,920
For every element in I we are again traversing the array to the second for loop over here.

60
00:02:41,920 --> 00:02:42,370
Right.

61
00:02:42,370 --> 00:02:44,170
So we have a nested for loop.

62
00:02:44,170 --> 00:02:46,930
So in the first for loop we have n operations.

63
00:02:46,930 --> 00:02:51,730
And for each operation in the first for loop we are doing n operations again.

64
00:02:51,730 --> 00:02:55,990
So that's why the time complexity of this solution is O of n square.

65
00:02:55,990 --> 00:02:58,450
And the space complexity is O of one.

66
00:02:58,450 --> 00:03:02,170
Because we are not taking any additional space, we are not storing anything.

67
00:03:02,170 --> 00:03:05,920
So again, we have seen that this is not the best way.

68
00:03:05,920 --> 00:03:07,300
This is the brute force solution.

69
00:03:07,300 --> 00:03:10,690
Now let's see in the next video how to optimize this.
