Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Leetcode 1509

Sdílet
Vložit
  • čas přidán 2. 07. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 95th Video of our Playlist "Arrays 1D/2D : Popular Interview Problems" by codestorywithMIK
    In this video we will try to solve a good Array problem : Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Easy Explanations | Leetcode 1509 | codestorywithMIK
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Minimum Difference Between Largest and Smallest Value in Three Moves | 2 Approaches | Easy Explanations | Leetcode 1509 | codestorywithMIK
    Company Tags : will update soon
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/minimum...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My Recursion Concepts Playlist : • Introduction | Recursi...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Summary :
    Approach 1: Using Complete Sorting
    Time Complexity: O(nlogn)
    Space Complexity: O(1)
    Description:
    Sorting: The entire array nums is sorted.
    Edge Case: If the array size is 4 or less, the minimum difference is 0 because we can remove all elements.
    Calculate Minimum Difference: The minimum difference is calculated by considering four possible cases:
    Removing the three largest elements.
    Removing the three smallest elements.
    Removing the two largest and one smallest element.
    Removing the two smallest and one largest element.
    Result: The minimum of these differences is returned as the result.
    Approach 2: Using Partial Sorting
    Time Complexity: O(n)
    Space Complexity: O(1)
    Description:
    Edge Case: If the array size is 4 or less, the minimum difference is 0 because we can remove all elements.
    Partial Sorting:
    Use partial_sort to sort the first four elements to find the four smallest elements.
    Use nth_element to position the n-4th largest element correctly, such that all elements before it are smaller and all elements after it are larger.
    Sort the last four elements to ensure the four largest elements are in their correct positions.
    Calculate Minimum Difference: Similar to Approach 1, calculate the minimum difference by considering the same four possible cases.
    Result: The minimum of these differences is returned as the result.
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024

