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

Поділитися
Вставка
  • Опубліковано 25 сер 2024
  • Whatsapp Community Link : www.whatsapp.c...
    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/MAZ...
    Leetcode Link : leetcode.com/p...
    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/MAZ...
    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

КОМЕНТАРІ • 42

  • @FifthArima
    @FifthArima Місяць тому +1

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

  • @shashwatdixit5886
    @shashwatdixit5886 Місяць тому +4

    Your way of teaching is great.
    Thanks !

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz Місяць тому +19

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

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz Місяць тому +8

    Aap ka hi Wait tha

  • @gui-codes
    @gui-codes Місяць тому +3

    yaar mast samjhate ho aap. ekdm maza hi ajata hai

  • @ugcwithaddi
    @ugcwithaddi Місяць тому +3

    Was waiting. Thank you

  • @wearevacationuncoverers
    @wearevacationuncoverers Місяць тому

    king of best explanations.

  • @__Aakarsh-jo7ov
    @__Aakarsh-jo7ov Місяць тому +1

    SUCH A BEGINNER FRIEDNLY AND EFFECTIVE WAY !!! KUDOS

  • @gocrazy6177
    @gocrazy6177 Місяць тому +1

    Voice increased wow thanks

  • @rev_krakken70
    @rev_krakken70 Місяць тому +1

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

  • @venkatarohitpotnuru38
    @venkatarohitpotnuru38 Місяць тому +6

    bhaiyya aagaya..

  • @vipinchaudhary3520
    @vipinchaudhary3520 Місяць тому +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 Місяць тому +1

    thank you bro , nice video !!

  • @Chandakshaythakur
    @Chandakshaythakur Місяць тому +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.

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Місяць тому

    Thanks man. Finally understood.

  • @HMBDM1976
    @HMBDM1976 Місяць тому

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

  • @Yashwanth-zn4qj
    @Yashwanth-zn4qj Місяць тому

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

  • @molyoxide8358
    @molyoxide8358 Місяць тому

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

  • @AMANYADAV-cd8tw
    @AMANYADAV-cd8tw Місяць тому +1

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

  • @study-yd6es
    @study-yd6es Місяць тому +4

    Learning ++

  • @gauravbanerjee2898
    @gauravbanerjee2898 Місяць тому +1

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

  • @sohiltr2310
    @sohiltr2310 Місяць тому +1

    thankyou sir

  • @KeshavKumar-jl1ub
    @KeshavKumar-jl1ub Місяць тому

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

  • @abhavgoel9390
    @abhavgoel9390 Місяць тому +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?

  • @santhosh7042
    @santhosh7042 Місяць тому +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

  • @rushikeshkadam7768
    @rushikeshkadam7768 Місяць тому +1

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

  • @himanshujain9933
    @himanshujain9933 Місяць тому

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

  • @LeetcodeToday
    @LeetcodeToday Місяць тому

    bhaiya priority queue wala solution bhi bata do

  • @HMBDM1976
    @HMBDM1976 Місяць тому +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 🥹🥹

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Місяць тому +1

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

  • @someshnayakrollno.-09sec-b27
    @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!!

  • @aizad786iqbal
    @aizad786iqbal Місяць тому

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

    • @aizad786iqbal
      @aizad786iqbal Місяць тому

      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...

  • @dikshantyadav1110
    @dikshantyadav1110 Місяць тому

    MIK bhaiya nhi sir ho gye hai @7.22 seconds,

    • @codestorywithMIK
      @codestorywithMIK  Місяць тому

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

  • @sakshamshrivastava3482
    @sakshamshrivastava3482 Місяць тому +1

    kuch samajh nhi aaya bhai

    • @codestorywithMIK
      @codestorywithMIK  Місяць тому +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 Місяць тому

    Second approach was bit unnecessary.

    • @codestorywithMIK
      @codestorywithMIK  Місяць тому

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

  • @manasdeora4601
    @manasdeora4601 Місяць тому

    Kuch smjh me nhi aaya aaj toh!

    • @codestorywithMIK
      @codestorywithMIK  Місяць тому

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