1
00:00:00,720 --> 00:00:03,210
Hi everyone, let's do this question.

2
00:00:03,210 --> 00:00:07,470
You are given an array of integers and another integer target value.

3
00:00:07,500 --> 00:00:14,160
Write a function that will take these inputs and return the indices of the two integers in the array

4
00:00:14,160 --> 00:00:15,930
that add up to the target value.

5
00:00:15,960 --> 00:00:19,740
All right, now let's thoroughly understand the question first.

6
00:00:19,980 --> 00:00:25,260
And remember in the interview when this is actually happening with an interviewer, feel free to ask

7
00:00:25,260 --> 00:00:30,510
clarifying questions so that you and the interviewer are on the same page, and that you have sufficient

8
00:00:30,510 --> 00:00:32,850
understanding about the question to be able to solve it.

9
00:00:32,850 --> 00:00:38,490
Now, in this question, it says that we are given an array of integers and we are given one more integer,

10
00:00:38,490 --> 00:00:40,650
and we it's called the target value.

11
00:00:40,650 --> 00:00:47,790
And we need to check whether any two elements in the array which is given to us add up to the target

12
00:00:47,790 --> 00:00:53,520
value, and if so, then we need to return the indices of those two integers in the array.

13
00:00:53,520 --> 00:00:54,720
So that's the question.

14
00:00:54,720 --> 00:00:55,260
All right.

15
00:00:55,260 --> 00:00:59,700
Now let's simulate the interview by asking a few clarifying questions.

16
00:00:59,700 --> 00:01:02,070
And these are some of the questions that you could ask.

17
00:01:02,070 --> 00:01:07,410
Now let's say you're asking the interviewer what if no two integers add up to the target value.

18
00:01:07,410 --> 00:01:09,450
So what should you do at that point?

19
00:01:09,450 --> 00:01:12,180
Now let's say the interviewer replies, okay.

20
00:01:12,180 --> 00:01:14,520
In that case, return an empty array.

21
00:01:15,330 --> 00:01:15,840
All right.

22
00:01:15,840 --> 00:01:21,540
Now, another question which you could ask is will multiple pairs of integers add up to the target value?

23
00:01:21,540 --> 00:01:23,670
And let's say the interviewer says no.

24
00:01:23,670 --> 00:01:26,400
At most one pair will add up to the target value.

25
00:01:26,430 --> 00:01:30,180
Now again you could ask are all numbers positive?

26
00:01:30,210 --> 00:01:34,470
Now the interviewer could say no, there can be negative integers also.

27
00:01:34,470 --> 00:01:40,830
Now don't worry, it's not exactly required that probably this question may or may not have an impact

28
00:01:40,830 --> 00:01:46,500
on the solution, but you can feel free that if it's a valid question, if you feel that it's good to

29
00:01:46,500 --> 00:01:49,470
know, you can feel free to ask that question to the interviewer.

30
00:01:49,470 --> 00:01:53,220
And again, another question could be are all the integers distinct?

31
00:01:53,220 --> 00:01:57,120
And probably the interviewer says, yes, all the integers are distinct.

32
00:01:57,120 --> 00:02:03,150
And another question that you could ask is what if the array of integers that is given as an input is

33
00:02:03,150 --> 00:02:03,660
empty?

34
00:02:03,690 --> 00:02:08,460
Now the interviewer says at this point, if this is the case, then return an empty array.

35
00:02:09,150 --> 00:02:09,720
All right.

36
00:02:09,720 --> 00:02:14,520
And again you ask, can I add the same integer twice to get the target value.

37
00:02:14,520 --> 00:02:17,220
And the interviewer replies at this point, no.

38
00:02:17,460 --> 00:02:17,820
All right.

39
00:02:17,820 --> 00:02:21,270
So these are some of the clarifying questions that you could ask for this question.

40
00:02:21,270 --> 00:02:27,270
So always feel free to ask sufficient clarifying questions to get a good understanding about the question.