Komentáře • 42

  • @FifthArima
    @FifthArima Před 24 dny +1

    00:00 Intro & explanation
    02:30 Approach 1 -Complete Sorting
    19:30 Approach 2 -Partial Sorting

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz Před 25 dny +19

    Aap ka alawa aur koi bhi accha nehi hai, Jo beginners se samjata hai

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz Před 25 dny +8

    Aap ka hi Wait tha

  • @shashwatdixit5886
    @shashwatdixit5886 Před 25 dny +4

    Your way of teaching is great.
    Thanks !

  • @gui-codes
    @gui-codes Před 25 dny +3

    yaar mast samjhate ho aap. ekdm maza hi ajata hai

  • @wearevacationuncoverers

    king of best explanations.

  • @__Aakarsh-jo7ov
    @__Aakarsh-jo7ov Před 24 dny +1

    SUCH A BEGINNER FRIEDNLY AND EFFECTIVE WAY !!! KUDOS

  • @gocrazy6177
    @gocrazy6177 Před 24 dny +1

    Voice increased wow thanks

  • @ugcwithaddi
    @ugcwithaddi Před 25 dny +3

    Was waiting. Thank you

  • @venkatarohitpotnuru38
    @venkatarohitpotnuru38 Před 25 dny +6

    bhaiyya aagaya..

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 25 dny

    Thanks man. Finally understood.

  • @rev_krakken70
    @rev_krakken70 Před 25 dny +1

    Thanks to your arrays playlist.. was able to solve it ❤

  • @vipinchaudhary3520
    @vipinchaudhary3520 Před 24 dny +1

    I was very happy to find out that you are my college alumni from IIEST, Shibpur. Your videos help me a lot, and I have a great respect and love for you. ❤️

  • @demonslayerx9751
    @demonslayerx9751 Před 24 dny +1

    thank you bro , nice video !!

  • @Chandakshaythakur
    @Chandakshaythakur Před 24 dny +1

    Thank you bhaiya. Bhaiya as Segment tree playlist is going on bhaiya please solve some segment tree problems from Codechef also. Codechef contains good quality problems on segment tree.

  • @Yashwanth-zn4qj
    @Yashwanth-zn4qj Před 25 dny

    aap hi ka wait tha. subah se referesh kar raha tha

  • @molyoxide8358
    @molyoxide8358 Před 24 dny

    last 3 years mein pehli baar partial sort bhi kuch hota hai krke pta laga.

  • @sohiltr2310
    @sohiltr2310 Před 24 dny +1

    thankyou sir

  • @santhosh7042
    @santhosh7042 Před 25 dny +2

    Bhaiya binary search vala approach bhi discuss karna please muje lag raha jaise Koko and bananas kiya same ye bhi karskate hai par Mera kuch test case pass nahi horahe

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 24 dny +1

    Thanks a lot bhaiya ❤❤ learnt something new today partial_sort and nth_element
    Congrats for 55k subs🥳🥳

  • @study-yd6es
    @study-yd6es Před 25 dny +4

    Learning ++

  • @AMANYADAV-cd8tw
    @AMANYADAV-cd8tw Před 24 dny +1

    Bhaiya please launch your DSA Sheet for ultimate interview preparation. By the way Got rejected in DE Shaw OA today. 😢

  • @abhavgoel9390
    @abhavgoel9390 Před 24 dny +1

    The hit and trial method came to my mind after i read the problem, but i kept on thinking that cant be the solution. I face this many times and try top get the optimzed solution from the beginning. How to deal with this?

  • @KeshavKumar-jl1ub
    @KeshavKumar-jl1ub Před 24 dny

    please make one video on leetcode 687 please see that question once...

  • @himanshujain9933
    @himanshujain9933 Před 24 dny

    Thanks for the video sorting takes also space o n right?

  • @rushikeshkadam7768
    @rushikeshkadam7768 Před 25 dny +1

    Bhaiya can you please add pdf notes of graph concepts and questions to github.

  • @LeetcodeToday
    @LeetcodeToday Před 25 dny

    bhaiya priority queue wala solution bhi bata do

  • @aizad786iqbal
    @aizad786iqbal Před 24 dny

    hi what if the questions had said K instead of 3... how would we approach then ?

    • @aizad786iqbal
      @aizad786iqbal Před 24 dny

      res = Math.min(res, nums[n-1] - nums[3]);
      res = Math.min(res, nums[n-2] - nums[2]);
      res = Math.min(res, nums[n-3] - nums[1]);
      res = Math.min(res, nums[n-4] - nums[0]);
      loop laga kar karna hoga then..
      I guess..
      ek start se and ek end se...

  • @someshnayakrollno.-09sec-b27

    i tried to solve the problem by making an approach by myself ,but the approach came out to be limited to some cases everytime .
    give me some suggestions to improve the problem solving skills!!

  • @dikshantyadav1110
    @dikshantyadav1110 Před 24 dny

    MIK bhaiya nhi sir ho gye hai @7.22 seconds,

    • @codestorywithMIK
      @codestorywithMIK  Před 24 dny

      No no. “No sir” . Only bhaiya 😇🙏❤️
      We all are on same level ❤️❤️❤️

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 25 dny +1

    public int minDifference(int[]nums){
    int n=nums.length;
    if(n

  • @HMBDM1976
    @HMBDM1976 Před 24 dny

    class Solution {
    public:
    int minDifference(vector& nums) {
    sort(nums.begin(),nums.end());
    int n=nums.size();
    if(n>=4){
    return min({nums[n-4]-nums[0],nums[n-1]-nums[3],nums[n-3]-nums[1],nums[n-2]-nums[2]});
    }
    return 0;
    }
    };

  • @sakshamshrivastava3482
    @sakshamshrivastava3482 Před 24 dny +1

    kuch samajh nhi aaya bhai

    • @codestorywithMIK
      @codestorywithMIK  Před 24 dny +2

      Hello there,
      Apologies if you felt so.
      Would you like to point which point exactly you felt lost. I will try my best to help here

  • @literally_ankur
    @literally_ankur Před 23 dny

    Second approach was bit unnecessary.

    • @codestorywithMIK
      @codestorywithMIK  Před 23 dny

      Yep I agree.
      Just felt like sharing 😇
      I will try to avoid adding similar approaches in a video.
      Thank you for the feedback ❤️🙏😇

  • @HMBDM1976
    @HMBDM1976 Před 24 dny +1

    Sir could you please speak in english .I am a south indian.You are really doing a great job .Your explaination and step by step approach is top notch.But south indian students cant understand hindi very well.Please do your videos in english so that everyone can understand sir. Its a sincere request from my side 🥹🥹

  • @manasdeora4601
    @manasdeora4601 Před 24 dny

    Kuch smjh me nhi aaya aaj toh!

    • @codestorywithMIK
      @codestorywithMIK  Před 24 dny

      Hi there,
      Would you point where exactly you felt stuck. I will try my best to help here ❤️