Learning Data Structures - Array | Tanmay Varshney | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

32 Lessons (2h 44m)
    • 1. Introduction

    • 2. How is Array Stored in Memory?

    • 3. Instantiating an Array

    • 4. Accessing the elements

    • 5. Initialising the Array

    • 6. Advantages And Application Of Array

    • 7. Searching in Array

    • 8. Searching in Array - Code

    • 9. Insertion In Array

    • 10. Insertion In Array - Code

    • 11. Deletion In Array

    • 12. Deletion In Array - Code

    • 13. Rotation in Array

    • 14. Rotating the Array Code

    • 15. Searching In Sorted Rotated Array

    • 16. Searching In Sorted Rotated Array - Code

    • 17. Reversing The Array

    • 18. Reversing The Array - Iterative Approach

    • 19. Reversing The Array - Recursive Approach

    • 20. Missing Number In Array

    • 21. Missing Number In Array - Code

    • 22. Pair Sum Using Sorting

    • 23. Pair Sum in Array Using Sorting

    • 24. Pair Sum Using Hashing

    • 25. Pair Sum in Array using Hashing

    • 26. Peak Element in Array

    • 27. Peak Element in Array Code

    • 28. Element Occurring Once in The Array

    • 29. Element Occurring Once using XOR

    • 30. Element Occurring Once using XOR - Code

    • 31. Leader In Array

    • 32. Leaders In Array Code

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Arrays: The simplest and one of the most important data structure which also acts as the building block for many complex data structures.

This course on arrays covers:

  • All the basic concepts of arrays
  • The various syntaxes you need to know
  • Different utility algorithms of  arrays
  • Questions asked in many coding interviews
  • Lots of practice stuff like quizzes, worksheets, and coding questions

Taking this course will help you:

  • Step into the world of data structures with strong knowledge.
  • To be well-prepared before touching on complex data structures.
  • Ace your upcoming coding interviews.

Topics at a glance:

  • Introduction & Declaration
  • Instantiating an Array
  • Accessing the elements
  • Initializing the elements
  • Advantages and Applications
  • Search in an Array
  • Insert in an Array
  • Delete in an array
  • Rotation in an array
  • Linear search on an Array
  • Binary search on an Array
  • Search an element in a sorted and rotated array
  • Reverse an array(Iterative)
  • Reverse an array(Recursive)
  • Find the missing number in an unsorted array
  • Find the missing number in a sorted array
  • Check for a pair with a given sum in an array
  • Peak in an array
  • Find the number which appears once in an array
  • Leaders in an array

Meet Your Teacher

Teacher Profile Image

Tanmay Varshney

Software Developer, Tech Educator


I am a Senior Software Engineer with vast experience of working in top tech giant companies.
I have more than 6 years of industry and teaching experience in domains like:

1. Designing scalable architecture for complex and distributed systems.

2. Developing components in a system across the full stack.

3. Solving complex data structures and algorithms related problems.

These are the major skills needed to be a good software developer who can excel in any tech company easily. I am really passionate about sharing my knowledge and expertise with you.

Thus, I am on board to create awesome technical courses on Skillshare based on my expertise which can be understood in the simplest manner.

