1
00:00:00,830 --> 00:00:06,920
Hi everyone, I hope you have tried to code the brute force solution which we discussed in the previous

2
00:00:06,920 --> 00:00:08,570
video on your own.

3
00:00:08,570 --> 00:00:11,780
Now in this video, let's try to code it out together.

4
00:00:11,780 --> 00:00:18,740
Now first let's create a function and let me call this function find indices some.

5
00:00:19,650 --> 00:00:20,070
All right.

6
00:00:20,070 --> 00:00:22,170
So this is find indices some.

7
00:00:22,170 --> 00:00:28,470
And as mentioned in the question the input to the function is an array and the target value.

8
00:00:31,400 --> 00:00:38,720
Now let's have a for loop so that we can iterate through the array.

9
00:00:38,750 --> 00:00:40,820
So let's use a for loop for that.

10
00:00:40,820 --> 00:00:46,760
For let I equal to zero I less than array dot length.

11
00:00:48,020 --> 00:00:50,930
Minus one because I need two values, right?

12
00:00:50,930 --> 00:00:52,820
So let me just show that over here.

13
00:00:54,190 --> 00:00:56,410
So let's say our sample array.

14
00:00:56,410 --> 00:01:01,270
Let me have a sample array over here which is like 12345.

15
00:01:01,270 --> 00:01:04,630
Now let's say we are trying to find the target value.

16
00:01:04,630 --> 00:01:05,860
Let let it be six.

17
00:01:05,860 --> 00:01:07,690
So we are trying to find two values right.

18
00:01:07,690 --> 00:01:15,010
So the I which is the first for loop over here needs only to go up to the previous element, up to the

19
00:01:15,010 --> 00:01:17,350
element previous to the last element in the array.

20
00:01:17,350 --> 00:01:17,650
Right.

21
00:01:17,650 --> 00:01:20,920
Because I will be searching matches for a particular.

22
00:01:20,920 --> 00:01:27,820
I value the value in the array at an ith index with something that is coming after it, right?

23
00:01:27,820 --> 00:01:32,620
So I just need to check I up to I less than array dot length minus one.

24
00:01:32,680 --> 00:01:33,130
All right.

25
00:01:33,160 --> 00:01:35,500
Now let's increment I now.

26
00:01:36,090 --> 00:01:37,260
I plus plus.

27
00:01:37,740 --> 00:01:43,320
Now inside this let's have a for loop again because this is the brute force solution right.

28
00:01:43,320 --> 00:01:47,670
So let's have j let j equal to I plus one.

29
00:01:47,670 --> 00:01:50,010
So I won't be checking for every element right.

30
00:01:50,010 --> 00:01:56,100
If I is equal to one I will check for j values for all the other values which are coming after I.

31
00:01:56,100 --> 00:02:02,670
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

32
00:02:02,670 --> 00:02:05,340
two, three, four and five.

33
00:02:05,340 --> 00:02:14,370
Now in the next iteration, when I is equal to two, I will try for j values 345, right?

34
00:02:14,370 --> 00:02:18,150
So I won't check values which are coming before two, right.

35
00:02:18,150 --> 00:02:20,580
Because the value before two is one.

36
00:02:20,580 --> 00:02:26,700
And I, in whatever combination is possible with one, is already checked in the first iteration over

37
00:02:26,700 --> 00:02:27,000
here.

38
00:02:27,000 --> 00:02:28,650
So I don't need to check that over here.

39
00:02:28,650 --> 00:02:28,950
Right.

40
00:02:28,950 --> 00:02:33,720
So that's why we are initializing I as J as I plus one over here.

41
00:02:33,720 --> 00:02:38,730
And this will go on till j less than array dot length.

42
00:02:38,730 --> 00:02:41,700
Now in the previous case we had array dot length minus one.

43
00:02:41,700 --> 00:02:46,710
Because as we saw we will go for I values only up to four over here.

44
00:02:46,710 --> 00:02:49,620
So I can take one I can take the value two.

45
00:02:49,620 --> 00:02:54,540
I can take the value three in the next iteration and four in the next iteration.

46
00:02:54,540 --> 00:02:55,620
And we'll stop over there.

47
00:02:55,620 --> 00:02:58,530
But when it comes to J we will go till the end.

48
00:02:58,530 --> 00:03:00,720
So j less than array dot length.

49
00:03:00,720 --> 00:03:03,270
And let's increment j j plus plus.

50
00:03:03,780 --> 00:03:11,580
Now inside this we are going to check if the value at the ith and jth index of the given array will

