Find the Power of K-Size Subarrays I | Simple Explanation | Dry Run| Leetcode 3254 |codestorywithMIK

  Рет қаралды 5,736

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер
@joydeep-halder
@joydeep-halder 11 күн бұрын
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 ❤
@swastikakundu8418
@swastikakundu8418 11 күн бұрын
same
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
❤️🙏
@dhruvchopra26
@dhruvchopra26 11 күн бұрын
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-codes
@gui-codes 11 күн бұрын
congrats man.
@dhruvchopra26
@dhruvchopra26 11 күн бұрын
@gui-codes Thanks broo
@universalcosmologist3675
@universalcosmologist3675 11 күн бұрын
bhai mai to 1 ya 2 hi solve kar paata hun contest me pls help
@dhruvchopra26
@dhruvchopra26 11 күн бұрын
@@universalcosmologist3675 Koi ni bhai Lage rho dheere dheere hojayga
@069_Souvik
@069_Souvik 11 күн бұрын
​@@dhruvchopra26Congrats bro...
@pankaj_kumar_malik
@pankaj_kumar_malik 11 күн бұрын
Able to solve in my own, Intuition apne ap banne laga bhaiya... MIK magic✨❤
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Way to go ❤️
@pankaj_kumar_malik
@pankaj_kumar_malik 11 күн бұрын
@@codestorywithMIK ❤🙌
@Ankitpal-y6h
@Ankitpal-y6h 11 күн бұрын
keep doing such a kind of series (if possible contest discussion as well),god bless you good health and life .
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Means a lot ❤️🙏
@bd1a0573
@bd1a0573 11 күн бұрын
Solved the problem in n*k TC but still not satisfied. SO watching your video now😁😁😁
@madmaxgaming5864
@madmaxgaming5864 11 күн бұрын
loved your optimised approach ♥
@VineetDixit
@VineetDixit 11 күн бұрын
i solved it using the optimized method,thanks to your previous videos
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Great 👍
@nishantdehariya5769
@nishantdehariya5769 11 күн бұрын
to not support brute force the length of array should be >= 10^5, great problem
@ugcwithaddi
@ugcwithaddi 11 күн бұрын
Was able to solve 👌🏻
@mohammadaftabansari6882
@mohammadaftabansari6882 11 күн бұрын
Thanks for the videos.
@Engineering.Wallah
@Engineering.Wallah 11 күн бұрын
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; } };
@vedanthbaliga7686
@vedanthbaliga7686 11 күн бұрын
Solved before the video using same concept!
@amanpaliwalvlogs6860
@amanpaliwalvlogs6860 11 күн бұрын
Radhe Radhe ❤
@shivaye08
@shivaye08 11 күн бұрын
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 ❤
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Way to go ❤️
@piyush0mandloi
@piyush0mandloi 11 күн бұрын
thank you bhiay
@kaat6663
@kaat6663 11 күн бұрын
class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); vector ans(n-k+1); for(int i = 0 ; i
@pranildhutraj6038
@pranildhutraj6038 11 күн бұрын
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-codes
@gui-codes 11 күн бұрын
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; } }
@harleenkaur8540
@harleenkaur8540 11 күн бұрын
pls solve using monotonic queue as well
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
kzbin.info/www/bejne/banOdHRuZc1ol5Ysi=BTZmLnId5xMES3gg Hope this helps ❤️🙏
@harleenkaur8540
@harleenkaur8540 11 күн бұрын
@codestorywithMIK thanks a lot ❤
@mohammadaftabansari6882
@mohammadaftabansari6882 11 күн бұрын
@@codestorywithMIK Much appreciated.
@AS-gf3ci
@AS-gf3ci 11 күн бұрын
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
@rickdutta942
@rickdutta942 11 күн бұрын
you can easily solve in O(n)
@AS-gf3ci
@AS-gf3ci 11 күн бұрын
@@rickdutta942 Yeah, but this approach is just for intuition building for beginners. 1st approach to discuss in the interview.
@rickdutta942
@rickdutta942 11 күн бұрын
​@@AS-gf3ci Agreed.
@gui-codes
@gui-codes 11 күн бұрын
@@AS-gf3ci yes bro agree.
@hellsterkun8764
@hellsterkun8764 11 күн бұрын
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; } };
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Awesome♥️
@rickdutta942
@rickdutta942 11 күн бұрын
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
@tauheedahmed4073
@tauheedahmed4073 11 күн бұрын
I have done till sliding but when i saw this count was like aha...
@ArnabBhadra02
@ArnabBhadra02 11 күн бұрын
Brute Force: T.C: O(N*K) class Solution { public: int isposs(vector&temp){ for(int i=0;i0) res.push_back(ans); else res.push_back(-1); } } return res; } };
@AbhiGarg-u1w
@AbhiGarg-u1w 11 күн бұрын
bhaiya can you please make a video for post contest solution
@aizad786iqbal
@aizad786iqbal 9 күн бұрын
how is it n-k+1 ??
@gui-codes
@gui-codes 9 күн бұрын
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.
@ArnabBhadra02
@ArnabBhadra02 11 күн бұрын
at first glance its too much difficult to think this otimized version
@Resham298
@Resham298 11 күн бұрын
pls tell me why we set consecutive count to 1 when a non consecutive element is seen
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
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
@Resham298
@Resham298 11 күн бұрын
@codestorywithMIK thankyou bhaiya understood
@AlgoUniverse01
@AlgoUniverse01 11 күн бұрын
Please use a black screen instead of a white one, as the high contrast of white is uncomfortable for the eyes.
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Sure thing. Actually I use black screen only. Actually I am currently travelling and it slipped my mind to use black screen.
@vidhut-rau
@vidhut-rau 11 күн бұрын
Why 1st window is preprocessed ??
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
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 ❤️
@dayashankarlakhotia4943
@dayashankarlakhotia4943 11 күн бұрын
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-rau
@vidhut-rau 11 күн бұрын
Not able to do own my own feeling low...
@tauheedahmed4073
@tauheedahmed4073 11 күн бұрын
How long have you been dng bro
@vidhut-rau
@vidhut-rau 11 күн бұрын
@@tauheedahmed4073 from may 2024 started Dsa
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Don’t feel low. This is the process of learning. Such phase will always come. With consistency, things will improve slowly but definitely ❤️
@Playvish
@Playvish 11 күн бұрын
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; } };
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Glad to see this ❤️
@KeshavKumar-jl1ub
@KeshavKumar-jl1ub 11 күн бұрын
monotonic and please ... and why are we processing the first window .... seperately why not in that loop only
@gui-codes
@gui-codes 11 күн бұрын
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; } }
@codestorywithMIK
@codestorywithMIK 11 күн бұрын
Using Monotonic deque - kzbin.info/www/bejne/banOdHRuZc1ol5Ysi=BTZmLnId5xMES3gg Hope this helps ❤️
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 11 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 3 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
Nexus Speaker Series: Jannik Spiessens
44:39
Nexus
Рет қаралды 1
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 14 МЛН
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 11 МЛН