Come, join me in this learning adventure!! I will... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction: Why should I be learning data structures when I want to work into means like data sites, machine learning and artificial intelligence should not be focusing on does instead. After all, the future has promising opportunities in these two meet learning data structure might feel unnecessary in the state, as you can do almost everything using the open source libraries. But it's always better to learn how the underlying program books, so that you can be anything from the basis for my understanding off the data structures helps in writing more vision. In this course, you will be learning about the Eddies. It is the most Parliament data start, and Harry is a collection off objects off similar data types like any other data structure , which supports the following basic operations, such finding a value within the leader structure, adding a value in the data structure. Delete removing a value from our data structure, a little baloney. All this and, of course, much more in the lectures to follow. The objective of this course is to make your total with the basics off the edit infrastructure. Let's dive into the course now 2. How is Array Stored in Memory?: now How in the memory are these variables? Often every actually store, so they are stored in the form off finding was memory locations the a unique based address for the first element off the area. So the first element is having a unique basically, it's on the others. Other variables Address can be computed from the offset off from the base address Monday bag. By the day that I sex, so the formula to access let's say an element or you can see address off an element can be fondled by first. We have to take the basic Krystle Daddy bless the offset off the element from this basic Chris. My date by the data type sex. Well, I dance because that's right. So this is the formula for calculating the address off any element inside in every. So let's draw a diagram to make it clear. So let's say we have on any, uh, let today there are a number off elements. So 1st 6 elements are like this. Um, since these are collected types, so the data I off the elements is corrector on the size off the state. All right, that is a corrected. Did I is one bite. Thank. So now let's see this very the basic endless off the city starts from 200 so visit resist 200 on similarly colon. Other elements will be stored at locations 201 202 103 204 100 Fine. And so, Andi, since the metrics, any index starts from zero. So this will be an egg zero. This will win next one. There will be 2345 And so So let's say we have to compute the address off the fourth this off the Lexan fifth element they so that will be actually at this off. Fifth element will be complicated on we have. If we have to access different element that will be the offset from the base will be n minus one, as we saw in the previous video. So the offset off the fifth element will be and minus in darkest five minus one For Soto access before element, that will be a or four. And now we are calculating the address off this element to doesn't be basic, Chris, that is 200 bless offset that is full times the size of the teeter. So here the size of the readers at this point. So the address off the fifth element is actually 200 for that. You can easily be a fire from this picture. 3. Instantiating an Array: as we saw in the previous lecture, How did they get in any in this lecture? We will proceed further to see how to actually. Instead, she ate or define in every So as we saw that declaration. We do something like this and and square records or in square brackets state. So these both are Well, it's in Texas for declaring and Eric still here on the A reference to the any has been created that this no actual memory is being allocated to the area on the actual memory location is being done by the instant she ation process. So in this process, the allocate the memory. So this anybody even so, how we do this. So this syntax for doing that is a square bracket is equal to new and square wreckage sites . So this size, basically, they noticed the number of elements which we need inside are and it so this particular number of elements will be stored in contiguous memory locations. So, for example, if you want to declare on instead, she 8000 elements off indeed a type. So these 1000 elements will be off the same time in danger. So how do that so that will be and a square bracket is equal to new and 1000. So in this way we declare on instead she it also it posing Indeed, your type variables within this particular Eric. And let's say for another example, if you want to declare an instance, she 8000 elements off corrected, like so let's see. So the leader, I believe God and let to the name is name of the is C so this index will be C square brackets is equal to new. Since this new is the operator, which is used for in sensation, we will include in each and every instance station ordinary. So this will be Cassie. Square brackets is equal to new. Yeah, on the size so size. Let's say we have we want toe have 1000 elements inside are correct ready so will specify 1000 here late. No, we have learned how to declare. On instance, she ate or locate memory to in a We will see various ways how to access an initial is the elements off the area In the next lecture 4. Accessing the elements: in this review will not see how do exist the elements off in every once the air has been declared on instant shaping as well. So the index often any starts from zeal. We access the first element as the name with zero inside square records. So this zero is the offset from the baseball daily. So for the first element, the Dawson is zero. So to access, the first element behind as and any Lexie A on zero inside the square pregnant. So this is the way to access the first element. For the second element in the office, it will be one from the base of Daddy. So this is the first demon on the second element would be offset will be one. So when will we mention inside the square brackets? So the second element will be excess, like eight off one. So we call it as a often with a 30 minute similarly, the offset weak Onda. We will access the her Lehman does heels do. And so on Italy last element to the last element. Let's it that is the element of very the offset will be end minus one. So, Alexis, the energy element of dairy. Well, I does a off and Madison. So this is the way to ecstasy. An element of dairy. So Messam off sick for a day off size Ennis and minus one. An element position, as you can see from here, is and off the base of the l E plus the offset of element. So as we calculated for the Annan element, the off signal is n minus one On any basis is this day. So basically, any man is an offset from the A is used to calculate the and it's element. 5. Initialising the Array: in this video, we'll see what is initializing a binary and how do we actually do it? Initializing open really means how be entered. The elements are probably the elements ordinary. So they're basically do is off. Doing that first is through every liberals and sickness through loops to edit Idris Uh, it is used in a situation where the size off the any on the variables off the airy or you could see the elements of the alley are already known to us. So in that case, every little scam used. So let's it. We know that our size often and it would be five and we want 12345 elements to be stored inside that. So toe do that true. We can do this through originals by this index. So we taking a culinary off indeed, today on B and essentially it through the new with nuclear work on, so that would be in a equal to new and square brackets. And inside this after the square brackets really clear the elements off the area as 12345 within the current places. So this is a way off. Initializing are very through every little's. We didn't be declared the area off size five that the elements as one Dupri for five. So the land, well, this any it reminds the length off the creativity which is equal to five. And there is no need to actually write this new impart basically, in the latest versions of job, So basically, you can write it as and A is equal to 12345 inside this curly braces. But this is on so the same as this inside latest portions of double as you might be used. So this was the first day off. Initializing the area through diligence on the second week is initializing through blues Two looks here we're using a formal to initialize the attic. So first of all the instance, she ate the area as we did in the previous videos. Instance, Shit like this and square record A is equal to new and And let's say the signs of r. A is five, so we get it as new and square records five. So this is by this way, we instance she ate the area on after instance creating we actually, now you need to Philip Orange place the area. So here we are using a for loop. So inside the for loop, we start from the first element That is, I equals zero means the first element. The index start from zero Uh, the blueprints till the Linda very that is a Dorland on each time. In each of tracing, we're implementing the value of fire. So for each contrition, the feeling off the elements is like this. If I is equal to I plus one. So what actually is happening here for the first contrition? It is the I zero. So zero will be one in the second generation. A off one will be do, um, similarly, in the penetration it off to will be stored us three, that is two plus one and similarly a off three stores for And finally, since this is Lou run still a lot land which is equal to five. So for the fifth and the final attrition, a or four will be three last five. So, as you can see both the ways Philip, the it is Philip this at a as 12345 So these are two different based off initializing the 6. Advantages And Application Of Array: Let's go after some advantages off using any the first ever wanted is that multiple date items off same later date can be excess using a single name inside every we just need to remember the any variable name. The 2nd 1 is is that every allows render mixes of elements. This makes accessing elements by position faster, as we just need to know the index off the reading any element. The third Monday it is the end is have a better gash locality that can make a pretty big difference in performance off your program. Now there are some real life applications off using in every the first application, as it is obvious that is are used to store a convenience list off values. It is our used to implement search algorithms as well. The enemies are used to implement sorting algorithms for this and that is used to implement data structures such as stag. You keep it center, which we will see later in the course, and that is are also used to implement various city, usually legal guardians 7. Searching in Array: So we have a problem that we want to search an element in an A. Andi returned the index, for example, We haven't s 1234567 So there are seven elements inside the area and we Hubble search anyone for inside the city. So, as you can see for is present inside the area and in extreme. So our Izzard should be indexed three. Now, let's take a look at the garden to come up with a solution. Now the garden for setting element off and any inside theory. So we dig us, search the Iranian on the women to be searched. So first of all, it's start it reading through the airy. I didn't come here each element with this required element. So it it any integration If the element is equal to the women to be searched, then we have found the element. So we have to return the index off that element. Uh, otherwise, if after training through the area also, we are unable to find out the element, we just simply done minus one. So that's what we had our area as 123 for fine six and the indexes are 01234 by six services i e And we need to sort the element for inside dearie. So it dreading the Eddie, we compared each element with the required elements. So for the first situation, the element is one. So we compare These are not equal. So we move on to the next situation Again we compare It wasn't not equal before. So again, please go after determination at this tree Daelim industry So three also not equal before now in the fourth attrition we have elements as for so at the Ford hydration, the A or three is equal do for which is equal to our required element. So that means the index We have found out the index off the fire element and that is equal to three. So index equals three will be returned to the any. Similarly, let's see if we have to find out an element eight inside, veering so like we did in the previous a previous for the previous example. And also we will be a trading through the any, and by comparing to each and every element will be seen that no element inside the every is equal to it. So we re coming out through the loop off penetration. Andi, Finally, we are not able to find any element. It is equal to eight. So well, Billy Dunning minus one inside this case so far certain and eliminate eight minus one will be indicating that the element is not fun. Pinsent Theory. 8. Searching in Array - Code: Let's take a look at the board to search an element X inside another. So do so. We have a function search which takes in any oh size and and it also takes an Indian X. So we have to find the index off this element X inside. Very so if the index of the element X is phone, we've done the index otherwise billiton minus one. So let's take a look on the working off this function. So what we do is we just simply trade over the every Andi for each element. Also the Eddie that just any I we compare with the wanted element that is X. So if in any case, the element every I is equal to X, then basically you just done the index. That is I from this function. But in the case, then the element is not phone inside the Eddie. So we come out of this loop and finally we're done just minus one that we have that we are unable to find the element inside this area. So let's take a look at the working of this court from this Minnery. We try to take a sample. Every elements are in 2030 40 50 and 60 and 70. No, we're trying to search X equal to 40. That is the excess here. 40 on. We have to search 40 inside this, Eddie. So, redan, uh, this function search with this X With this element X is equal to 40 on Blessed is the name on the size of that it that is equal to seven. So let's try to in this court. So, as you can see on the right side, the output is next on the element 40 between researching in the areas three. And as you can see, the next of the fort index of the Element 40 012 tree. So it's giving us the correct answer. No, Let's try to do it with another key values. So it started coming this out on Try to run the same good with another element? No, we dig element. Weichel do 90 for example, on people night not trying toe search this Anyone? 90 inside the city? A. So you can see there is no element 19 inside the city. So when we read, search this element 90 from this any it will be We will have The answer s minus one, since this is not present inside this area. So let's try to run this. So, as you can see on the right side, the index called Element 90 in that is minus one as my nineties. Not presidents like this area. So this was a basic good to a certain element inside genetic. 9. Insertion In Array: Let's take a look. How do we do insertion inside of me? We have to insert element inside in any and then bring the updated elements. So we haven't had him. And yes, that's a 1234567 So there are seven elements inside the area, and now we want an element eight. Toby I didn't Saturday, so our final answer should be one do tree for 567 and then eight upended inside dealing. So there will be eight elements on values to be something like this. 12345678 So this is our problem. Now let's take a look at the bottom that how we will come up to the solution. So Wortham is that we have to insert inside and Eddie with in the limit to be added and let's would be hard to find with a capacity off the dairy. So the best means the maximum number of elements that the Eric and hold. So our first up for the garden will be that we have a check. If the size off the Ari is already greater than or equal to the capacity of that very that means we're we can no longer at nearly any element in Siberia. So we were just Britain, the original size of Danny, and it is not so. Then we will be able to insert the element on the insertion off the element will be doing at the location of very I'm since we have inserted and elements successfully. Then we'll be list returning the original size plus one as the new size off. Very. Let's see we have any as 123 for why six and the best e off the ferry is seven, and we want to inside an element eight inside To say so element is equals do gigs. So now, plus, we will compare that the signs of the any that is equal to six is greater than or equal to that. The best deal today. No. Six is less in the capacity, we just said, so we can move forward to insert this element it inside the seven. So it was just inserting this element at the end of dirty, and finally, now the new size will be or is in size, that is six plus one toe Dapple equal to seven. So we will be returning this new size, which in 4 to 7 as the new side a little Eddie. No, let's say we have to and another element element, which is equipped nine inside this newly created A. So this newly later, that is 123 568 So, first of all, good. Make sure that the size off that Eddie is or really greater than or equal to the capacity of the area, so capacity of that Eddie is seven, and the size of that Eddie is actually seven now, so we can no longer accommodate any new intervene elements. So that means we will just return the original say's that is seven, which is the original size off the ready, and we will be not adding any element inside this. 10. Insertion In Array - Code: we'll have a look at the court and how to insert an element in an area. So don't do that. We have a method inside this. Any insert class name guys insert its stakes in any on off size. And Andi, this is the key which is takes as an argument to insert and daddy. And also there is a capacity off the that is the maximum limit off elements which we can inserted into in this area. So first of all, inside this matter often, sir, we check if the original size off this idea that is in is already greater than or equal to capacity. Then we just returned the original size and on we are no longer inserting any elements. Since deterrence size is already it will do a greater than the capacity of this area. Otherwise, if if this is not so, if this condition is not true, then we basically insert the element and the last index that Eddie and at the index and which is the newly created last element. So in the end, we insert this, he added, in is equal to key. And finally, if we are able to insert an element we just returned the objected size that is, end less one from this matter. So now let's try to run the score with a simple example. So inside main function we have this very with the best t 20 Initially, the elements are loaded with 124578 they. So the initial is equal to six. The festival were inserting a key, the one here which is equal to nine inside This So that's trying to first print this any before inserting an icky. So before inserting. If you try to run the score, this will bring the original Eddie. Well, uh, this is by mistaken by mistake, this is Commander. So let's try to come in this list. So if you drive around, done it again, we you can see that before insertion. This Eddie is something we just which which we have started from from maybe be obstructed. So that this 124578 So now let's streible insert. Okay, Sochi one here we're inserting that is equal to nine. And after inserting, we will print the mortified at it. True, No, that's tryto in this court. So if you're running, then you can see the ski one that is equal to nine is successfully in circuit in sit in this area. So now let's tryto insert when more key, so keep your inserting and keep. It was defy industry so we'll insert when working here found. We'll run this to see that the newly legendary has nine as well as tree. Both the keys are inserted at the end of the area, as you can see from this, So this was insertion inanity. 11. Deletion In Array: Now let's move forward to take a look at How do we do volition inside Every so we have to believe an element from an early if it exists, so are ready. Let's say we have again the elements has 1234567 And now the element do with religion, is that it? Five. So five exists inside this any so that element after the leading the any will look like one . Don't 3467 Since this fine is already billeted So the new any will be 123467 after the leading Dillaman five from very now the garden to delete an element from the Eddie is something like this. First of all, we find the position of the element who will deleted using the search algorithm as we have discussed before. So if you were able to find out the position off the element, then we couldn't move forward. So if the element doesn't exist, we just bring the element doesn't exist and redone the grand size off the area other ways. If the human does exist, we start from the positional element, but which we we got from the search of garden till the end ordinary. And what we do is we should be any elements one place to the lip sync. So why are we doing this? Because and from that place, we just want to remove that element. So we have to perform the stripping operation on that shipping operation is done one place to the left side from the position of the element of the end of dairy. And finally, if the element is fine, if all the elements are successfully shifted well done. The new size off the A s original size minus one since we have successfully delivering the element from the area. So, for example, let's say we haven't any as when do three for fine six and seven and we want oh, delete an element five from the city. So first of all, we start finding the position off this element in secondary. So the position off this element is this That is, it is at the fifth position, or you can say I in export. So we have successfully find out the index equal to four off this element. So from index four or the fifth position, we start shooting the elements one place to the left side. So basically at disposition from this banquet before or fifth position, we insert the right element to one place to the left side. On the seven will be shooted. One place to the left side. That is that dissuasion. So we can see the new berry will be 1234 And from here we will insert this anyway. So that would be six. And that desperation the seven will be Children one place to the left side. So this will be against thank you. And finally well done. The new size of areas or even size, which is it will do seven minus 16 So this is the newly created Every so we are concerned with These are dimensional. So the new size of the area is six and we saw that Element five. It's successfully deleted from the every on shipping off. All the remaining elements is being done. One gets to the level set. So this is the Gorgon. How do we do volition inside 12. Deletion In Array - Code: Let's take a look at good to delete an element from get it. So did you lead an element? This is the function which will do them believe Chris Egypt. So, uh, delete an element inside this were first finding out the position off this element. That is the key here, off which which we are trying to delete from Daddy. So first, we need to find the position off this element from the city. So as we have discussed in the search element video So the function which were using in this for the searching often element. So this function is basically the same mass that function. So this is doing the search operation by which we were able to find the index off the element. If it is phone other ways, we have your training minus one. So there's the same as the search element. I meant it. So after this, if you're able to find out the position off the element, then we will proceed further. So if the element is not phone, that means this matter. There Don's minus one. So in that case, if position is equal to minus one, we will simply say that element is not fun on since this element is returning an indigent. So this indigent type is used to, uh, returned this size off the modified 80. So if the element is deleted, V didn't and minus one other ways rude and the original size like this. And so if the position is equal to management, that means we're not able to find the element inside Terri. Then we will simply bring the element is not found, and we will return the original size at this end. From this function other ways, we are able to find out the position off this element which is not equal to minus one that needs. This element is present in this area. Then we will proceed further to read this element. So let's say the position US pause were able to find the element. Then, from starting from that position, we will try to shift the neighbor elements from the right foods left. That means at it I from the position I could ah, position us boss. Still the second last element, we will replace it with the next element. So basically, we're shifting one element from the right to left, starting from this in expose baby. And after this, since I get it successfully a burrito lead element, we will return the abdicate size of the area practice and minus one like so, this is the delete element function which were using to lead the element from there. Let's try do on this score with a simple example. So this isn't any The elements are 10 50 30 40 point find this is the end which is the size on decides this five. First of all, we are rending the area itself. So So first of all a day before relation. So just to ensure that the area gets printed correctly So as you can see the raise 10 2030 40 20. So wear doing nothing here, just printing ordinary. So no, you start to delete an element from this area. So commending in commending this again, you know what I'm doing is I am trying to delete an element that this he is equal to 30 from this area and after deleting what I'm returning from this method is the of needed Saiz off the area. So that will be travel inside this variable and and finally we will be using this newly sacked newly created a new created data size basically to bring the elements off the area. Let's try to in this court. So, as you can see on this side, Gary before deletion was this as we printed previously. Now, after deleting this element 20 as you can see, this element I p is being deleted from the city successfully on we are shifting these elements that is starting from this 40 one place to the left. That means for people shooting here. So discomfort it has come here and 20 will be shooting there. So it has come here and 30 years will do the trick successfully. So this program shows that are the lead element function is successfully deleting elements from the area. 13. Rotation in Array: gradation in every. The problem is through date and every off size and by the elements. So here we are, assuming that quotation is performed in anti clockwise direction, so are ready. Is, uh let's say 1234567 and rotation is done by two elements. So what will be the final dessert? So after reading two elements and the clock was fashion, we will obtain the result as 34567 and bundle will be in certain in the end. So after doing the anti clockwise rotation, this will be the wizard. Also, the gradation is done by two elements. Now, let's take a look at Elgort Um, how we will achieve this. So they got, um, Rudic Books. As we take any as first argument, the second is the signs of their ready On the turn is the which is actually the number of elements that have to be rotated. So how do we achieve the rotation? So the first step is at the store the first be elements in temporary. Now, this should rest off the elements. Two D places left Andi finally be store back that d elements which are being stored inside the temporary on the last deep places. So let's take a look at an example. So plus, it's they are any is something like this on here. We take the physical toe. That means we have to rotate the very by Gulen's So two elements in anti clockwise fashion . So first day for temporary. So inside the temporally Harry, it's stool, the First de Elements that is the first to elements which is one and two. And then we ship the rest off the area elements to be places left so deep places means to places left. So our new and it will look like this. This starting from this element will be shipping. So three will come here at the first position, four will be coming at the second. Similarly 567 and the 67.2 Bilby store. Here are heroes since we're just coping at the new location. So at the original finally two locations, the 67 will also be there. So this is the second step. No. The third step is we have to store back the key elements. That is two elements off the temporary very at the last G places. That means that the last two places So what we do is all right. Values off the temporary that this one and two dubious positions. And finally, we are able to find the rotated every which we have to rotate by two elements. So finally we are able to achieve the Louisville area from here to hear any is 3456712 Which bus are in prison? Don't worry. If you are unclear about any step off this algorithm, they have been making it more dear when we are going through the cord for this particular problem. 14. Rotating the Array Code: we'll see. How do they did the elements inside netting So true did elements inside Geneti we have, ah, this method no need, which basically eggs and Eddie off size and on this day is to tell us that these amount of elements we need to root it and mind you, a rotation is stricken counterclockwise direction. So we will be rotating in counterclockwise direction these D elements thing. So, for example, if you have an airy 1234567 and he's able to do that means we have to no need. The that is a two elements from the Steri in counterclockwise fashion. That means these what this two elements want to will be shifted to the end here and the final they will become 3456712 So this in this way, we're doing the tradition, right? So to do this inside this road, it mattered. What we're doing is, as we were discussed in the garden section, were first of all, the step one is we're taking a temporary any off size d. Andi, we're storing the first the elements inside this temporary. So this step is just to store. The first, the elements inside a temporary area which we created previously? No. Since we have just copied the first the elements. Now what we will do is starting from this Beit Index. That means after if, after we have a copier the elements that means still in next D minus. When we have B minus one, we have covered. So from starting from the index D in the end, off, every even shift the elements be places to the left. That means if equal to do here, then that means three will be shifted here. Four will be shipped with second place. Five will be shipped it to the third place. Six will be shipped into the fourth place and seven with Bishop to do the first place. All right, so we're just shifting the elements. Be places to the left. So this is our second step. Finally, the third step is toe copy back the D elements from the temporary Do you know, Daddy? Now, as we remember that in the temporary we have store the first the elements off the area. So we are now just copping back those the elements towards the end of that So this court is basically tryingto stole back the D elements off this I'm pretty any So be we're calling this loop de times to store the elements towards the end of the So this part of court is doing that. So combining all these three steps, we will be able to nor did the elements off the area in Quantico. Progress Faction fashion they. So let's try to run the school with this simple example. So we're having in any with elements as 1234567 and musical total. So vegetable, let's bring the original area so coming despite it is strange rooted in the city. So initially I run this so original areas you can see is 1234567 No, that's right. Incoming This once again. So after in commending, I'll call the group date method. But with this A as the first argument, the second argument is the size, which is seven. Here on the third argument is the number of elements which we want blue in anti clockwise or counterclockwise fashion. So that does the trick will go to which we have defined here. So now we will call dysfunction. And finally, we'll be bringing the rotated every through this print very matter, as we have previously discussed. Also, true, I'll try to run this court again. So no, As you can see, the original Larry was 1234567 And now the rotator that is being printed as 3456712 techniques these want to that is that the elements that is, that he was equal to two. So these two elements I've been shifted in a counter clockwise or anti clockwise passion towards the and so the router that it becomes as 34567 and went toe so by this manner were able to forget the elements often very many places. 15. Searching In Sorted Rotated Array: No. Let's discuss a problem. In every we have to search an element in a sorted and rotated it. That means the is sorted and then rotated. So we have to search an element in sex surgery, for example, we have any like one do three, four fine. 67 And we rotate two elements from the right in counterclockwise direction. That means that it becomes something like this. 6712345 So this is the area we are taking inside our problem. So this is a rotated and sorted. Hurry the bus. We have sorted the area and then we have rotated this sort of and inside this way we have to search an element. Let's say we have to search for insect This so, as you can see for it is present here, that means 1st 2nd 3rd 4th 5th 6 That means a six position, or you can see index fifth. So the results should be the index off this element that this fight. So this is our problem? No, let's look at the solution to this problem. So as we were having, I really like 6712345 So this was our sorted and regularly. Now we have to Sirte four inside this. Okay, Thank so simple solution which comes in our mind is just look linearly inside the sari and search for this element for so that will be a simple order off in solution to this problem . But it is obvious that it is not the optimized solution to this problem. So we have to find an up in my solution to this problem. So in order to optimize it, we have to find a better way or you can see a they test time complexity less than order often. So we will. So first thing which comes in our mind is order flogging and order off. Logan can be achieved through by envisage symbol. So the thing which comes in our mind is we have to somehow play by dessert inside this rotated and sort of dairy in order to come up to an optimal solution. But since this whole of the airy is not sorted, as you must be aware, that bind lizards applies to assorted at it. So we have to apply. I'm mortified about research inside necessary. So however you go about it. So let's see by going with the concept off buying research. First of all, let's I write down. The index is off this any. So this will be 0123456 Right? So first of all, we calculate the mid index from the city, so the mid will be zero plastics by to dock with the tree. So this will be the main index. No, Here comes a tricky observation, but either off this, any lower to made or this very minute plus one still high will always be sorted inside surgery. That means you have to look about this, that this observation is the major key behind the solution of this optimal solution. That either the left part that is from a low to mid or the right part Adam and Placement Ally will be sorted. So either of these two parts will be sorted. And if we find assorted part in this attic, that means we can apply buying research inside that part. Like since buying the search gets applied insider sort of dairy. So our Wharton will be something like as we discussed. So first of all, we'll go about finding the middle point are you consider middle index. That will be lower index. Bless Hynix by two. So this will be a mid index. Thank you. I'm just going by the notion off finally sets first of all, will compare our key. If the key is present at the middle point, all the middle index and basically we will do it under made index. Right? So if I go about this lex it full Alexa completely first turnover the bottom, then we will then this will in our garden or this example. So as I said that if the Keys president middle point with it simply did under mid index right other ways, As I said, either the left part or the right part that is any low to mid will be sorted or any plus a remake plus one high, or you consider the parliament off. The index will be soccer. So first, if at a low two minutes started, then we will check whether our key, which were searching, lies in the range from a off Lord Index to the day off mid index so that if our key lies in lies in this strange, then obviously we will be going in the strange. That means our my research will be flickering down to disobey that this at a lower index through the mid index. Otherwise, we can trigger for a limited plus one to supplement, right? So this thing gets applied only if this at a lower index familiar sorter like otherwise. If this part is not sorted, that means definitely I didn't make plus one toe upper limit will be sorted. So if that is the case, then people again search Archy, that if our key, which is to be searched, lies in the range from any off mid plus one toe area of the apartment. So it lies in the range, and definitely we will be going to recur for Ari off mid plus one to the apartment. Otherwise, as discussed in the previous case, we will be recurring for the left part like this. The a lower limit to Mitt. So this is a deal garden, or you can see the optimized garden to solve this problem. And as you can see clearly that we are using a single pass off immigration to achieve to the solution that that means we're not we're not passing over this, but over any particular Eddie, which is rotated and sorted more than months sequence. We're coming up to the solution in a single pass, right? No, let's quickly run this and Gordon over this example. So we have 6712345 Right. This is a very so first of all will go on to calculate the main index. The meeting next will be three and the value the area element at the 1,000,000 nexus to and we have a search for right. So at it three, which is equal to two, is not equal to four. That includes the second point is not right, so we'll go inside. Other. The answer will be true or this else will, which will pray. Select triangle Czech vit off the Which part of the any disorder? So any lure made Lauren next to me. Is it sorted? No, it's not sort of that means definitely at a mitt. Plus one toe high will be sorted. And as you can see this part, it's soccer, right? No Are our solution breaks down to this party, less of it. So we'll be recurring inside this. This somebody so no we will be again concluding the mid index inside this somebody so major next will be four plus six by to Pakistan by two. It will be fine. I will be going again to calculate this So we'll be going again to compare if this model the element at this index that is the main index is equal to the value off the key, which we were searching. Yes, mingle four is equal. Before that, that means you have phone your key and that is the final answer. So you will be returning the index of this element that is fight, so the answer would be five. 16. Searching In Sorted Rotated Array - Code: so, no, we'll see the good do. Certainly one in sorted and located Eddie. So for that, we are using the search mother, which is they're dunning the element. Or consider index off of the element consorted in your community. Right. So we're taking Eddie the lure and higher next off the any on the tee off whose index we have to find in this secluded and sort of dairy. Right? So, first of all, we're using a basic knowledge of Europe using basic check as the law should be readers should nor be greater than Hi. Right. So if Lewis good and high, we will return at any is reserved as minus one. Right. So this isn't this is not a valid input. So a little bit of dunning minus one Just going to get that? Uh, we're not having a really didn't put, so this is on. And then after that, uh, we have we're calculating the main element as low. Plus I rightto right. So festival by using the bindi search technique, uh, we will compare that if I limit is equal Lukey. So if that is true, then basically we were littered under made index right other ways. So the ending the route ended in sort of ready will be divided in Google parks based one the mid. So I don't think loadem it will be sorted or they make plus one pill high will disorder as we're discussing the approach, right. So if first is a case that if the area looked amid assorted So how we will attack that to check that if any law is less than equal to an image since it is an increasing or you can see non decreasing order off sorting that means if the left part disorder So inform Dr be true the left should be less than equal to enemy. Right? So this is the case. Then we will check that better are key is in the range off very low to very high. So that means we will compare that if it if RG is Britain in Quito at a low and also the key should be less than equal toe Hoffman, right? And that is, uh, me. But they're done, or you can say drink over this part of the area that means from law Do make minus one. Thank you. Otherwise we will be trading over the remaining bottle there. That means make plus one too high. So we'll be returning recurring to that part. So this was our first condition that if our left Parkas sorter, then up, Then way we're recurring, according do the buying the search approach in this particular right. So these two conditions were based on the fact that a low two men is started other ways. If case to is there, that means if I limit plus one to hire sorted, that means no, we can use the Bindi such methods inside intact particular part of the A break. So we will see that if I he is in the range from enemy do any high. So that means if he's Egerton equal to add a man. And also he's lesson equal to every and George Egan say Eddie at the the Last Index that musically high hired High Index, then again are eager for that part of the elite. That means we will recur from it, plus one to it. Otherwise, we will be recurring toe the left part of the area that needs we will liquor from low to mid, minus more. So this was our good for you. Consider Mentor, uh, to search an element in the road. Vigilance sorted A So you got the point how we were how we're using the modified by the search to make use of the fact that begin search inside didn't located and sorted So let's try to run the score So we have a mean method on your pigging Out is something like this So this is our any on we have to search Sochi is six. That means we have to search if he's president, that success, president or north And if it is present, then we will go down the index off this element so in this of this element should build And that's as you can see from this area. But so So we're finally bringing that If I is not equal to minus one. That means if we're not passing any invalid input than in that case, uh, weaken when the index successfully off that ready, you can see what is this? I will be management in two cases if we're past. If ah, we're passing some invalid input advice were not able to find our key inside the particular in this particular ready so that I guess a some point of time during the question calls this low will go beyond this high. So in that particular call we might be didn't we might be having a value returned as minus one. So this minus one say is that either it is a invalid input O r r. He's not from right. So if that is not the case, that means we are getting our key inside this pretty Clary. So in that case, we're bringing the index off that key. Otherwise, if icicle dot minus one, we are bringing that, he is not wrong. So now let's try toe on this or on this program. So, as you can see, the index is coming. Ask, too. That means this particular matter is able to successfully redone the index off this particular key Six in this started and rooted in it. So this is so this was recorded at this point. The three elements are being rotated from here, so this is a rotation sorted out and you are able to get this their day index of this particular he six from this matter. So this is the approach. This is the modified by any such approach which we have discussed to solve this problem 17. Reversing The Array: So let's take a look at the bottom two rivers in any so first approach is the radio a brooch? So inside the tradable brooch, we trade through the alley to reverse and any So then you gotta miss We did the any when the size of the size off the every. So our first er is we initialize a start variable as zero just the first index and the next enviable as this size minus one because the final index or the last index off the area No, in a loop this from the area off start with any off end and jeans. The start and end as start is equal to start plus one and and is equal to n minus one. So this loop continues, Will start is less than and to now let's take a look at an example to Let's see, any is one do cream for Frank And we have to reverse this any using this creative approach . So our start will be initialized as zeal and and will be initial aiders and minus one which is equal do five months minus one that is equal to four, which is the last index so in a loop will start is less than end. It will be shopping the ideal start with the and so first of all, this one will be slab with this element. So this will be five and this will be one. Now the stars will be implemented. The start index will be no one and end with end will be implemented that this that is equal to n minus one So and will be done three No again People swam any off one with any off treat. That means you will be strapped with this full So four will come here and to welcome here No, the start is equal to do and and is now equal toe. So since start is no this condition is no Well, it it that start is less than and since start is equal when people permit outlook here, so tree will remain as it is here. And finally we're able to reverse the elements off the So this waas the creative approach to reverse in any No. The recursive approach to solve for reversing in a is something like this you got, um, the worse takes the any has the first very matter on the second and the third perimeter are the start and the end index off theory, so the START index will be initialized a zero and the index. We'll finish like this size minus one. So as we did in the incredible approach, we will be again trapping the Eddie off. Start with the area of end. But here, instead of implementing and documenting the start, and we will be calling big personally for the rest off the ferry with other updated start and end indexes. So the updated start index will be start Plus one, Andi upgraded and Index for the next goal will be and minus one. Now let's take a look at an example how we will do this. So let's in the again is 123 for infants. So our first step is initially start as zero and end this size minus wanted here and will be for so first of all, we've been slapped and a zero with any full. So that means five will come in the first place. And when will be coming at the last place? No, for the next step, that is. The third step is we will be discussing the constantly calling reverse for the rest of their nanny. So we will be calling Rivers, Eddie. No start will be start blessed one and and will be and minus one. So this will be rivers, Eddie. One going three. So again, inside this be shrugging off. Anyone will be done with any tree That means four will come here and you will come here and again. We will be calling this because some function give us when the updated start and ambles So no, the reverse function will be called Any do go on to and since the loop dominates been started is no longer less than end. So here start is no longer less than n So that means our global dominant here So three will be remaining here hair itself on and there will be no longer recursive Lee The curse of calls for the rest of that very that that means our garden for the records of approach ends here and finally were able to Did I? We can see your able duke finally reverse the elements. So we started from 12345 And now after the end of the garden, being able to find the reverse of the area as fine for 3 to 1. Now we'll get a look at the goods off. Would a cradle on the records of a brute toe make your concepts even clear? 18. Reversing The Array - Iterative Approach: No. In this video you will see the good toe rivers, the elements off in any. So reversing means we just reverse the order off the elements off the area. Right. So to do this, we have a method rivers, any inside our glass, very diverse. So this method reverse Eddie eggs and Ari on X two arguments as well, which are the start and the end off this area. So basically this start and are the indexes starting and the ending in Texas off this night ? So these this is these are the perimeters. I'm inside this method. What we do is we take a biloute. And still, the start index, which is initially equal to zero, is less than the ending index which is equal do and minus one if and is the size of dairy. So starting from this, uh, starting from this condition start is less than end. We dick. We basically strap the starting element and with the element. So if this is, let's say we have a Neddy here. 123456 So, basically we first strapped this, but this that means we still have a one with six, thanks to six comes here and when comes there and after that after the first immigration the implement this stop index on with deployment the an index, that means first the first we have Ah, strap this with this element, the now it still turned to slap this with this element. So five will come here and two will come here knowing again the next situation will be for these these two elements that is part three and four. So in this way we will be as a total will be reversing the elements off theory. So this look, Bill Germany and start is equal to or greater than and so will the start is less than in this group will continue to it to trap D starting and the ending elements all we can see the elements at the started and an index like so this is the method to do the reverse reversal of very so let's try to run this with the help of a simple example. So we haven't area here as you saw from 1 to 6. That means 1234566 elements out there. So festival you just bring the originality so as you can see here for printing were just using a method print area, which is doing nothing but printing. The elements of the area from the Michaela zero will lessen says So let's turn this good. So anything, uh, we can see Not the original area, which is a 123456 is being printed right now. Let's try toe all this method, reverse any and then we will people ending the worst area. So now I'll be intimating this court again. And once I have uncommanded this on, I'll run the score than this reverse. Every function will be called from the starting and the ending indexes. So as you can see, the starting in next will always be zero, and the ending index will be the last index. So since the sizes six last index will be size minus one that is equal to fight. Who will be passing this? We're recording this reverse that emitter with parameters parameters for starting and index as zero and five, respectively. So now let's try to run the score. So, as you can see, the original Eddie was 123456 an hour after reversing off this. Very the reverse, Terry is 654321 So all the air is being reversed. That is the elements off this. Any are printed. Indeed. The was started so were able to reverse the elements off the area successfully. Through this you're setting admitted 19. Reversing The Array - Recursive Approach: we'll see how to reverse a the elements ordinary in a recursive manner. So previously we have discussed to waiter manner in this court we will see how to do that same thing with regard suppression. So to do that, we're again having the Method River steady and inside this diversity were basically taking those same pattern because that is any on the starting and the ending index all very true. First of all, we will check that will start and the end element, or you can sister starting and the ending index are saying or the starting indexes exceeding the end index, then we will simply return from this. Otherwise, if start is less than a good win, then we will strap the element at starting Start Index with the element at the end index. So this is the procedure in orderto strap elements. That is the start with the very end we've taken, take help, offer temporary. We're able to do that. So these three statement basically traps the air. Let's start with this Aryan right? So if you take an example, let's say this is an area 123456 and the start Index zero, that is so any start will be one and then indexes five. So the every five or six. So basically, we're just wrapping this one with six. But this six will come here, and this one will come here, right? No. As you might have noticed in the dreaded manner while we were doing in a regimental, we would just doing this thing in a violent till The start with was less than equal to end . And we were just implementing, start and discriminating. And till the start was less than a. But now, since we have since we are doing this in a Dickerson manner, it just doing this for the first iteration and for the from the next aggression, we're basically calling this matter again with the Abdic start and the end values. So the uptick it startle you will be start plus one. As we were doing their start plus plus darkness start was updating its value s start this one and end was updating its value, their in in their trade. A man. It was Douglas and minus minus. So and was operating it as an minus one. So similarly, here we are just now calling the same function with the updated start and the end values. So this is by which we are achieving the precaution. So we're doing this for the first situation on for the remaining settlement rations. We are skipping this on to do this. We're just doing this, Rick. Arsene. So first situation after that, we're doing this truly caution the rig Ocean will help this to reverse the remaining part of that. Right? So I tried would make this more clear by going through this example. So for the first situation, as you can see, just slap from strap. This one and six to the sixth will come here, and when will come here. Now, the next call will be done with this Sudbury documents to the area which is having elements as 2345 So again, the first and last end eventually be stopped here for this particular call. That means five will be coming here and we will be coming here and again. The curse of call will be made from this particular Saberi with the updated started anybody . So the updated startle You will beat this to Onda bloated and valuable bit three So again in the final Because of gold, these two elements will be stopped, and after that, the course of call will be made as the start element will be. Three on the end element will be, too. No, since the start element will be exceeding that called from the end element. We will build Dunning and finally, are the concept. Calls will be terminated and what we will be getting is did reversed. Eddie, right? So not right. Run this. I'll try to run this logic with the sample example. So first of all, I'll just bring the original area. So as you can see, the original Ari is rendered as follows. That is one between 456 I'll be calling this liquor safe method by in commending this court , I I'll do this, rivers any procedure by a recursive fashion, and then we'll be Brit bringing the They were studied. So after reversing the elements, should be 654321 Right. And we're doing this because of Lee, as we have explained. So I'll be running this corn. So, as you can see, we were steady is 654321 which is actually the reversed order off the original area. And we are doing this to Rick Ocean 20. Missing Number In Array: Let's if you look at other interesting problem finding a missing element. So the problem is you're given a list off and minus one elements in every on, and these numbers are in the range off. One. Do in Now there no duplicates in the list, and one of the numbers is missing. And very that is from the range off one doing. There's a number with just missing inside this very so our task is to find the missing in teacher or the missing number from the area. Let's say our example is any as one do 46738 Yeah, and is equal to eight. Let's see. So the range is one. Do it so as you can clearly see from the rich off one big. The missing element is five so that we have to find out other garden to find. The missing number is something Lindus. So the first element first argument is the early on. The second argument is the size of the daily, So our first in is we'll get the some off all the numbers from one doing since the range waas from one doing the We will calibrate this some off those numbers from one doing so. The formula for calculating the sum of numbers from one doing is equal to end times and plus one by two. So this is a total. In the second step, he's going to subtract all the numbers off the ferry from the story on This is really into do that this after subtraction, whatever will be the desert. That will be the remaining number. So that remaining number is the missing number, which we are finding. So let's take a look at an example to make this fear. So that's er any here is 1 to 4, six, three, seven feet and any here is equal to it. That means the range is from 1 to 8, inclusive off would. So we have to find the missing number in this range from this area. So first of all, we will be calculating the some so that some will be end times and bless. One might be So it is in Quito aid to that means and is 8 to 8 times nine. By do so, this is equal 36 right? So the some which is the good in here, is equal to I directed a study. Their daughter is equal to 36. No, we will go on to subtract all the numbers off this. A lot of this area from the story. So 36 minus one. Let's do less for six plus tree. Let's seven, Missy. So subtracting one means finding of all these together and then finally subtracting from the body. So this is two plus one is three plus 47 7 16 industries 16 plus seven is 23 plus skaters 31. So this is 36 minus 30 and finally result comes as five. Which means this fight is the missing number from the range 1 to 8 in the city. So this was the garden to find the missing number from a given range inside genetic. Now we'll take a look at the code. Plenty of this 21. Missing Number In Array - Code: I will see the good to find missing number from Manetti. So by missing number, we mean that there is a range from one, so that given range on it is a number which is being missing from that particular range. So we have to find that missing number. So we have We're doing that with this matter. Get missing number. So what did this guy get missing Number is doing is it's digging any with the size and and also there is a range being defined. Um, previously or you consider a range is already defined in the problem that, uh that the elements will be strong one to this particular number each. So how is how it is doing is first of all, since we have to find the missing number from the from the given range, that is from numbers one well, the range. So, first of all, we are maintaining a total off all the numbers from one Turing's so given numbers from one toe a party animal range the formula toe find the total is range times range plus one by toe. You might be knowing that some off natural numbers from one to end is en times endless one by two just This is a total or some off all elements from one to the number range. Right. And what we will be doing is since this is the total sum before all the elements and out off these elements from one to range, there is one number we're just missing, which is not present in the body. So if you subtract all the elements off the ferry from this particular total, then finally what we will be getting is the missing number. Okay, so what we're doing is we are degrading over the very on one day when you're subtracting all the elements off their daddy and finally what we will be anything is the missing number . So this total it is doing are missing number and we just Britain, this total let's will be amazing number. Thank. So let's try to run this court with a simple example. So we are digging in Eddie. 12456 Onda, you're tryingto bring the missing number by calling this method get missing number. We're just taking this at a. The size is five. Since the number of elements present in the areas filed on the range is being defined as six. That means numbers will be in the range from 1 to 6. Okay. Andi, my observation. You can see that number, which is missing from the range. 1 to 6 is three. Right. So this particular method get missing. Number here should return us three, which would be interred here. Right to know that struggle on this corn. So as you can see clearly, the missing number in the alley is three. So this is the correct answer. Hence were able to find the missing number from the A in the range off one to a particular number, Ridge. 22. Pair Sum Using Sorting: So in this video we look for a question check for a beer, but given some immunity. So the problem is we're given very off in numbers and under the number X is given to us. So we have to get the money whether or not there exists a pair in the l. A who some is equal, do X. That means we have to find a pair in this area who some is equal to this number X right? For example, we have this very something like this Onda. We have to search for a number. Let's a 16 and find out if there is a pair in this A who Some is equal to 16. They so as you can see hair two elements that is six and then some of you 16. So as a result, we will output like a pair exists in this any who some is equal to 16. So now let's look at what the a brooch to solve this pretty a program. So the first uproot inarguable discuss is using a sorting technique do increase and decrease the some according to our requires him. So we're making use off a sorting techniques So the bottom is something like this that we are taking on a and the size of the area and the some which is the required Cem with We need to find out for appear in this area. So first of all, we will sort the Eddie in Northern decreasing order. Right. So known. Decreasing order means it is the elements can either be equal or they can be in increasing order. That means they ensure that they're known decreasing or right. So we got the first sword deity in nor in decreasing order. So starting the very lex it were given in l. A something like this on that said, we have to find a sum equal do seven. So we will first sort this any in non decreasing order of sorting this and he will make it as 12345 Now, the next step is people initialized to index variables to find the candidate elements in the sort. Larry, that means we will take one first index. It will be initial esto. The left most index, I think, will do zero on. We will initialize the second to the right most index that this Isaac will do at this size minus one. So let's name this as l And this index is up. So by doing the sorting in the study, we can adjust the some off this area. Helpless, very are according to a requires, um So the sorting will help us to lead closer. Or you can say Leach. So don't require Cem between intending to find in this particular area to the require some missed seven. Thank you. So this it gration off this left, and the right index will be then tell this left is less than our. So the left index will tremors in this direction that is from left to right, and the light index recovers from right to left. That is in this direction on this will continue till our left is less than our Where you can see the left index is less than the dike index. They So, first of all, in the first part, we'll see if any off l plus area off our is equal to the required some. So let us conclude that the NFL is one and any off this fight. So the sum is six, which is not equal to seven that means way have not successfully got t quite something quite fair. So other ways we will see as if the L a area that is six is less than some. Then we will implement the left index. So as you can see, some off left and right index, some off the air, elemental left and night in Texas six, which is less than the required some. That means it gives us an indication that we no need toe increase the sun off any helpless area, right? So hardly bill increases. Um, So if you ever still left most this left index from left to right, then you can see easily that we are increasing the values. And if you, uh if you move the right index from right to left, then you can see we're decreasing the values. So here we need to increase the value. Or you can see increase the pier some, since it is less than requires, um so since six is less than required, some we need to increase the person. So that's why people including Bill left index. So in that case, we will see that we do left plus plus right, So left will be No, it rated toe next index. So no left will point to this. So l A will be equal doing otherwise. If area helpless area was greater than some And in that case we had to they remained the person. So to discriminate the person, we will move this ride element from right to left. So that is the sum off. The repair will decrease. So in that case, we will declaim in our from its initial. So ar minus minus will be the operation in that case. Okay, so we will look this way, will it? Right over these and well, for the beers while the left index is less than the right index. So as we were here, so left index was here and writing exercise. Right. So now every left less area is equal to seven. And as you can see, it is equal toe required some. So if any helpless area is equal, required some and rebuild your done one. So here we were done. Yes, You done one. And we will say that this some exists. Some equal to seven exists in the eddy. The beer is two and fight. We're just making that quite some, but in any guests even if we are doing a loop, were looping in this looping while l is less than they are on Once this look Terminix and were not able to find a successful pair than in that case, we will say we will bring that No candidates in this particular whole area are found says that some off any here is equal to the required some. So in that case we would have returned zero. But here, in our example, your successfully found off the two elements which make up a pair toe, make some as seven. Okay, so this is the garden for approach number when it is using sorting Andi using pu index variables to find out the pair which is equal do who some is equal do require some No, we will analyze the time complexity of this particular approach. Since we are doing sorting on this any and for sorting the best The mentally used bottoms armored start quick sort. So the best get best cases for those sorting algorithms is order off in Logan. So and are the elements the order off and log in time is taken to sort this particular unknown decreasing order. So festival. So total time complexity will be ordered off in Logan. Less execution off desperately. So these are constant instructions, so they don't contribute to the whole time. And these are the looping instructions. So this new basically starts from left. This l start from the left most index, and our starts on the right was index. And this, uh, this Dunn's left is less enough. So this proportional this the execution of this look is proportion to the size of the elements of the city. So that will be or rough and to the order time complexity will be on the order and Logan on the space complexity. Or you can say the facilities place which we are taking in this uproot is the space occupied by these took index variables. So that is a constant space. So we can write it US. Order off one. So this is the time complexity order off in Logan. And this is the space complexity it just ordered off. One 23. Pair Sum in Array Using Sorting: to the good to solve this problem. Using the starting technique is as follows. So we are using the static function this has at a book interdict. So this is the method which we will use to return. If the is convening appear, some is equal. Do big wax. Um so in this method were passing nearly the size and required some some. So, with the two index, variables are left for left and right index are in our and the first step Waas. We will sort this area. So first of all, we are using the sort method of the dysfunction. So artistry So that will be ordered off in Logan Starting on after that girl initializing left and right index to zero. And the idea size minus one, respectively. No, we are looking and this left index is less than our and best of one. Here we are comparing if the area helpless area is, it will do required some. Then, in that case, we have found out a spare successfully when we will return True from this matter. So the method has returned. I best bowling so other ways else. If if the sum is less than required some. So if the some off at a helpless area is a lesson to require some and we need to increase the some so increasing this some will only be done when? If when we increased this left pointer or the left index. So we'll be doing the increment off like left index s, l plus Plus. Otherwise, we have to declaim in this, um, so in the else case, that will be any of helpless Any off our is greater than some. In that case, we have to decrease the some off aerial place area. So for that, we will do our minus minus or will shift who minus one So the index will be shipped it to left, right, So this loop will dominate when we're l eyes equal to or greater than not. So when once l gets past this are this little bit of metal. So inside this look we are able to find out the some Then it will do it and do other ways. Finally, if you're if you have, If you have come here on day, look, doesn't didn't drew in any case, the new diligent plots. So this is the sorting technique by using to index pointers. Ellen are too. Search for a peer who some is equal to get quite some sub to Let's run the score. So in this main function you're digging Eddie, or this is the sum somebody go to 16 which were searching in this particular area you have to sign if this has added took any dates, function which is off will indict if dissidents true, then we will see that has toe elements with the given some. Otherwise, we will bring that it doesn't have the two elements with you in some Andi, as you can see by observation that any physical to 16 so six plus 10 is equal to 16 which is equal to the required some. So that means this Harry has two elements which is having that with busy equal toe who some is equal to that given sometimes 16. So this should stay pressure group later. No, that's good. So as you can see, that put us has two elements with even some so successfully were able to that this Eddie is having took any elements who some music will do the required some 24. Pair Sum Using Hashing: Now we will see the second approach to solve this particular problem. In the first approach, we were using sorting techniques to solve the problem so that I'm complexity there was ordered off and Logan and the space complexity or the auxiliary space between were using was order Flynn. So that was constant. So here the approach number two is using hashing pick me. No, What is hashing hashing is basically used in this particular approach. Jake better the some is fine in the particular area or no, so hashing is done by Akiva Loop here insertion on. So we are digging Ah ha's off, Let's see. But a large a large number off this size is very large. And each from this particular index Let's say 012 So this particular and let's have an engine, the location I for this particular boots is the element off the so If it was something like this, then he will be storing the area element on the value like he will be storing the area. Element on the value will be indicating that this particular element has bean reversed or came in the hydration on the Eddie. So this will be denoting the presence off this particular element to If he has this these elements, then it will be something like for the first element. But we will be setting. It s true for the second element to we will be setting the element. What and next do drew similarly for three, similarly for four and similarly for five. So in this particular hash were using the element, the element as the key and the presence off the particular area. Women in the airy as the value. So this is only this hashing basically uses this general key value approach to solve any problem. So we will make use of this hashing techniques to solve our problem. So first of all, we will initialize in him empty hash table. Let's s so we will initialize this whole big Eddie. Oh, you can say you can take any big number which is the size of this particular hash. Just that it incorporates all your any elements. So we will initialize this ham MP hash table with let's say any before value. So before value, we will set us. It is the element is not present. So we will initially will market us. You can use any Gulan value also. So the bullion value for this not present can be false. And for if it is present, you can market. That's true. My sister was India, right? So initially all the in values are filled us false. No. Well, it right over this, Eddie. So for each element a off I in this Ellie, we will see if this some minus the element at this particular location. If this hashes said, or more so why we're taking this. So if, as you can see, we have found out here, that means a will definitely will no be said. But if some minus a is also present in this hash, then we can see that that part of the element that is some minus air less this a so some minus a less e a. So this will be one element. And this will be the second element. The boat, some of together to make some right. Since this minus, they get translated this process. So in that case, we can see that we have successfully found out a pair who's who was some is equal. Do the required something, right? So vile traversing for each element A off by if the encounter the the element at this particular location that is some minus a off my delegation in this hash s. If that is said, then we will bring the pair. Yeah, and someone is it. So that will make us give us a successful player, which some of together to make required some thank you. And if that is not the case, that that means you're not found, we're not able to find these this case. Then we can say for this particular a the some minus is not safe. The some minus the eight index is not said. Then we will just insert this a in do this backlash and even reverse for the remaining elements. So, no, let's, then this approach for a simple eri. So let's say we have an areas 345 to 1 owned are some required. Cem is equal to five. Let's it. So we have to check whether this very has a pair who some is equal to fight. So festival We will take a hash big enough to incorporate all the elements off the city. So, in order to incorporate the elements of this, we will Bullets before simplicity will use only five elements. Its maximum value is five year. So first of all, we will start with the first element. So initially, this hash will be set to false. So all the elements will be set Goals. That means any elements. Presence is not found yet. Okay, So for simplicity, we will market as 12345 Right. So this means this desperate color element location this particular cell, the north that whether one is found ordinary, this is for two. Is it 40? This is before this is fact. They first of all we will see three So three is three we're in conquering. And for three, if the sum if there is there is a pair which makes up to five. Then we will see five minus three. That is to yes to is existing in this particular if to his tooth presence is already set or mood. So it does not sink. So that means we will simply mark the presence off. Three on move, move over to the next. Elements of this area will mark this as true. So trees element, please. Presences, Monk, that's true now for this next element that this for so before again people see. But there's some minus four that this one is settled north. As you can see, one's presence is not set. So that means again we will insert this A. That means for into this particular hashching so forth presence will be marked. That's true, right? We will move again to the remaining elements. So the next element is five like five minus 50 So is your existing in this pretty lady? No, it is not existing that needs. We can simply no mark again. 5 55 presence as true and move again. So the remaining elements now comes to so for two we will see whether five minutes to that is equal to three If three's presence is there or not. Yes, we have found out three teas already Prison. So now we have successfully find out the pair with this contributing to the some fight. And you can print that every eye that is to air equal to do and some minus here. But some was five minus eight days with three. Both are present already so three who will be to be a found or so that will be. In any case, President and three was already present before in this particular hash table. So we can successfully say that two plus three, which is equal to file, are making up a pair, and we will bring two and three as the required here, which make up a some as five. So this is the working of this hashing techniques to solve this problem? No, the time complexity to solve this problem now, since we are reading over the elements once in in this particular luke. So the time complexity will proportion to the number of elements in this particular given every So that'll be or enough and on the space complexity. So space complexity will be contributed by this hash, which were taking initially. So this has is also the number of elements in this hash is also proportional to the number of Hellmann's in this particular area, right? So that will be also order the thing so the space complexity is ordered off in and the time complexity is ordered off in. So, which is in contrast to the previous approach where time complexity was ordered off and Logan and the space complexity was ordered off. One. So both these approaches are efficient approaches to solve this particular problem, and they have a trade off between the time complexity and the space complexity to the first approach was better in terms of the space complexity. But the second approach is better in terms off the I'm complexity. So it is your decision, Mitch particular approach you will take in different circumstances according to the required complexity off the particular problem to solve this thing. 25. Pair Sum in Array using Hashing: So now let's look at the court. It is using the hashing techniques to solve this problem. So we're using this print Players mattered to find out, but then we have a beer who some is equal to requite some. So you're digging at a and the some s department this and first for for simulating a hash using 1/2 sick here off indeed, a date. This hash variable is equal to s We just initial is often do today, Ana. No, we will turn over the elements off the area on for each element Any off I we will look some minus any off I the presence off this m which were initializing as someone is area if it is in this particular in the critical s in this particular hash. So the condensed matter with tell us that if its presence is said or not, So also, this some minus area. So for this to be an index, it should be better than zero. So this is a check for festival that we're not checking for a negative index. The Thames should be really than equal to zero on. We will see if it is contained or its presence is there in the in this hash. So if its presence in isn't is there in the hash, then we can print that bear with the given some. There is some is any off I and someone is area if eider distance so we can print any off I and him as the lead. Quite fair, which contribute to the even some other ways. If these hash doesn't convince that them that it's someone this area, we can simply add this area, So the hash. So this function, Wolf has said, is used to do so. So this is the method which were using to simulate the hashing technique for finding the beer who some is equal to the required some. So now let's run the score. So in this main function, we're using every like this on a again. We are, uh, big and equal to 16 which is the required some. And as you can see, six. Piston is the pair, which is contributing to the required some, so the answer should be six tendon. So now let's turn this court so, as you can see bear with given, some 16 is going on six so as you get since six, presence will be set before 10 presents. So when? When, when will be encountered. So then, in that case, we will bring that in, Uh, that someone honest and that will be six for 10 and six are printed. Lynn six center, right. Since six is getting set or six presences mount before ends present. So when we are leading to this element in, then we're able to find already quite some pair. We just bring two guys and go my six. So this is the Gordon for this clip approach. 26. Peak Element in Array: Now let's discuss another problem. Beat in an edit. So, given any off indigenous, you have to find peak element to it. So by peak element, we mean that an element is speak if it is not smaller than its neighbors, or you can see if it is greater than its neighbors to order the neighbors. Let's say for this element one. The neighbors are seven to 17 the neighbors on 61 and for the corner elements, the that is the sex. And three. There's only one neighbor element as there is no left neighbors on hair, only the right number is there. That a seven on here, Only the left number is there. That is to so these are the neighbors, right? The left and the right neighbor or for the corner elements. Only one neighbor, either right number for the left. So we have to find an element inside. This way we just speak. That means it is greater than its neighbors. So as you can see one off the speak element inside, this Eddie is seven. That means seven is greater than it's both. Whatever its neighbors so similar is greater than six as well s seven is greater than one, so we have to return any off. A big element to dancer here can be seven supposed to fall. We will have a brief explanation off having been going about do come up to an end garden to solve this problem. So let's say to generalize this problem. We have our ideas. So we have any comprised off. Let's see seven elements. So the elements I'm taking in a general manner has a one a two a three a four a five a six on a seven. So there are these seven Elements night and we have written a peak element. That is an element which is greater than it's both neighbors inside this area. So a general or a basic solution which you can think about this just trading over this Eddie and checking for each element by the latest greater than its neighbors. So that is a basic solution, but just off the order off. And so this is an order off in solution, which means just linearly traversing over the Aryan finding by the condition that the only element that is at I it index the A F I is a big element or north just by comparing it with its neighbors. Right, So that is a simple order of in solution. Um, as you can in dude, it will say that that cannot be the optimal solution. So we have to look for an update. My solution to this problem soon this any is a jumble, Larry rate, that is, It's not either sorted or you can see located sorted. But yes, you know, you have to optimize if you have to optimize it. That means you have to make the complexity better than order of him. So better than order, Finn, Some here somehow in some, somehow you have to come up, order off Logan solution. So since only Logan is brighter than and as in general dumps, we see in The New York Times so how he can come up to an order, a flogging solution to this problem. So for first heart, if I just think about buying descends only just, let's say, but not by the exactly buying research. Just by going with the concept off, minus it. If I tried toe, look for the solution to this problem. Then we'll see. What are these circumstances? between in which we are stuck to solve this problem. True going by the buying research. So let's say first up, first of all, we can play the middle element. So middle element is this. If we're so this by the great area that if this middle in mind that is a four, uh, we will check whether it is a big element or not. That means if it is, if a four is greater than a tree on a four is greater than 85. And obviously we will be returning here for as the answer. Right. So this will be around in that guest other ways. Either of this will be false. That means either a tree will be greater than a four. Are this if we will be less than if it they I don't This would be true. Okay, Now comes the tricky part off this solution that if either eight threes bigger than a four or a four is less than if I so first of all, let's consider this case. If it is greater than me for they, then I'm saying that inside the 70 even to a tree there will be big element for sure. So how were coming up with this statement? That with the sea, by general contradiction or you consider general inequality proof, which are a vision to you. So, first of all, if we're going inside this case, that means 80 is get grilled in a four. Right? So there are chances that either a tree is the peak element or some some other element inside this area will be a big element that will show it, show you in something. But how? How is it true? Right. So if it isn't the big element this a tree If it it is the pick element, that means a tree will have to be greater than this eight to also that will, that will make this a tree as the people in thank you. But if it is not the case, then it will be get greater than the three. And we can simply do loud a three from being a peak element. That means it is now no longer a candidate for being a people. So we'll simply to love entry. No, again. The going with a similar approach, we can say it. You can not be a candidate for being a bigot. So if it was definitely a peak element, then a toe will have to be later than even. Thank you. So in that case it will be a bigger. But again, let's say if this if it is not greater than even that means even is greater than it toe. Thank you. And it was again to load for being attended element. And finally, if even is greater than a two and you can see this is the corner most element of the story . And there are no left neighbor off this particular at element and since even is bigger than it, then no, we're sure that if we're ruling out some candidate elements, that is a 382 And finally we are confirming that even will be speak element. In that case, that means either a tree, our it to or even any off the one off out of these three, Ali can say any. This is a general case where we are taking this summary as three size. This can be a general somebody off any size. So in that Sudbury they will be definitely a big element inside this. As you saw with this basic inequality proof. That means if this mood element 84 is less than a tree, then they will be definitely a big element inside the Sudbury, even to a three. Otherwise, if a four is less than a five indefinitely, there will be a peak element inside the summary. If I go in seven sit and the proof is similar as we show short in this so buddy So this means that if a for is not the big element and definitely either, even to a tree, we'll have a big element. If I've a seven will be having a be getting so this I have proved to you know, you must be very clear about this. Uproot that. Do you have to either recur for this? So very Or you have to recur for this Epsom Berry. If a four is not the peak element, and if it before is the big element that definitely you can written from the first comparison itself. So that means this somehow is dreaming like a Bindley such solution. Although it's not a bind research what it assembly some something flaring like up by news. A solution all you can say a solution which is having a time complexity off the order or off order off Logan with discredit. Then this order off. And so this order flogging solution is as we discuss now, we will go forward to like the structure and garden for this problem. Now, as I discuss the basic concept off, uh, the solution to finding the peek inside the city. Now let's try toe right, a structural garden for this. So for this problem, So first of all, we were calculating the middle man. So middle element was calculated as something you can easily calculate as hopeless high by toe. So this will be the middle element. So well, first come check that if the middle element is created and both the neighboring elements, then we will be there. Danny, the murder element, right? That means your main element is a peak element. Otherwise, there will be two cases. Either the middle element will be smaller than the left element, or your middle element will be smaller than the title. Right. So, in the first, the nation is true. That meets the middle element is smaller than the left element. Then, as I prove you earlier that there will be definitely a big element inside the severity low to me, that is, the somebody will be very low to animals. So there will be a guarantee. A speak element will be here in a real Oto elements. So very if the middle element is smaller than the left. Otherwise, if the middle element is smaller than the right element, then definitely there will be a big element in the area. Mid less one, do I. Right? So this is a basic or you can see it, um, structured L Gordon and in my garden as well do find a peak element in the area, so the time complexity will be ordered off long. 27. Peak Element in Array Code: So now let's see the good to find a big element using the divide and conquer. Or you can see the modified by and research approach. So the matter which is doing the final peak element in the Eric is this fine? Particularly. But so inside this big find picketer, we have an edit on a the low and high index. Off this Ellie, you can see the starting and ending index as the as off this area and finally the end as the size of the Senate. Right. So, first of all, we used the modified by any such approach. So first of all, we have toe compute the main element. So a 1,000,000 next with me, Loveless high too, right? So after concluding a mid, But we're doing is looking whether this particle amid element or the element and this money index is a big element on Earth. So how it will be a big element if it is greater than both of its neighbors. Right? So form a mid minus one and mitt plus one index Ah, element with the it's never straight. So, in order to tackle the corner elements as well, we're using this condition that if Mitt is equal to zero, so in that case, they will won't be any left neighbor other ways they are comparing it with the left number . Right? So true, me, we either the Nets let me listening. Left neighbor, Right, I'm And so the coalition should be that it should be legal than it right neighbor as well. That means at eight mid plus one. Also, you can say I didn't make should be getting unequal toe limit plus one. Um, this case can be true for the right most corn element. So in that case, mid will be equal to n minus one, or we can say they will no, be. They will not be any right neighbor for this particular murder element. So in that case, we won't become very this particular mission so that this will be true permit or this will patrol. And also in the escalation I that this would be true for this rebuttal on this whole this whole condition with this whole condition, these would conditions should hold true. And in that guest we will visit a neat mint. True, if that is the case, we will be returning met other ways if it's left, neighbor is greater than it. Uh, this middleman and we are sure that we're finding way. We'll be finding a big element in the left part off this in the left part of the area. Right. So the part from low to mid minus one and to have a check that we have Ah ah, left neighbor. So that made to be greater than zero right in or do have a left name. So in that case, we are recurring for made low to mid minus one. That means the left part off the left. How viral theory, right? Other ways. If the right neighbor is greater than the mid mid element, right, that means any meat plus one will bigger than element. Then, in that case, we're recurring for the make plus one too high part off the That means we're recurring to the right. That's right. Half fire off the tree. And finally, we are your dining the and next which is being written by this pretty Lefcourt right to this one. This is our method which is using the mortified by any such mentor do come up to the solution to find the people in my to let's strike around the school to inside main function . We have any as the this and finally Ah, well, Brandy, the next off index of the big point where you can see the peak element is the return value from this fine matter. So fine peak Mother is basically calling Ah util matter which which we have discussed buying pick Util and said this Frank pick you till we're passing the law and the high and next husband Right? So lower Lauren. Hi, Index. Why we're passing We're passing in order to calculate this Thank you. So index off the big point So any big point inside this particular area will be printed. So this 20 as you can see, is a peak element. Why? Because it is greater than it, both of its neighbors. That means it's left neighbor as well. It's as well as it's right number like so let's or I don't run the score to We saw that 20 is the big element. So we are. You were turning the index of the peak animal. So in that Zatopek element will be 012 So you should be dancer and as you can see. The result is index off a big point this toe which verifies that it is successfully our method find big. It'll Oregon to the fine. Big matter is successfully your dunning big element or theory. 28. Element Occurring Once in The Array: No, we'll discuss a question. Find the number which appears once in every. So the problem statement is given Adolf in beaches, all numbers in that very occurred voice except one number. It's the first ones we have to Fine. That number may just occurring. Loans in there get it. So for example Ah, this isn't Eddie. And in this edit, all the numbers have their frequency as to where you can say they're occurring twice in this day except one number. Not this one, which is a great once inanity. So our results should be one that means the number which is occurring only once in this area. Ladies. So what will be there? What will be our first approach to solve this problem? So the basic or the name approach to solve this problem is by using a hash table to let's discuss deal garden. So So this with a basic approach. So we have an Al Gore Tamir certain single number as its name. The method name is such a single number, and we're taking at it as the input. So the first step will be we will rivers through the any Andi put the elements in the hash table. Thank you. So, element basically in a hash table. We have a key value pair, right? So for making G so he each and every key is being map to look pretty global. You inside a hash table. So what? We will do well to make use off a hospitable living Make the element or the number as the G on X count or the frequency as expel you. Right. So what will happen is when a vendor will it great through the any. All the elements except one element, will have ah, the value as to that means all off the elements will have the pound or the frequency has to accept one element which will be having it's gone Does but thank you to after women in this hash table, we will rivers The added again you are you can see begin There was either this hash table or the Onda We can see better. The element which we are on which we are, is having a phone has having found as one in their hash table. So, while trading through this hash table or reconsidered through the area, we can say we can see which elements count basically has the value s one adaptable will be our answer. So this is the basic approach by using a hospitable to decide the element which is having its Kaunda's one, or you can say the number which is occurring once in an attic. So this is the basic approach? No, in this approach, the time taken or the time complexity will be order off. And since we're traversing through this any so since we were traversing place. So that will be price afford run, which is also ordered often. So I'm complexities basically order of him. But here, since we're using a hostile right on in the hospital, were basically storing all the elements. So the space complexity also is in the picture. That means we're taking extra space. So the space complexity. So we have. We're taking an auxiliary space or an extra space just to the additional space complexity off this approach becomes or driven. So we're taking extra order off in space to solve this problem. So this this is the complexity analysis that the time complexity or the time taken do, uh So this problem is off the order off and on this base complexity or the extra space which we are taking you. So this problem is also order. And so this is the basic approach. No. We will dive deep to discuss a better approach, or you can say a more optimized approach than this. 29. Element Occurring Once using XOR: another better approach which we will use here is by using the Zohar operator. So those are is made by the excuse of or great so as you as you might be doing that Zor off a number with itself is zero. That means if we have a number X and resorted with itself, then the reserve will be zero. So this is the first idea behind this approach. So the first idea is a numbers or with itself will deserted Z break through. The second idea is Saar off a number zero is number. Except that means if these are excellent zero the reserve will be accepted. So these are the two basic principles are basic concepts offices are this we will be using inside Anna inside out approach, right. So we will use this approach to give us the number which is having a single occurrence in already No, we will discuss the just now discussed the czar approach to solve this problem. So let's say we are given the as seven three five four fight three for right So this is a very which we have which were just given to us on. Do we have to calculate the element which is having its frequency as one. Or we can say that the which element is occurring once in a very and apart from that, all other elements are occurring twice in the same right. True how the czar operator will come in the picture. So now, considering this example, we first try to do Zaroff all the elements inside the say so let us take up valuable as a result or s so less will be equal to 70 or three zor five czar for these are five zor three is our four, right? So basically, we're doing Czar of all the elements first. Now why I'm doing this only I'll be explaining you in a while. So doing this is our is helpful to us by because first of all, these are property of the sore operator is associative associate By associative we mean that if we a plane, let's it. These are three variables. Yeah, bless me. Pussy. So this is Samos doing a plus B in parenthesis. Let's see. Right? So basically, we are associating in once, uh, in one on the Left Insight. We are associating these two operators or you can see these two variables on the inside. In the artist we're using these two Grable's so basically associate associative property state this and those are property is or operator is communicative as well. That means you can see a operator. Me is equal to me, Operator. So by this what do you mean? It's from here This thing this is, er can we re suffered so that dip airs from like this. This tree is making a pair with this tree. This fire is making a pair with this pipe and this four is making a bear with this for so by the property off associative nous on the communicative nous we can rearrange or shuffle this particular operation or this result do something like this so our desert can be read it and as seven Zohar in Brandis is trees or three exhorted in Brantas is floors are four zor with in parenthesis, fives or fight. So by making use off the associative and communicated property off the sore operator, we are we have come to this particular reason they now, as you might be remembering that I discussed two important principles or disorder operator . So first waas if we do a sore off a number like the X zero that will be resulting in X on. And secondly, if we do, these are the number X with X itself not really deserted zero right that are making use of these two principles. Little League use this result flow better. Are you gonna You can say to a simpler dessert. Not since X or X is you. So by making use of these pairs threes, artery will be zero fours or for a game a zero and five for five. Again busy Thank you and zeros are zero again will be zero. So this thing will be zero and 000 will again be zero. So this whole thing from this zero, they finally there. Ah way have arrived. Toe sevens are 07 or zero. According to this property will be seven night. So we are finally left behind with seven. They're doing the czar of this whole and elements they left behind with seven. And what is 77 is the element which is happening only once in any early on. Apart from the seven every other element, like safe 354 These all elements are occurring person to say That means if you're doing zone off all the elements the elements which are coming in pair well, you can see him placing in Eddie. That will be ah many fight or by doing czar on the element it is occurring. Only bunten in every is being left behind right to the desert finally is the element which is arguing only once in a A. 30. Element Occurring Once using XOR - Code: so no good to solve this problem back. We have to find the any element that appears. Only ones is something like this. So inside this good, we have a yes, but in which we have a matter as find single, which will be doing our past to find element which is having ah, single occurrence inside this matter. We're taking a honey on the other side. So as we know them Documents approaches, being zaroff all the elements. Well, it was the element which is having the, uh, frequency as one. Right. So what we're doing is we're taking a result variable in Grizzard on we initializing it with the first element. So basically, we have to take the door off all than elements. So we're starting this, or you can see we get We're starting to initialize this over evil with the first element that this any off seal, right? So we're starting with equal one until the last element. We are a trading with the area and we are zoning all the elements on we're capturing the result each time in this rest element. So finally, after exiting through this loop, the risk element will be containing the sort of all the elements. Right? So you are off All the elements, as I explained earlier, is the element which is happy frequency as one. All you can say element, which is agreement on Hebron seminary. Right? So the rest will be storing a result itself Finally, after exiting through this movie. So what? We lose your ability turning this. But even so, to run this example, we are taking a simple at it inside out of mean function on the the very is something like this. So this is to three, five, 45 feet four. So you're gonna see the element too is occurring only ban seminary. And apart from this all the elements that is three for five degree points in this area. So the were pointing in this calling this function on printing the desert as element of doing one's is we're done value of this variable That should be do right since two is a appearing on it Once an enemy thank let's run this good So as you can clearly see on the right side that element occurring ones is do so This verifies that this is our approach basically is very fast or you can see optimized on you. Concede the time complexity is also order off. N on the space Complexity is constant that this it is our roof when we're not taking any extra space to solve this problem. So this is the most optimized approach to solve the problem off finding an element that appears when he wants. 31. Leader In Array: leader in in any So problem is we have to find all the leaders in any. So an element is a leader. If it is greater than all the elements to its like, say, and as you can obviously say, that the right most element will always be a leader since it does not have any elements to director. True, next in this is this particular very for is their example. In this particular, any leaders are 34 since 34 is greater than all the elements to it straight. Say so the next this 23. Similarly, as you can see, it just created and all the elements to this right side Well is a leader again, since it is greater than three, which is the elements which are the elements to strike, sir, On the right, most element is all this a leader. So we have to bring these leaders in the Nettie. So the approach number one is the neighbor brooch or the basic approach to solve this problem? No, as you can see from the language of the problem. So the leader is the element which is greater than all the right most elements off that particular element. So the basic approach will be we will. A drink from Isaac will do zero elder, second last element that is size minus one off diary. And we'll pick this element and store it in a very well X. And then we will trade from Jay Sigel. Do I plus one pill the size of dairy? This these elements will be the elements which far to the right side off that a little air element our area off I. So we will compare all of these elements with the X that is equal. Do any if I And if it's if X is greater than all the elements from Michael one to size, then we can see successfully that X is a leader, right? So for the basic example, we dig the areas 16 like 2 34 It then 23 nine and three. No, we will pick up each element and use this approach this basic approach to find out the answers. So pick up one and we will see if one is greater than all the elements off to the right side. No, it is not so. When will not be the leader So when is not the leader? No, we'll see. Six a six, president old elements Or do the right say no. It is also Northern Leader now coming to 34 34. You can see it is bigger than all the elements Fruits right side. So that means 24 successfully. We can say that it is a leader similarly No, we will go for eight and see if it is greater than all the elements. Stewart Straight say it is not so. It is also not a leader. No, Corbyn, This is also not a leader. So 10 year old son or the leader? Trinity? Yes, it is really an old elements from strike side. So Eternity is also a leader in the night. No, it is not since you a little later than nine. So nine is not a leader. Well is the leader since since it is greater than all the limits would strike side. So where is the leader and treat by the false? In this sense, it is a right most element, certainly with the leader. So four leaders are 34 going to three. And to so this is the basic approach the outer look to pick up an element on the inner loop to compare that picked up element with all these elements. So this is the basic approach on the time complexity do this for this approach is ordered off and square, since the first loop is all the order end and the second look. This is the inner look inside this particular loop so that will also be are off the order and enzymes and will be off order and square on the space complexity. Is your play space. We're not taking any extra space. We're just taking a variable X. So the orange auxiliary space, the space complexity for those less space will be or did Flynn. So I just So this is the time, complexity and a space complexity for this particular approach. No, we will see a better approach to solve this problem. So in the previous approach we were just using two loops and that was a basic solution. The basic and do the solution to find out all the leaders all that. But this solution is opting my solution than the order off and score solution which will be previously discussed. So this approach works in a little smart way. So let's take help of an example. So we will make you the same example that you were taking in the previous approach soda. And it is one six. But before eight, then 23 nine. Well, entry. No, the elements but a leader are those elements which are greater than all the elements goods today say so rather than releasing from the left side, if you can think that, then be rivers from this light side left side. So whenever the encounter a new maximum element that means first of all, this tree is the maximum element. So then the maximum element that you have encountered so far will be the leader. If the maximum element gets updated, then it will be a new leader. So, for example, this three first of all will be leader. So first leader will be. So I'll make a list of leader. So first with victory, no evil go from this. But look at this particular element to this particular element. So we're traversing from right to left. And as you can see, the on the maximum element initially was three. No, it is updated toe. That means this will definitely be greater than all the elements goods, Right? Say, since it is updating the maximum millions from encountered from right to live, so you can think of it that may never The maximum element get gets updated by traversing from right to left. So that Max element that element which is getting Max, as in Max updated so that critical element will also be a leader. So as fun is abating the max they since 12 is greater than three days updating the max so well, we'll also go in the leader list certainly will be a leader. Similarly, we will goto the next location, so that will be nine. But here nine is not a bleeding the max. That means this nine is not greater than all the elements. Good, great. Say right. So this nine is northern, not a leader. Similarly, we go for goto, go for 23 23 is updating the max since 33 is ready, then quit. So that means it is also greater than all the elements would trade said. And still no, this is the maximum element. So the country will again go to the leader list so similarly, this approach will garden Will Burke and we will see that No. 34 will update the max and the next leader will be 34. And after this, the maximum would be updated any further. So there will not be any more leaders. So we are able to successfully find the leaders of the Senate. The just one thing which we are compromising in this approach is that we're printing all the leaders from right to left they So if you are if you if you want to play it all the leaders from left to right, you can simply reverse this list and Brenda leader list. So I'm making a list off this leader, leaders and printing and right to left fashion here. So if you want, you can bring from left to right fashion as well. So that will make up to the require a post that if you are allowed or if you're required to print in the left right fashion, then you can do in that case also right. So deal Garden waas The first step Waas we're here scanning from right So we're scaring from right to left. Andi, we're keeping track off the maximum element form so far in the let's say, an IMAX variable. So Max so far is the variable, which we're taking the cell garden to keep track of the maxim element. So if the next element which were encountering while traversing from right to left, is greater than the Mets so far that needs the maxim for is getting updated. In that case, that particular element is a leader on. We're adding that element to the leader list. So this is the North, um, to song. Or you can see a better approach to solve this pretty good problem and how it is a better approach, since we're just to everything from right to left off this area on leave ones. So that means that I'm complexity will just be ordered off. And since we're traversing only once in the city, and as you can say, space complexity again is just for our taking. Available to the axillary space, which we are digging is sort of one there this constant. So as you can say, this approach is far better than the name approach, which were discussed in the previous video with the time complexity was ordered off and square. No, here. The time complexity is far better. It is old off. And so this approach is the most optimized approach to solve this particular problem. 32. Leaders In Array Code: So the good to the solution is the basic mentored. Uh, those are solution. Is this spring cleaners? So the print really users? It's the every on the other side. So as you know, we Trevor's from the right to left side on, we keep track off the maximum element. So we keep track in this maximum right element for the maximum element encountered so far from the cooler courage. So festival we initialize it as the last element of the area. And as you can see be individually you can observe that the last element, the right more Simon will will be a leader on this so well, firstly, spending the next room right, which is the trade muscleman. So that will be a little no. We trade from the second last element in the first element from rightly love fashion And in any case, if being Gunnel, that the in country element every I is really than the next from right or the max Ah, the the next which is in so far. In that case, we updated the max from right as area and we think that makes from right so that will be a leader. Toby rivers. This look from size minus 22 I equals zero and we never being counter a max element. Or you can say, whenever we update the max from right, you plant it. Andi, no wonder the next next time. So this is so this ingenious. It is sending all the leaders in this way to get it. No, to run the score inside. I mean, function we're taking and any Onda in this area. We're taking these six elements. And finally you're passing the Is Eddie on the size of this Any to this pretty girl? Apparently dysfunction which were discussed previously? No. Let's in this court to the leaders off this, Eddie, Uh, as you can see, the two, five and 17 since you're traversing from the right collaboration. So the leaders are printed in my pullup fashion here. So to five and 17 as you all took and find out from this Eddie, are the leaders off theory