51
00:03:11,580 --> 00:03:12,960
add up to the target value.

52
00:03:12,960 --> 00:03:14,220
So that's what we are going to check.

53
00:03:14,220 --> 00:03:16,290
So if target value.

54
00:03:17,630 --> 00:03:18,110
Equal to.

55
00:03:18,110 --> 00:03:18,530
Equal to.

56
00:03:18,530 --> 00:03:19,430
Equal to.

57
00:03:19,580 --> 00:03:22,610
Array at the ith index.

58
00:03:22,610 --> 00:03:26,120
So value at the ith index plus array j.

59
00:03:26,120 --> 00:03:28,790
So the value of the given array at the jth index.

60
00:03:28,790 --> 00:03:31,670
Now if this is true then we have found the answer.

61
00:03:31,670 --> 00:03:35,240
So we just need to return I and j.

62
00:03:35,240 --> 00:03:38,030
So let's return it as an array.

63
00:03:39,530 --> 00:03:44,840
Now, if this is not true, then as per the question, we just need to return an empty array.

64
00:03:44,840 --> 00:03:46,670
And that would happen over here.

65
00:03:46,670 --> 00:03:46,940
Right.

66
00:03:46,940 --> 00:03:48,980
So this is outside the for loop.

67
00:03:48,980 --> 00:03:52,880
So this is outside the uh initial the first for loop.

68
00:03:52,880 --> 00:03:58,520
So when we are coming out of it and if we have not yet returned anything till this point, it means

69
00:03:58,520 --> 00:04:04,040
that we were not able to find a combination of two elements in the array, which adds up to the target

70
00:04:04,040 --> 00:04:04,460
value.

71
00:04:04,460 --> 00:04:05,360
And we are done.

72
00:04:05,360 --> 00:04:09,350
Now let's write a test case to check whether this algorithm is working.

73
00:04:09,350 --> 00:04:11,600
So let's take an example.

74
00:04:11,600 --> 00:04:15,680
Let's say array is equal to 12345.

75
00:04:15,680 --> 00:04:21,980
And let's say the target value is equal to let's say six.

76
00:04:21,980 --> 00:04:22,490
All right.

77
00:04:22,490 --> 00:04:25,130
So one plus five should give us six.

78
00:04:25,130 --> 00:04:30,590
Now let's console log what we get when we call the function console dot log.

79
00:04:31,070 --> 00:04:35,150
Find indices sum and we'll pass the array.

80
00:04:36,350 --> 00:04:38,090
And the target value.

81
00:04:41,080 --> 00:04:43,930
And let's see what we are going to get.

82
00:04:46,550 --> 00:04:48,290
Now I'm going to run this.

83
00:04:49,270 --> 00:04:52,840
And you can see that we are getting two zero comma four, right.

84
00:04:53,050 --> 00:04:54,100
This is zero.

85
00:04:54,520 --> 00:04:57,130
So that's the first element that which is one.

86
00:04:57,130 --> 00:05:02,890
And then the element at the fourth index that's 01234 which is five.

87
00:05:02,890 --> 00:05:04,510
So one plus five is equal to six.

88
00:05:04,510 --> 00:05:07,540
So yes, our, uh code is working.

89
00:05:07,540 --> 00:05:09,520
Now let's try a few more cases.

90
00:05:09,520 --> 00:05:13,240
What will happen if the input array is empty?

91
00:05:13,240 --> 00:05:14,590
Let's try that.

92
00:05:14,590 --> 00:05:17,860
So when that's happening, we are getting an empty array as the output.

93
00:05:17,860 --> 00:05:19,630
So that's correct as per the question.

94
00:05:19,630 --> 00:05:23,410
Now let's say we have uh three values.

95
00:05:23,410 --> 00:05:27,760
But the no combination adds up to the given target value.

96
00:05:27,760 --> 00:05:30,100
So what happens again we are getting an empty array.

97
00:05:30,190 --> 00:05:34,090
And what happens if I have only one element in the given array.

98
00:05:34,090 --> 00:05:35,800
And the target value is also ten.

99
00:05:35,800 --> 00:05:40,780
Now in this case also we are getting an empty array because we need two values that add up to ten as

100
00:05:40,780 --> 00:05:41,680
per the given question.

101
00:05:41,680 --> 00:05:43,840
So yes, our code is working.

102
00:05:43,840 --> 00:05:48,370
Now let's take a relook at the code and see what's happening over here.
