In this Article i am going to solve a leet code question and question goes like this given an array of integer where element of array is greater than or equal to 1 and less than.
equal to n where n is the size of array some elements appear twice and other appears once find all the elements of 1 to n inclusive which means that we have to find all the elements from 1 to n including one and n that do not appear in the area could you do it without extra space in big o of n run time you may assume that return list does not contain extra space this line means that can we do without using extra species of course we can do it and our runtime complexity our time complexity should be big o of n yes we can do that and.
you may assume that the return list does not count as extra space by this line it means that i will reset this code for phrase start you may see that the return type of the return value is listed which means that we have to introduce a list here and which is which will be of type integer integer and let’s call it name it as listed and as you know how we will define an object in java or what we will do new of class name are released and we here we are going to use list which is of type array so array list and it is also type 2 integer our data type is going to be integer you can read this as early type 2 integer so in this way we will initialize this and it will not considered as extra memory space in this question and if you analyze input here we have at zeroth.
index four one three and at index two we have two at index three we have seven at index four we have eight and index five we have two at index six we have three and at index seven we have one so the size of this array is eight so this this array can have an element any element from one to eight including one and here so if we see in this input array we have four three two seven eight.
and we don’t have five and six so missing elements are five and six and in list we have to return this in list we have to add these two element and return the list that will be our output hopefully you understood the question now we will proceed toward our whiteboard and we will see in more detail depth and we will see the approach how we will proceed towards this question.
so now we will take one example an example goes like this suppose we have an array and let’s take array index as 0 1 2 and 3 so n is going to be which means size of the array is going to be 4 and which means that our element should be something greater than or equal to 1 and less than equal to 4 so we can place any element between 1 to 4 which should be greater than equal to 4 or less than equal to 4 so let’s keep let’s give 2 here and three here and again three here and two so in this case if you see we have if we write all the elements from one two four one two three four so we have element one.
no we don’t have element one in this array did we have element to be yes we have element two did we have three elementary yes we have do we have four no we don’t have four so at last we are at the last digits and we don’t have these two elements one and four so in our list we will add first one then after that four and we will return this this will be the output so this is the question this is the explanation of question but how we will proceed towards.
solution so what we are going to do in this case we will draw here the array same array so that everything will be more clear let’s give it index 0 1 2 and 3 and i will erase this and n is going to be four and this is same area we are not drawing a new array for anything or just same array so now here we have element two here we have element three and again three and two and what we will do we will iterate we will iterate this area means we will start with first index and go until last index and how we will do this we will use for though simple for zero to.
minus one and when we are at index zero and index 0 but let’s call this array as nums this both of these are r names only as given in the question also you will see while coding so nums of i numbers of i is going to be what in this case numbers of i is i here when we are iterating when i is 0 which means we are talking about this particular element numbers of i so numbers of i is 2 and what we are going to do we are taking going to take numbers of i numbers of i and we are going to subtract one from it and why we are subtracting because.
we might have element as four and you will see that four will when we we want to go to fourth index there is no fourth index it will create a index out of one exception so we have to minus 1 from the element and after minusing it after subtracting 1 what we will get 2 minus 1 we will get 1 right we have one now what we will do terms of i minus 1 and what we will do here we will go to this index in terms of 1 and how we will go to this index again numbers so we will go to index 1 which is here and we will make it minus how we will make it minus simple minus of numbers of this which is numbers of i minus numbers of i minus 1.
i will simply subtract this whole thing here and we will store into this so in this way we will make this particular index as minus 3. now when i become 1 what will happen when i become 1 we are at the index which is whose value is minus 3 so what will happen numbers of i here i is 1 so numbers of i is going to be minus 3 and minus 3 minus 1 is going to be minus 4 and nums of minus 4 will give a index out of bond exception because there is no element.
there is no index in this particular area which has index as minus 4 so we have to understand this factor so what we have to do we have to update our this particular line of code what we have to do we have to take absolute value absolute value of this array and here also absolute value now if we take absolute value here absolute value if we did then what will be this value so absolute value of minus 3 so it will be 3 3 minus 1 we will go to at index 2 right at index 2 we will go and we will make it minus right and now in next iteration when.
i become 2 when i become 2 again we have -3 since we have updated our code it will become plus three and we will go to index two right we will go to index two and we will make minus of minus three in this case it is going to become positive so to avoid this what we will do we will take absolute of this whole code in the right side not in left side absolute value so in if we take absolute value what will happen it will go to this particular at index 2 it will go.
to minus 3 first it will convert it into a 3 and 3 minus 1 it is going to be 2 and at index 2 we have minus 3 and what we will do whatever value at index 2 we will take absolute value so it is going to be 3 and after that we are going to decrement so we have to take absolute value so our final your final code will be something like this inside for loop we have numbers of absolute value of numbers of i minus 1 and minus equal to equal to minus of absolute value of this whole whole thing absolute value of numbers of absolute value of nums of i minus 1 and this is our golden line from this this whole element will become something like.
this minus 3 minus 3 now we will when i will become 3 and at index 3 what we have we have to write and we will subtract it from first we will subtract and it will become 1 and we will go to index 1 right at index 1 it is already -3 so we will again do all the operation and it will become minus 3 only so we have here index 0 and index 3 now we will again iterate over this array now our new array is updated into this if you see it becomes something like this 2 minus 3.
minus 3 and two right zero one two three now our this array is updated to this we didn’t use any new array so what will happen we will iterate this area again since this is uh standalone uh for loop these two for loops are not nested for loop so there will be no effect in time complexity so we will iterate over this array and we will check condition if a rr of i is greater than 0 which means that if the element present at index if the element is positive then what we are going.
to do and we will introduce list also which we have to do and let’s call list as listed and at that particular time when element is greater than zero and element is positive then what we are going to do we are going to add index and what we will add add index at this point of time our index is here the value is 2 so 2 is positive we are going to add i about it we have to add plus 1 also so so that we will get actual missing number so list dot at i plus 1 in this case this value is.
positive and this value is positive so I plus one zero plus one will be added to our list and here three plus one four will be added to our list so after that we will return our list and in our list, we have one and four which is required hopefully you understood the whole approach now we will go towards code and type it out and we will see is our code getting accepted or not now I will try to write code and code goes like this we will try to offer the array first num start link and I plus and what we have to do inside iteration we will go to the index.
i and we will subtract one from it and now we will make this element as an index after subtracting one from here and we will go to that particular element and make it minus and store it into an overwritten element y minus all that element and to avoid our index output bond exception what we will do to match dot absolutely right math dot absolute we’ll use so that we don’t have any negative index math dot absolute so in this way, we are avoiding ours.
index out of bound exception for negative indexes and after that when we reached some element and if that element appeared once, again and again, we will go to that index and yeah element at that index is already negative and we will again negate it and then it becomes positive to avoid this we will take absolute value once again of this whole array map dot absolute of this whole array.
and here we are done with this whole manipulation now what we have to do we have to again iterate over the array we are going to copy-paste the same for loop and inside this, we have to check the condition if numbers of i are positive or not if the element is positive or not if the element is positive which means that we didn’t reach that element and so that.
we are going to add the index of that element list dot add index plus 1 to get the output so after that we are going to return list and hopefully, this code will work fine and there is no spelling mistake yeah run code I will make habit of run code because if you will submit then it will reduce your acceptance and get submitted that got accepted hopefully you enjoyed the video and consider sharing and subscribing thank you bye