41
00:02:27,300 --> 00:02:31,380
Now once you have done this, let's work on some test cases.

42
00:02:31,380 --> 00:02:37,710
Remember, test cases are just different inputs and you write out the expected output in those cases

43
00:02:37,710 --> 00:02:42,930
and in the actual interview you can form these test cases along with the interviewer.

44
00:02:42,930 --> 00:02:44,160
So that's not a problem.

45
00:02:44,160 --> 00:02:45,810
You can always say all right.

46
00:02:45,810 --> 00:02:48,240
So thank you for the answers that you have given.

47
00:02:48,240 --> 00:02:52,800
Now is it okay if we work together to formulate some test cases so you can do that.

48
00:02:52,800 --> 00:02:53,310
All right.

49
00:02:53,670 --> 00:02:56,940
Now let's say the input array is two comma seven.

50
00:02:56,940 --> 00:02:59,370
And the target value given is nine.

51
00:02:59,370 --> 00:03:01,470
So what should be the output in this case.

52
00:03:01,470 --> 00:03:04,230
In this case the output has to be zero comma one.

53
00:03:04,230 --> 00:03:04,410
Right.

54
00:03:04,410 --> 00:03:08,610
Because the indices over here this this has an index zero and this has an index one.

55
00:03:08,610 --> 00:03:11,670
And we need to return the indices as per the question.

56
00:03:11,670 --> 00:03:13,710
Now what if the target.

57
00:03:13,710 --> 00:03:19,950
What if the array which is given to us is let's say 273 minus one and four.

58
00:03:19,950 --> 00:03:22,050
And the target value is equal to two.

59
00:03:22,050 --> 00:03:28,230
Now in this case we can see that if we add up these two three and minus one we will get the target value.

60
00:03:28,230 --> 00:03:31,170
So we need to return the indices of these two values.

61
00:03:31,170 --> 00:03:32,640
So this is 012.

62
00:03:32,640 --> 00:03:34,170
This is two and this is three.

63
00:03:34,170 --> 00:03:34,500
Right.

64
00:03:34,500 --> 00:03:38,460
So the answer or the output in this case should be two comma three.

65
00:03:38,550 --> 00:03:42,480
Now what if again the same array is given as input.

66
00:03:42,480 --> 00:03:44,970
And the target value let's say is equal to 100.

67
00:03:44,970 --> 00:03:49,320
So there are no two values in the input array which add up to the target value.

68
00:03:49,320 --> 00:03:55,380
So in this case the output has to be an empty array as per the clarification that we got from the interviewer.

69
00:03:55,380 --> 00:04:02,010
And let's say you have a single element as input which is 25 and the target value is also 25.

70
00:04:02,010 --> 00:04:03,840
So what should be the output in this case?

71
00:04:03,840 --> 00:04:08,940
In this case also the output has to be an empty array because as per the question, we need to find

72
00:04:08,940 --> 00:04:11,730
two integers that add up to the target value.

73
00:04:11,730 --> 00:04:14,190
So again only one integer is there in this array.

74
00:04:14,190 --> 00:04:15,300
So that's not possible.

75
00:04:15,300 --> 00:04:17,850
And we have to return an empty array.

76
00:04:17,850 --> 00:04:20,820
And let's say again an empty array is given as an input.

77
00:04:20,820 --> 00:04:26,250
And whatever target value is given in this case also the output has to be an empty array.

78
00:04:26,550 --> 00:04:26,850
All right.

79
00:04:26,850 --> 00:04:31,080
So we have understood the question and we have formulated test cases.

80
00:04:31,080 --> 00:04:34,260
Now it is time to think of a way to solve this.

81
00:04:34,260 --> 00:04:37,410
So we will look at two ways in which we can solve this.

82
00:04:37,410 --> 00:04:43,080
Now before you go ahead and look at the solution, try to think through the problem and try to think

83
00:04:43,080 --> 00:04:45,360
about a way in which you would solve this.
