@@karan150191 This might be too late, but I think one possible explanation is that the time of execution of the while loop is not related to the actual length of the array. When talking about o(n), we are saying if we take n as the length of the array, the general time execution is o(n); the while loop condition zeroCount > k is relative to the number of zero count & k, which can be trivial when met with an arbitrary sized array. But I can be wrong, I have just been taught this way. (Or maybe its just amortized O(n).)
you can do this code instead, for me it was much easier: int last0 = -1; int left, right, max; for (left = right = max = 0; right < n; ++i) { if (arr[right] == 0) { if (left
This may be a bit late, but that is the comparison between the previous longest sequence and then the current length of the sequence (distance from pointer 1 to pointer 2 + 1 to get the length)
thanks for the clear explanation,now I understood how to implement sliding window technique in linear time
Great to hear!
Hi Can you explain how is this linear time? I see a while loop inside the for loop which makes it Quadratic time.?
@@karan150191 This might be too late, but I think one possible explanation is that the time of execution of the while loop is not related to the actual length of the array. When talking about o(n), we are saying if we take n as the length of the array, the general time execution is o(n); the while loop condition zeroCount > k is relative to the number of zero count & k, which can be trivial when met with an arbitrary sized array. But I can be wrong, I have just been taught this way. (Or maybe its just amortized O(n).)
but one thing please can you explain how we can return that
index that we made 0 to 1 .
you can do this code instead, for me it was much easier:
int last0 = -1;
int left, right, max;
for (left = right = max = 0; right < n; ++i) {
if (arr[right] == 0) {
if (left
Great code.
Thanks!
Hi, Thank you for the video, can you please explain why you did end-start+1(why +1) in the last line of the loop?
This may be a bit late, but that is the comparison between the previous longest sequence and then the current length of the sequence (distance from pointer 1 to pointer 2 + 1 to get the length)
good one..very easy to understand.
Glad to hear that, Thank you for your comment.
Nice explanation....
Thank you
The code is not returning the correct result for this input "1110" k = 1 please check this case
It is not working for input 11100 and 01, could you please check once
The point of two pointer is to improve performance! Your Algorithm yields O(n2) time complexity!
It is not O(n^2).
@@ProgrammingTutorials1M Could you please explain how it is not O(n2)? Thank you.
@@abulhashemjr8999 because K is constant. It is O(n + k).
Question link to solve ?
It's a leetcode problem.
Nice explanation 👌 but in while loop this should be the condition-> zeroCount>=k
Got it☺ thanks!!!
Thank you
For this case, the code is going to fail. 111000.
If the input is [1,0,0], I think this solution will give 2. But the expected result would be 1?
One flip is allowed so answer would be 2.
@@ProgrammingTutorials1M you are right, my bad. This question is about flipping and not swapping. So the answer should be 2 as you mentioned.
nice
Thank you very much.
class Solution {
public:
inline int findMaxConsecutiveOnes(vector& nums) {
int ans=INT_MIN;
int n=nums.size();
int temp=0;
if(nums.size()==1 && nums[0]==1) return 1;
if(nums.size()==1 && nums[0]==0) return 0;
for(int i=0;i
i did not get proper concept
What's your doubt?