3224. Minimum Array Changes to Make Differences Equal | Prefix Sums | Why not Greedy

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024

Комментарии • 35

  • @ARYANMITTAL
    @ARYANMITTAL  2 месяца назад +12

    This is not a normal Medium Problem, it is Medium Hard (not in terms of implementation but a why on every step makes it Hard), skip dry run (that is longest section) ❤
    Do connect on Twitter☺ : x.com/aryan_mittal007

  • @-RAJSINGH
    @-RAJSINGH 2 месяца назад +4

    Finding this too hard 😢😢

  • @krishpoptani7862
    @krishpoptani7862 2 месяца назад +3

    I did it in contest with same approach

  • @ritikraj26_
    @ritikraj26_ 24 дня назад

    We can use segment trees as well. The resason we can use prefix sum here is because the queries are offline.

  • @satviksrinivas8764
    @satviksrinivas8764 Месяц назад

    Thank you I learned so much from this video!

  • @architagr984
    @architagr984 4 дня назад

    this is a very good explanation

  • @literally_ankur
    @literally_ankur Месяц назад

    Crazy question

  • @silverpoision
    @silverpoision Месяц назад

    Laut awo Aryan Bhai....

  • @priyapandey8951
    @priyapandey8951 2 месяца назад +1

    Bhaiya, I tried this approach which was to take the abs difference of all pairs and add it to a max priority queue and according to which diff has greater frequency that queue would handle it and we would check for all the diff in the increasing order of their frequencies so that way lesser number of operations would be required. Now considering n/2 differences even in the worst case would make it nearly half the size of array that is the worst case when all pairs have different frequency and then checking minimum operations for them would make it nearly O(10^5 * 10^3) would be 10^8 in worst case and generally that is allowed but my solution is still giving TLE. Can you explain where I went wrong?
    class Solution {
    private:
    struct myComp {
    constexpr bool operator()(
    pair const& a,
    pair const& b)
    const noexcept
    {
    return a.second < b.second;
    }
    };
    public:
    int minChanges(vector& nums, int k) {
    unordered_map freq;
    priority_queue pq;
    int n=nums.size();
    int i=0,j=n-1;
    while(i

  • @Rajdweep
    @Rajdweep 2 месяца назад +1

    nicely explained bro, but i dont think this should be a medium qs, the prefix intuition was pretty hard man.

  • @studyplanet4656
    @studyplanet4656 2 месяца назад +1

    What would be the difficulty of this question on codeforces

  • @MOHITRANA-to7rf
    @MOHITRANA-to7rf 2 месяца назад +1

    Hold on kr kr ke kuch smjh nahi aya

  • @saumyaagarwal7
    @saumyaagarwal7 2 месяца назад

    bro why are you not checking ans for all values from 0 to k .. you are just checking difference that are already formed by pairs...
    because my difference value can be between 0 to k..

  • @InWonderland-z2l
    @InWonderland-z2l 2 месяца назад +1

    wooaaahh awesome!!
    i thought about a lot of ways to get greedy approach to work during the contest.. and you tackled all of them in this video!

  • @asthajain2511
    @asthajain2511 2 месяца назад +2

    Aryan Bhai >>>>

  • @priyamvadashah4839
    @priyamvadashah4839 2 месяца назад +1

    Your explanations are getting so better!

  • @IK-xk7ex
    @IK-xk7ex 2 месяца назад

    Excellent problem, it's clear now after explaining :)

  • @Mandh-c7r
    @Mandh-c7r 2 месяца назад

    Your effort helps us a lot ..... Explained it awesomely......i guess you also did it with greedy first ...coz this was the first test case where greedy failed for me 😄

  • @fraserdab
    @fraserdab 2 месяца назад +1

    Liked

  • @chaitanyaamajala2582
    @chaitanyaamajala2582 2 месяца назад +1

    🙇

  • @prajitbanerjee8226
    @prajitbanerjee8226 2 месяца назад

    bro can you tell if this problem was on codeforces what would be its approximate rating like 1600?

    • @zaid6527
      @zaid6527 2 месяца назад

      1400 max to max

  • @SAURABHKUMAR-yo7er
    @SAURABHKUMAR-yo7er 2 месяца назад

    gawd lvl explanation 🔥🔥

  • @mohd.vaseem7410
    @mohd.vaseem7410 2 месяца назад

    great explanation bro

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 2 месяца назад

    thanks mittal saab

  • @monilcharola6873
    @monilcharola6873 2 месяца назад

    I solved it in Greedy way
    Find the difference with max freq
    Find the number of replacement moves to make the above difference
    return min(ans, (n-2)/2);
    This is an accepted solution in O(N)

    • @kartikegandhi3153
      @kartikegandhi3153 2 месяца назад

      It might have worked in contest but idts that it is the correct approach

    • @omkara477
      @omkara477 2 месяца назад

      Can u explained how is it working for this test case :
      [1,1,1,1,0,0,0,5,4,3,19,17,16,15,15,15,19,19,19,19] ?

    • @aysheri8321
      @aysheri8321 2 месяца назад

      what's the n-2/2

  • @akashsonar6332
    @akashsonar6332 2 месяца назад

    Well explained 👏

  • @nikhilprakash729
    @nikhilprakash729 2 месяца назад

    Great Explanation!!!!!

  • @23cash86
    @23cash86 2 месяца назад

    Good one

  • @priyanshupriyam174
    @priyanshupriyam174 2 месяца назад

    Finally