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

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ย. 2024

ความคิดเห็น • 66

  • @joydeep-halder
    @joydeep-halder วันที่ผ่านมา +12

    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 ❤

  • @dhruvchopra26
    @dhruvchopra26 วันที่ผ่านมา +13

    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 วันที่ผ่านมา +2

      congrats man.

    • @dhruvchopra26
      @dhruvchopra26 วันที่ผ่านมา

      @gui-codes Thanks broo

    • @universalcosmologist3675
      @universalcosmologist3675 วันที่ผ่านมา

      bhai mai to 1 ya 2 hi solve kar paata hun contest me pls help

    • @dhruvchopra26
      @dhruvchopra26 วันที่ผ่านมา

      @@universalcosmologist3675 Koi ni bhai Lage rho dheere dheere hojayga

    • @069_Souvik
      @069_Souvik วันที่ผ่านมา +1

      ​@@dhruvchopra26Congrats bro...

  • @pankaj_kumar_malik
    @pankaj_kumar_malik วันที่ผ่านมา +9

    Able to solve in my own, Intuition apne ap banne laga bhaiya... MIK magic✨❤

  • @Ankitpal-y6h
    @Ankitpal-y6h วันที่ผ่านมา +2

    keep doing such a kind of series (if possible contest discussion as well),god bless you good health and life .

  • @bd1a0573
    @bd1a0573 วันที่ผ่านมา +1

    Solved the problem in n*k TC but still not satisfied. SO watching your video now😁😁😁

  • @VineetDixit
    @VineetDixit วันที่ผ่านมา +2

    i solved it using the optimized method,thanks to your previous videos

  • @nishantdehariya5769
    @nishantdehariya5769 วันที่ผ่านมา +1

    to not support brute force the length of array should be >= 10^5, great problem

  • @mohammadaftabansari6882
    @mohammadaftabansari6882 วันที่ผ่านมา +1

    Thanks for the videos.

  • @madmaxgaming5864
    @madmaxgaming5864 วันที่ผ่านมา +1

    loved your optimised approach ♥

  • @amanpaliwalvlogs6860
    @amanpaliwalvlogs6860 วันที่ผ่านมา +3

    Radhe Radhe ❤

  • @ugcwithaddi
    @ugcwithaddi วันที่ผ่านมา +2

    Was able to solve 👌🏻

  • @harleenkaur8540
    @harleenkaur8540 วันที่ผ่านมา +3

    pls solve using monotonic queue as well

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา

      th-cam.com/video/7xkCA80h5a4/w-d-xo.htmlsi=BTZmLnId5xMES3gg
      Hope this helps ❤️🙏

    • @harleenkaur8540
      @harleenkaur8540 วันที่ผ่านมา

      @codestorywithMIK thanks a lot ❤

    • @mohammadaftabansari6882
      @mohammadaftabansari6882 วันที่ผ่านมา

      @@codestorywithMIK Much appreciated.

  • @vedanthbaliga7686
    @vedanthbaliga7686 วันที่ผ่านมา

    Solved before the video using same concept!

  • @piyush0mandloi
    @piyush0mandloi วันที่ผ่านมา +1

    thank you bhiay

  • @shivaye08
    @shivaye08 วันที่ผ่านมา

    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 ❤

  • @Engineering.Wallah
    @Engineering.Wallah วันที่ผ่านมา +2

    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;
    }
    };

  • @kaat6663
    @kaat6663 วันที่ผ่านมา

    class Solution {
    public:
    vector resultsArray(vector& nums, int k) {
    int n = nums.size();
    vector ans(n-k+1);
    for(int i = 0 ; i

  • @hellsterkun8764
    @hellsterkun8764 วันที่ผ่านมา +1

    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;
    }
    };

  • @pranildhutraj6038
    @pranildhutraj6038 วันที่ผ่านมา

    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 วันที่ผ่านมา

    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;
    }
    }

  • @tauheedahmed4073
    @tauheedahmed4073 วันที่ผ่านมา +1

    I have done till sliding but when i saw this count was like aha...

  • @AS-gf3ci
    @AS-gf3ci วันที่ผ่านมา +2

    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 วันที่ผ่านมา

      you can easily solve in O(n)

    • @AS-gf3ci
      @AS-gf3ci วันที่ผ่านมา +1

      @@rickdutta942 Yeah, but this approach is just for intuition building for beginners. 1st approach to discuss in the interview.

    • @rickdutta942
      @rickdutta942 วันที่ผ่านมา

      ​@@AS-gf3ci Agreed.

    • @gui-codes
      @gui-codes วันที่ผ่านมา

      @@AS-gf3ci yes bro agree.

  • @AbhiGarg-u1w
    @AbhiGarg-u1w วันที่ผ่านมา

    bhaiya can you please make a video for post contest solution

  • @rickdutta942
    @rickdutta942 วันที่ผ่านมา +1

    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

  • @AlgoUniverse01
    @AlgoUniverse01 วันที่ผ่านมา

    Please use a black screen instead of a white one, as the high contrast of white is uncomfortable for the eyes.

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา

      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 วันที่ผ่านมา +1

    Why 1st window is preprocessed ??

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา

      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 ❤️

  • @ArnabBhadra02
    @ArnabBhadra02 วันที่ผ่านมา +1

    at first glance its too much difficult to think this otimized version

  • @ArnabBhadra02
    @ArnabBhadra02 วันที่ผ่านมา

    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;
    }
    };

  • @Resham298
    @Resham298 วันที่ผ่านมา

    pls tell me why we set consecutive count to 1 when a non consecutive element is seen

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +1

      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 วันที่ผ่านมา

      @codestorywithMIK thankyou bhaiya understood

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 วันที่ผ่านมา +1

    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 วันที่ผ่านมา +1

    Not able to do own my own feeling low...

    • @tauheedahmed4073
      @tauheedahmed4073 วันที่ผ่านมา +1

      How long have you been dng bro

    • @vidhut-rau
      @vidhut-rau วันที่ผ่านมา

      @@tauheedahmed4073 from may 2024 started Dsa

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา

      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 วันที่ผ่านมา

    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;
    }
    };

  • @KeshavKumar-jl1ub
    @KeshavKumar-jl1ub วันที่ผ่านมา

    monotonic and please ... and why are we processing the first window .... seperately why not in that loop only

    • @gui-codes
      @gui-codes วันที่ผ่านมา

      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  วันที่ผ่านมา

      Using Monotonic deque -
      th-cam.com/video/7xkCA80h5a4/w-d-xo.htmlsi=BTZmLnId5xMES3gg
      Hope this helps ❤️