What is the logic behind this?

what is the logic behind this.plz explain me

Hi @garganshul151
The solution to this algorithm will require 3 pointers to iterate throughout the array, swapping the necessary elements.

(1) Create a low pointer at the beginning of the array and a high pointer at the end of the array.
(2) Create a mid pointer that starts at the beginning of the array and iterates through each element.
(3) If the element at arr[mid] is a 2, then swap arr[mid] and arr[high] and decrease the high pointer by 1.
(4) If the element at arr[mid] is a 0, then swap arr[mid] and arr[low] and increase the low and mid pointers by 1.
(5) If the element at arr[mid] is a 1, don’t swap anything and just increase the mid pointer by 1.

the thing which u have said i have seen that in the video only…i am asking what is the logic

Hi @garganshul151
Can you be more specific about logic which you are asking ? I mean what exactly you need to know

I want to know how to think about certain questions

Hi @garganshul151
See in the starting you will face difficulties in thinking about the solution but as soon as you solve more and more questions, you will get idea how to think.