Each technique has some requirements. If you feel that the question's constraints satisfy the requirements, you can try to use it. The way we have solved this problem here is similar to sliding windows but not an exact implementation of it. You must have figured out that we want to make the i and j pointers as far apart as possible so we are trying to move it right as much as possible. Whenever it contradicts our constraint we try to change the left pointer i and repeat the same process. Honestly, when it comes to choosing the right technique for a particular question it is more about intuition which comes with practicing questions. I can tell you why it can be solved with a sliding window but the identification part is something that comes with practice. You will be able to relate a new problem to older problems that you have solved when you solve a lot of them.
Each technique has some requirements. If you feel that the question's constraints satisfy the requirements, you can try to use it. The way we have solved this problem here is similar to sliding windows but not an exact implementation of it. You must have figured out that we want to make the i and j pointers as far apart as possible so we are trying to move it right as much as possible. Whenever it contradicts our constraint we try to change the left pointer i and repeat the same process. Honestly, when it comes to choosing the right technique for a particular question it is more about intuition which comes with practicing questions. I can tell you why it can be solved with a sliding window but the identification part is something that comes with practice. You will be able to relate a new problem to older problems that you have solved when you solve a lot of them.
For example in this question sliding windows works in O(N) and the method we are using to check whether the conditions are satisfied or not is working in constant time. If the checking part was also O(N) then we could not have solved this question this way and the overall complexity would have been O(N ^ 2). This is what I meant by saying that we need to check whether our constraints allow us to do so or not.
@@cpwithcpp i have a doubt ,i applied merge sort and at mergig function i added the required conditon, why it doesn't work there as I have seen similar types of questions done with this logoc,also please let me know whci qs can me done with merge sort code.
Great explaination. It was hard to find video with simple explanation. Finally found this. Keep posting.
excellent explanation sir
please help with how to know it can be solved with sliding window technique
Each technique has some requirements. If you feel that the question's constraints satisfy the requirements, you can try to use it.
The way we have solved this problem here is similar to sliding windows but not an exact implementation of it. You must have figured out that we want to make the i and j pointers as far apart as possible so we are trying to move it right as much as possible. Whenever it contradicts our constraint we try to change the left pointer i and repeat the same process.
Honestly, when it comes to choosing the right technique for a particular question it is more about intuition which comes with practicing questions. I can tell you why it can be solved with a sliding window but the identification part is something that comes with practice. You will be able to relate a new problem to older problems that you have solved when you solve a lot of them.
@@cpwithcppThankyou for the advice!
We take j-i as our answer but it may be possible that arr[J]
Yes you are correct and I have discussed the same example at 10:22
how can we identify that it can be solved with sliding window ?
+1
Each technique has some requirements. If you feel that the question's constraints satisfy the requirements, you can try to use it.
The way we have solved this problem here is similar to sliding windows but not an exact implementation of it. You must have figured out that we want to make the i and j pointers as far apart as possible so we are trying to move it right as much as possible. Whenever it contradicts our constraint we try to change the left pointer i and repeat the same process.
Honestly, when it comes to choosing the right technique for a particular question it is more about intuition which comes with practicing questions. I can tell you why it can be solved with a sliding window but the identification part is something that comes with practice. You will be able to relate a new problem to older problems that you have solved when you solve a lot of them.
@@cpwithcpp ok got it one doubt but constraint part plz eloborate that how we can connect contraints with sliding window technique?
For example in this question sliding windows works in O(N) and the method we are using to check whether the conditions are satisfied or not is working in constant time. If the checking part was also O(N) then we could not have solved this question this way and the overall complexity would have been O(N ^ 2). This is what I meant by saying that we need to check whether our constraints allow us to do so or not.
@@cpwithcpp ok thx
the problem statement was very confusing and also very short, this problem should be labelled as Hard 😐
It's helpful 🎉thanks sir but I still don't understand.
chad
Bhai pls do dry runs taking examples
Sure will keep that in mind.
@@cpwithcpp i have a doubt ,i applied merge sort and at mergig function i added the required conditon, why it doesn't work there as I have seen similar types of questions done with this logoc,also please let me know whci qs can me done with merge sort code.
@@cpwithcpp yes plz in the next video plz do dry run with at least one example