Initially i thought i can not solve this problem. But applying brute force, i had solved it on first try. Then I came to your video. Thanks for the optimized approach ❤
@swastikakundu841811 күн бұрын
same
@codestorywithMIK11 күн бұрын
❤️🙏
@dhruvchopra2611 күн бұрын
Tag krne waalo me se Ek Success story meri thi mik bhaiya Knight bn gya mai😁😁Thank you so much for your awesome explanations!!
@gui-codes11 күн бұрын
congrats man.
@dhruvchopra2611 күн бұрын
@gui-codes Thanks broo
@universalcosmologist367511 күн бұрын
bhai mai to 1 ya 2 hi solve kar paata hun contest me pls help
@dhruvchopra2611 күн бұрын
@@universalcosmologist3675 Koi ni bhai Lage rho dheere dheere hojayga
@069_Souvik11 күн бұрын
@@dhruvchopra26Congrats bro...
@pankaj_kumar_malik11 күн бұрын
Able to solve in my own, Intuition apne ap banne laga bhaiya... MIK magic✨❤
@codestorywithMIK11 күн бұрын
Way to go ❤️
@pankaj_kumar_malik11 күн бұрын
@@codestorywithMIK ❤🙌
@Ankitpal-y6h11 күн бұрын
keep doing such a kind of series (if possible contest discussion as well),god bless you good health and life .
@codestorywithMIK11 күн бұрын
Means a lot ❤️🙏
@bd1a057311 күн бұрын
Solved the problem in n*k TC but still not satisfied. SO watching your video now😁😁😁
@madmaxgaming586411 күн бұрын
loved your optimised approach ♥
@VineetDixit11 күн бұрын
i solved it using the optimized method,thanks to your previous videos
@codestorywithMIK11 күн бұрын
Great 👍
@nishantdehariya576911 күн бұрын
to not support brute force the length of array should be >= 10^5, great problem
@ugcwithaddi11 күн бұрын
Was able to solve 👌🏻
@mohammadaftabansari688211 күн бұрын
Thanks for the videos.
@Engineering.Wallah11 күн бұрын
Unique class Solution { public: vector resultsArray(vector& nums, int k) { int n=nums.size(); vectorans(n-k+1,-1); int i=0,j=0; while(j0 && nums[j]-nums[j-1]!=1){ i=j; } while(ik){ i++; } if(j-i+1==k) ans[j-k+1]=nums[j]; j++; } return ans; } };
@vedanthbaliga768611 күн бұрын
Solved before the video using same concept!
@amanpaliwalvlogs686011 күн бұрын
Radhe Radhe ❤
@shivaye0811 күн бұрын
Chalo bhaiya Aaj logic toh ban gaya tha but kuch mistake thi magar you me or saath saath aur acchi practice se bhi mistake bhi aana band ho jayegi ❤
@codestorywithMIK11 күн бұрын
Way to go ❤️
@piyush0mandloi11 күн бұрын
thank you bhiay
@kaat666311 күн бұрын
class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); vector ans(n-k+1); for(int i = 0 ; i
@pranildhutraj603811 күн бұрын
class Solution { public: vector resultsArray(vector& nums, int k) { vector ans; // [1,2,3,4,3,2,5], k = 3 vector temp(nums.size(), 1); // 1 2 3 4 1 1 2 for (int i = 1; i < nums.size(); ++i) { if (nums[i] == nums[i - 1] + 1) { temp[i] = temp[i - 1] + 1; } } int currMax = INT_MIN; for (int i = 0; i < k; ++i) { currMax = max(currMax, nums[i]); } int i = 0, j = k - 1; while (j < nums.size()) { if (temp[i] + (k - 1) != temp[j]) { ans.push_back(-1); } else { ans.push_back(currMax); } if (currMax == nums[i]) { currMax = INT_MIN; } if (j + 1 < nums.size()) { currMax = max(currMax, nums[j + 1]); } i++, j++; } return ans; } }; This is Accepted Solution for problem. Can someone suggest me any improvements in the code quality
@gui-codes11 күн бұрын
can be done in one loop also. class Solution { public int[] resultsArray(int[] nums, int k) { int n=nums.length; int[] res=new int[n-k+1]; Arrays.fill(res,-1); int counter=1; for(int i=0;i0 && nums[i]==nums[i-1]+1){ counter++; } else{ counter=1; } if(counter>=k){ res[i+1-k]=nums[i]; } } return res; } }
@harleenkaur854011 күн бұрын
pls solve using monotonic queue as well
@codestorywithMIK11 күн бұрын
kzbin.info/www/bejne/banOdHRuZc1ol5Ysi=BTZmLnId5xMES3gg Hope this helps ❤️🙏
@harleenkaur854011 күн бұрын
@codestorywithMIK thanks a lot ❤
@mohammadaftabansari688211 күн бұрын
@@codestorywithMIK Much appreciated.
@AS-gf3ci11 күн бұрын
Brute-Force : T.C --> O(n^2) || S.C --> O(1) class Solution { int checkSorted(vector &nums, int idx, int k) { for(int i = idx; i < (idx + k - 1); i++) { if(nums[i+1] != (nums[i] + 1)) return -1; } return nums[idx + k - 1]; } public: vector resultsArray(vector& nums, int k) { int n = nums.size(); vector ans; for(int i = 0; i
@rickdutta94211 күн бұрын
you can easily solve in O(n)
@AS-gf3ci11 күн бұрын
@@rickdutta942 Yeah, but this approach is just for intuition building for beginners. 1st approach to discuss in the interview.
@rickdutta94211 күн бұрын
@@AS-gf3ci Agreed.
@gui-codes11 күн бұрын
@@AS-gf3ci yes bro agree.
@hellsterkun876411 күн бұрын
Easy Question Tha Aaj Ka. Was able to solve it on my own. 100% faster. class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); vector results(n - k + 1, -1); int j = 0; while (j < n - k + 1) { int i = j; bool isValid = true; while (i < j + k - 1) { if (nums[i] < nums[i + 1] && nums[i + 1] == nums[i] + 1) { i++; } else { isValid = false; break; } } if (isValid) { results[j] = nums[j + k - 1]; } j++; } return results; } };
@codestorywithMIK11 күн бұрын
Awesome♥️
@rickdutta94211 күн бұрын
TC: O(n), SC: O(1) class Solution { public: vector resultsArray(vector& nums, int k) { int power = 0, prevNum = INT_MAX, n = nums.size(); vector ans; for (int i = 0; i < n; i++) { if (nums[i] (prevNum + 1)) power = 1; else power++; if (i + 1 >= k && power != k) ans.push_back(-1); if(power == k) { ans.push_back(nums[i]); power--; } prevNum = nums[i]; } return ans; } }; //Brute force
@tauheedahmed407311 күн бұрын
I have done till sliding but when i saw this count was like aha...
bhaiya can you please make a video for post contest solution
@aizad786iqbal9 күн бұрын
how is it n-k+1 ??
@gui-codes9 күн бұрын
The size of the result array is ( n - k + 1) because you are calculating the power of a subarray of length k for every possible starting position in the array nums where a subarray of length k can fit. Here’s a detailed explanation: To determine the number of subarrays of length k, consider: - The first subarray starts at index 0 and ends at index k - 1 - The second subarray starts at index 1 and ends at index k - ... - The last subarray starts at index n - k and ends at index n - 1 You can't start next subarray from n-k+1 because it will not make a subarray of size k as it will go out of bound. This gives you a total of n-k+1 subarrays. Why n - k + 1 ??? To fit a subarray of size k - The starting index of the subarray, i, can range from 0 to n - k - Therefore, there are n - k + 1 possible values of i corresponding to the number of subarrays you can form.
@ArnabBhadra0211 күн бұрын
at first glance its too much difficult to think this otimized version
@Resham29811 күн бұрын
pls tell me why we set consecutive count to 1 when a non consecutive element is seen
@codestorywithMIK11 күн бұрын
Because when you are at jth element, it’s one element and we keep its count as 1 and we expect that in future we will see more consecutive elements to this jth element
@Resham29811 күн бұрын
@codestorywithMIK thankyou bhaiya understood
@AlgoUniverse0111 күн бұрын
Please use a black screen instead of a white one, as the high contrast of white is uncomfortable for the eyes.
@codestorywithMIK11 күн бұрын
Sure thing. Actually I use black screen only. Actually I am currently travelling and it slipped my mind to use black screen.
@vidhut-rau11 күн бұрын
Why 1st window is preprocessed ??
@codestorywithMIK11 күн бұрын
You can choose to not preprocess it. I did it for getting a count value before hand. No need to preprocess. Check in the comments, someone has posted a code without preprocessing ❤️
@dayashankarlakhotia494311 күн бұрын
Brute forces public int[]resultsArray(int[]nums,int k){ int n=nums.length; int[]ans=new int[n-k+1]; for(int i=0;i
@vidhut-rau11 күн бұрын
Not able to do own my own feeling low...
@tauheedahmed407311 күн бұрын
How long have you been dng bro
@vidhut-rau11 күн бұрын
@@tauheedahmed4073 from may 2024 started Dsa
@codestorywithMIK11 күн бұрын
Don’t feel low. This is the process of learning. Such phase will always come. With consistency, things will improve slowly but definitely ❤️
@Playvish11 күн бұрын
finally after 3 wrong submissions I've successfully solved it in O(n) runtime and beats 100%. Feeling happy Thanks to you #MIK bhaiya here is my solution class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); if (n == 1) return {nums[0]}; int f = -1; vector ans(n - k + 1, -1); for (int i = 1; i < k; i++) { if (nums[i] != nums[i - 1]+1) { f = (i - 1); } } if (f == (-1)) ans[0] = nums[k - 1]; int i = 1; for (int j = k; j < n; j++) { if (k == 1) { ans[j - k + 1] = nums[j]; } else { if (i > f && nums[j] == nums[j - 1]+1) ans[j - k + 1] = nums[j]; else if(nums[j]!=nums[j-1]+1) f = (j - 1); } i++; } return ans; } };
@codestorywithMIK11 күн бұрын
Glad to see this ❤️
@KeshavKumar-jl1ub11 күн бұрын
monotonic and please ... and why are we processing the first window .... seperately why not in that loop only
@gui-codes11 күн бұрын
you can do it in same loop as well. Both work fine. Here is the one loop code class Solution { public int[] resultsArray(int[] nums, int k) { int n=nums.length; int[] res=new int[n-k+1]; Arrays.fill(res,-1); int counter=1; for(int i=0;i0 && nums[i]==nums[i-1]+1){ counter++; } else{ counter=1; } if(counter>=k){ res[i+1-k]=nums[i]; } } return res; } }
@codestorywithMIK11 күн бұрын
Using Monotonic deque - kzbin.info/www/bejne/banOdHRuZc1ol5Ysi=BTZmLnId5xMES3gg Hope this helps ❤️