hey everyone I hope you all are safe and doing good so in this series of discussing coding interview questions lead code questions in this video we'll discuss one more lead code question that is very famous one and the easy one right and that is the two sum problem so the question given is you are given in Array of integers the name is num and then and an integer Target you have to return indices of the two numbers such that they add to the Target what you have to do is examples are given also I'll show you those examples as well now you may assume that each input would have exactly one solution each input would have exactly one solution and you may not use the same element twice one element you can use once you can return the answer in any order so what you need to do is see this is one example so input is given the how is the array 2 7 11 15 and the target will be given to you that is 9 so now output is 01 means indexes of this array is 0 1 2 3 index start from zero so 2 + 7 is 9 so it will return 0 and 1 these 2 + 7 2 and 7even these numbers are at the index 0 and 1 so it will return 0 and one I hope you got this because num of zero and nums of one is equal to is equal to 9 that's why it is returning this N9 sorry these two indices right now same with second example you can say I hope you got this what you need to do I'll I'll discuss the logic as well before writing the actual program second input is this 3 2 4 and the target is six so 3 + 2 is five not 6 okay now 3 + 4 is 7 okay not equal to now 2 + 4 yeah 2 + 4 is 6 so it will return index is 0 1 2 1 and two 1 and two and every input will have exactly one solution we will assume this thing so once you find your solution just stop right so how to think how you can do this thing see generally if you think generally if you are given don't think about any programming language we'll discuss the code in Java basically and if you are not aware about one thing let me just take tell you my data structure and algorithm with Java course has already been started right and if you enroll now you will get recording of the previous lectures as well quiz assignments notes ppts we will be discussing some projects in the coming lecture on Friday we will discuss one project we'll make some project one project that is basic one whatever we have learned till now based on that knowledge we will make one simple project okay so you can enroll you can check out the link you will get in the the description box of this video you can go and check out in this course I'll cover first Java in detail and then we'll go for DSC okay so the link you will get in the description box of this video as well as in the pinned comment you can go and check out right so see have you can solve how you can think to solve this problem first we will see like suppose this is the input we are given so first we will see let's take two now try to add with seven and check is it nine yes if you got at the very first time just stop you don't need to check further you will return only indexes of this 2 and S that is 0o and one okay now suppose here here sum is not nine sum is suppose uh here 18 sum is 18 so what you will check 2 + 7 is it 18 no then again 2 + 11 then 2 + 15 it's 30 and it's 17 so you will not get the solution till now then okay two is done now we will take seven do you check 7 + 2 previous case no because it is already done 2 + 7 or 7 plus + 2 these are same so no need to check backward if you are at this element so you will check further like this you will move further only forward only so we will check not this we will check 7 + 11 and and 7 + 15 but 7 + 11 this is 18 yes you got it so you no need to check further that's it just stop and that's it so this logic you have to implement so it's I guess very simple one two for Loop would be there what we need to do you have to take one variable I and one is J because obviously we first we will take two then we'll add the seven so we need variable to store this data and all in Ram right so one is I one is J I would Loop through from 0 to till here 0 to 1 2 3 index is three from 0 to three right if we have 2 7 11 15 so one will take I and the J will contain the next element so I will Loop through from 0 1 2 3 these are index from 0 to till end and J will start from I + 1 from next element till end that's it so one for Loop here I would be from 0 to I less than whatever the length of the S and how to find out length just name of the arrays we are given nums nums dot length in Java and then I ++ within this one more for for Loop that is J is equal to I + 1 and J is also nums dot length see length would be 1 2 3 4 length would be how many total elements four but we want to move till three three index so that is why I'm writing here less than nums dot length not less than equal to less than four not less than equal to four because we want to move till three only not four right if you do less than equal to then you have to subtract one from the length same j++ within this we will write simple if like if I'm writing here if at I and j i is zero J is one so how to access these element sorry num so I Plus nums of J sorry j equal to equal to whatever the target you are given Target then we can do what see now this do some this is the method which is returning an error okay so it will return an array and in this array we will have only the indexes of two elements which are uh giving the sum of the starget which are giving the star so we can have an array and there we can store in at zero element store I and at one element store J and then simply return array ARR that's it it's the simple one the D force uh technique okay this is how you write down your program so let's implement it and let's see if it's working or not so let's write down our solution here 14 Loop let's take in I is equal to Z then I less than till array dot length simply we can find out this with this um dot length the array of the length sorry the length of the array then I ++ and one more Loop in which we have second pointer that is j = i + 1 why we need two variables I hope it's clear to you less than same nums do not less than equal to it's less than okay remember this thing and why this less than only I have told you now we check if how to access elements name of the array and then index plus nums of J equal to equal to whatever the target given here whatever the target is given equal to equal to this then we store this I and J in an array and we return that array because this method is returning an array so let's just take an array here of int Ty int let's name of the AR take ARR that's it end with new and this is how we uh initialize an array in Java the length I'm taking just two because I need only two elements in that because two index only I want so a r0 would be I and AR r r 1 would be G that's it and at last simply we will return this array because it is returning array so after this whole Loop just return ARR but here one more thing after finding one solution because input would have exactly one solution so if you get the solution then just break no need to check further break means out of the loop now let's see here it will return RoR and that's it so let's run our code and let's see it's working or not we will get a compiler error here and what is that okay see you haven't we are just using this J but we haven't declared this obviously you have to declare like int I so int J if here you are not declaring so anywhere else like here you can say int J something like this right so int J now I guess it will work fine let's run this and see what you are getting yeah case one accepted case two and case three so all the test cases we have passed so you can now submit from here only I hope you got this how it's working so this is a solution I hope you got this if you got this then well and good and you can leave now but if you want to dry run this with me you can just continue watching this video okay so we have this see I have taken one array and size is two so one array here size is only two the name is AR RR zero and one maybe the address I don't know but maybe suppose 100 and it's Sint so 104 next element would be 104 because one element will take four byes now for Loop so we have declared one I variable having zero now condition see the condition I should be less than nums do length it will give nums do length so nums array of length suppose you have given the Nam they have given that this was the given example 3 2 4 so nums length is three so it will give three so I zero I less than three condition true so we will enter here now J now J is another variable it is I + 1 I is zero J would be 1 right so I is here and J is here now okay this condition is also true we will enter here now n of I num of J num of I is num of zero num of zero is nums of zero is three so 3 + nums of 1 is 2 is equal to is equal to Target Target is given in this case is six is it six no condition is not true so we will not enter here the control will not enter here within this if blog now control will go to Here and j++ Now J becomes two is this condition true yes condition true again enter here now num of I is still three plus number of J J is now two so J is now two so four 3 + 4 is equal equal to 6 no because it is seven so again the condition is not true so control will not enter here in this if block and j++ now J becomes three is this condition still true condition is not true because J uh J is three less than num do length is also three is it less than three no condition is not true so out of this four Loop means now the time is for I ++ now I becomes one same step would be repeated condition true I less than this yes enter here J becomes I + 1 now J becomes I is one so now J again would be initialized and here J would become at this point of time two J is two now again check number of I I is one so number of one is two so again in next situation 2 plus number of J J is 2 that is four is equal to 6 equal to equal to six yes condition true enter in this case AR RR of Zer is I I is at this point of time one so here one would be assigned J at this point of time two so J would be assigned so the indexes of A and B are assigned to this are and break break means we are still remaining with this Loop because J is still true j++ then I is still one then I would be two then I would be three so for two same iteration would be continued but we don't want to continue now because the solution would have exactly uh this problem would have exactly one solution this is given so directly I want to break from here I want to go out from the loop so just break right now break and return ARR so AR it will return one and two whatever the SR the AR so output you will get is one and two you got it I hope you got this problem solution right so now that's it for this video now in the next video we'll see even more lead code problem and if you want these kind of video helpful you can just let me know in comment section so that I can continue uploading these kind of videos lead code questions or any coding question and if you want me to upload any on any specific question on any specific lead code question you can just mention that in comment section okay and now that's it for this video now I'll see you in the next video till then bye-bye take care