Aggressive cow | SPOJ

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • This video explains a very interesting problem from sphere online judge (SPOJ) which is the aggressive cow problem. I have shown proper insights and intuitions for solving this problem. First i have explained the problem statement using proper examples and then i have shown a simple approach to solve and also explained intuition for improving the time complexity further. I have shown the solution using binary search and have shown an example on how to solve it. Finally, at the end of the video, i have explained the code for this problem. As usual, CODE LINK along with some RELATED VIDEOS are present below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: gist.github.co...
    RELATED VIDEOS:-
    Search in rotated sorted array: • Search in rotated sort...
    First bad version: • First bad version | Le...

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

  • @hellorvce
    @hellorvce 4 ปีที่แล้ว +44

    U r the best ......Now whenever I have a doubt in a questio..... I just search "(Question name) by tech dose" and TH-cam gives me ur video

  • @spetsnaz_2
    @spetsnaz_2 4 ปีที่แล้ว +27

    One of the toughest problems explained very effectively.
    Best video on youtube 1⃣

  • @honest1805
    @honest1805 4 ปีที่แล้ว +11

    Sir your explanation for these programming problems is just awesome. (P.S. - I never posted any comment before this on any youtube video, your hardwork made me to express myself and Show how thankfull i am.)

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Thanks honest :)

  • @_360Art
    @_360Art 2 ปีที่แล้ว +1

    One of the best coding question Channel. Your explanation is very simple and easy to understand. Thank you

  • @soumensinha305
    @soumensinha305 4 ปีที่แล้ว +3

    One of best coding channel.
    I am really amazed by your hard work and making programming more accessible to tier n cities.

  • @gaunikasrivastava7851
    @gaunikasrivastava7851 3 ปีที่แล้ว +1

    For those not able to understand it, this problem is actually quite similar to "Allocate Minimum Number of Pages" problem on gfg. There we had to minimise the maximum value, and here we have to maximise the minimum value. The approach and logic is very similar

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว

      Yes you are right Allocate minimum number of pages, Painters Partition Problem and Aggressive cow problem is similar to each other

  • @ghumakkad_yogi
    @ghumakkad_yogi 4 ปีที่แล้ว +2

    kab se samjhna tha aap ne itne easy way me samjha diya thanks alot

  • @SB20002
    @SB20002 3 ปีที่แล้ว +2

    Excellent explanation better than most videos keep up the good work

  • @jaskaranbindra9939
    @jaskaranbindra9939 4 ปีที่แล้ว +2

    I was not clicking on this video because it was the longest one. But finally watched this one till end after quitting others in the first 1 min.
    Really nice. Thanks.

  • @nikhil.pandey
    @nikhil.pandey 4 ปีที่แล้ว +1

    if perfect explanation exists......! Hats off to you sir.

  • @shaiksalman9010
    @shaiksalman9010 ปีที่แล้ว +1

    how could you get such intution (applying binary search on it) plz reply bhai

  • @reyazahmed9320
    @reyazahmed9320 4 ปีที่แล้ว +4

    Great video. Quality is more important than quantity when it comes to these types of videos!

  • @shahbazalam4565
    @shahbazalam4565 4 ปีที่แล้ว +3

    Why is the low starting initialized with 0 and not ( arr[1]-arr[0] ) in sorted array as this would give the lowest possible answer in search space?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      1st cow will definitely be at 1st position. You can try submitting in SPOJ with your logic as well.

    • @rishabjain2634
      @rishabjain2634 3 ปีที่แล้ว

      Did it worked

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว

      What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @MohitSinha4
    @MohitSinha4 3 ปีที่แล้ว +1

    Best explanation!
    Thank you for the solution

  • @BestOption19
    @BestOption19 3 ปีที่แล้ว +2

    why mid=(low+high+1)/2 instead of (low+high)/2

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว +1

      What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @amanbhadani8840
    @amanbhadani8840 2 ปีที่แล้ว

    Why position of first cow is always fixed,why it is always placed at first index?

  • @EngWorld-nr2ww
    @EngWorld-nr2ww 2 ปีที่แล้ว

    Great explanation as compares to others on this problem on you tube.

  • @pastelek3
    @pastelek3 4 ปีที่แล้ว +2

    hey lad, can you explain me code in prolog?
    there is exercise: "list has to include months. define a predicate that will show one month before and after that you typed in. use append/3".
    solution is:
    month( X, M1, M2) :-
    append( _, [M1,X,M2 | _],
    [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).
    can you explain me why we use anonymous variables and lists here? i dont really get it :/

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      This is simple man. Let's say X=Jun. The entire list of months you gave at the end will be matched with your anonymous and other variables. So, first anonymous variable will be a list from Jan to Apr. M1=May, M2=July and last anonymous variable is a list containing aug to Dec. what's the confusion? It's better to use anonymous variable for the part you don't really care about. It just takes in any data (just like auto keyword in c++).

  • @ghumakkad_yogi
    @ghumakkad_yogi 4 ปีที่แล้ว +4

    one doubt: why are you taking mid as (low+high+1)/2 instead of (low+high)/2 or low+(high-low)/2

    • @osheennayak9477
      @osheennayak9477 4 ปีที่แล้ว

      to take care of even-odd cases

    • @naimishrastogi481
      @naimishrastogi481 4 ปีที่แล้ว +1

      if low and high are int and of order 10^9, then low + high will overflow the int range and the answer will be garbage, whereas low+(high-low) /2 will always work.

  • @anmolsharma9539
    @anmolsharma9539 3 ปีที่แล้ว +1

    great explanation bro !!
    But one question
    why you use mid =(low+high+1)/2 instead of mid = low+(high-low)/2

    • @tekbssync5727
      @tekbssync5727 3 ปีที่แล้ว

      same doubt here

    • @K_CO_NamanGarg
      @K_CO_NamanGarg 3 ปีที่แล้ว

      You can use that too. Mine got accepted there at the spoj using low+(high-low)/2.

    • @abin_4857
      @abin_4857 3 ปีที่แล้ว

      Both are same thing right,
      low+(high-low)/2=
      low -(low/2)+(high/2)=
      ( low+high)/2

    • @K_CO_NamanGarg
      @K_CO_NamanGarg 3 ปีที่แล้ว

      @@abin_4857 yes all are same but u should try to avoid using (low+high)/2,as if both low and high are huge numbers then their sum may lead to overflow

    • @tekbssync5727
      @tekbssync5727 3 ปีที่แล้ว

      @@abin_4857 Aree mere bhai, pehla comment thoda dhyan se dekh kya likha hua hai😂 , usne likha hai mid= (low+high+1)/2 , aur aap hume (low+high)/2 samjha rhe ho 😅

  • @saisriangajala8399
    @saisriangajala8399 ปีที่แล้ว

    why mid is (low+high+1)/2, it should be (low + high)/2 right.

  • @Abhishek-Khelge
    @Abhishek-Khelge 3 ปีที่แล้ว

    I think there is bug low should be equal to mid not mid +1, we can not discard mid itself, since it may well be the first element for which it is true. This is why moving the lower bound to mid is as aggressive as we can do without introducing bugs

    • @anjaliparihar9718
      @anjaliparihar9718 3 ปีที่แล้ว

      yes, u r right but to improvise this issue sir has used variable "best", not to miss the best possible answer.
      Hope it helps.

  • @rajeshbammidy180
    @rajeshbammidy180 4 ปีที่แล้ว

    Java code for AgressiveCows:github.com/RajeshAatrayan/InterviewPreparation/blob/master/src/BinarySearch/Aggressivecows.java

  • @prateeksrivastava1577
    @prateeksrivastava1577 3 ปีที่แล้ว +2

    Boy....u r good... : )

  • @sainathreddy2632
    @sainathreddy2632 3 ปีที่แล้ว

    giving the wrong answer for below logic (updating the answer only when the cnt == c) , but we have to update the ans when the cnt == cows only not when the c>n
    why this was not working
    if(cnt == c)
    {
    best = mid;
    low = mid+1;
    }
    else uf (cnt > c)
    {
    low = mid+1;
    }
    else
    {
    high = mid-1;
    }

  • @dontony7071
    @dontony7071 4 ปีที่แล้ว +1

    Best explanation bro.Thanks a lot

  • @Page_Turners_Hub
    @Page_Turners_Hub 3 ปีที่แล้ว +1

    why is low =1 ? Should'nt it be 0???

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Try with 0 and N-1

    • @Page_Turners_Hub
      @Page_Turners_Hub 3 ปีที่แล้ว

      @@techdose4u Yeah it's working with 0 and high = pos[n-1] - pos[0]

  • @aryannijhawan8448
    @aryannijhawan8448 3 ปีที่แล้ว +1

    Great solution buddy

  • @anuragdash6565
    @anuragdash6565 3 ปีที่แล้ว

    14:35 gap should be 3 and mid =4 so i think you are right in the video

  • @DreaMagnifier
    @DreaMagnifier 4 ปีที่แล้ว +2

    no words to define hard work..

  • @amlansain3610
    @amlansain3610 3 ปีที่แล้ว

    if i devide the stall by cows then i get equal gap which will be max gap.

  • @BhagyaRana
    @BhagyaRana 4 ปีที่แล้ว +1

    Great Explanation!👍

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Thanks 😊

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว

      What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @AbhishekSingh-og7kf
    @AbhishekSingh-og7kf 3 ปีที่แล้ว

    best explanation. Thanks a lot!

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว

      What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @AkshanshGusain
    @AkshanshGusain 3 ปีที่แล้ว +1

    Good explanation.

  • @sannidhayvashal9806
    @sannidhayvashal9806 3 ปีที่แล้ว +1

    Nicely explained

  • @crimsoncad3230
    @crimsoncad3230 4 ปีที่แล้ว +1

    Can you make a video on Google Foobar challenge???

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      This is the first time I am hearing this 😅 I will search and see it.

    • @crimsoncad3230
      @crimsoncad3230 4 ปีที่แล้ว +1

      @@techdose4u A pop up will appear on the browser when you search stuff related to computer science.
      There are other hacks to activate it but I've never tried those.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I read on Quora about it. Some challenges are given 🤔

  • @aviligondagowtham1153
    @aviligondagowtham1153 4 ปีที่แล้ว +1

    Sir this is data structures or any other language🤔

  • @harshpatel1385
    @harshpatel1385 4 ปีที่แล้ว +2

    plz upload video on problem like this

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +5

      I understand the need. I was bored from leetcode challenge and its consistently easy questions. You will see very good questions in future (but due to corona I committed to leetcode challenge and now there are many people who requested to continue in may). When I start going office, I won't be making daily videos and you will see only important videos. Making multiple videos in a day is next to impossible 😅 I hope you understand my position.

    • @harshpatel1385
      @harshpatel1385 4 ปีที่แล้ว +1

      @@techdose4u i am talking about after leet code challenge .ya sir we can understand your situations.you putting so much effort . God bless you

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      I will continue with good problems after leetcode challenge. You guys keep suggesting good problems. My list of videos to be made is very long now. I am processing them one by one. Today's problem was also requested by someone.

  • @akankshapathak9751
    @akankshapathak9751 4 ปีที่แล้ว +1

    Why low = 0 ? According to me low= arr[0]...pls tell

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Arr[0] will always be 1 because 1st cow always takes first position. You can even take low=1. There will be no problem. I took 1 extra for safety.

    • @akankshapathak9751
      @akankshapathak9751 4 ปีที่แล้ว +1

      @@techdose4u got it...thanks sir

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      👍

  • @dragoban1146
    @dragoban1146 4 ปีที่แล้ว +3

    is normal to do not be able to solve this problem ? because i feel that my level will not grow up

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      This is not a normal binary search. You can learn this and solve painters partition problem using same technique.

  • @bisatisrilatha7957
    @bisatisrilatha7957 3 ปีที่แล้ว +1

    Bro in code low must be a[0] isn't it??

  • @ashutoshtiwari4719
    @ashutoshtiwari4719 3 ปีที่แล้ว +1

    Thank you

  • @suprabhatkumar151
    @suprabhatkumar151 4 ปีที่แล้ว +1

    why low and high are 0 and a[n-1] respectively? Won't low be minimum gap(in this case min gap = 1) and high gap = a[n-1]-a[0]?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes you can take that as well. But 0 to a[n-1] is only 1 value larger. So no worries.

  • @ashishbajpai1016
    @ashishbajpai1016 4 ปีที่แล้ว +1

    awesome.

  • @indsonusharma
    @indsonusharma 4 ปีที่แล้ว +1

    Thanks bro
    You have said that in april it's not possible to make video for iterative merge sort . Can you please make a video of iterative merge sort in this month

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yea sure......I will try to include in on weekend.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Where are you stuck in iterative merge sort? It is simple only. Just like recursive.

    • @indsonusharma
      @indsonusharma 4 ปีที่แล้ว

      @@techdose4u in MERGESORT function if you saw geeksforgeeks tutorial for this. Basically we know what is going to happen but I can't come up intuitively like if we forget this question solution after reading this then how to proceed and all. One more reason I said this because there is no tutorial for this on YT (like you explain) and in blogs we see someone's solution which you also know that it is undigestable so that's why I put this question forward.

  • @sanyamjain5635
    @sanyamjain5635 3 ปีที่แล้ว

    Why are we taking mid = (low+high+1)/2 ? I tried with the standard mid = low + (high-low)/2 and it gave WA on SPOJ.
    Edit :
    I think we can use mid = low + (high-low)/2, but the think thats causing the issue is line no. 32 of code we can't take best directly , we must use something like best = max(best,mid); Then it will work fine for all cases.

    • @imshivendra
      @imshivendra 2 ปีที่แล้ว

      What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @devprakash4671
    @devprakash4671 3 ปีที่แล้ว +1

    I tried this problem , but I was getting WA , because i took `high` as accumulate(arr , arr+ n , 0 ) , i.e , sum of all elements . This is wrong . I watched this video then I realized It should be the highest element in the array . Thanks for this video .

    • @devprakash4671
      @devprakash4671 3 ปีที่แล้ว

      #include
      using namespace std ;
      #define fastio ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
      #define endl '
      '
      #define f(i , k , n ) for(int i = k ; i < n ; ++i )
      bool isPossible(int a[] , int n , int c , int minDis) {
      int lastPos = a[0] ;
      int cows = 1 ;
      for(int i = 1 ; i < n ; ++i ) {
      if ( a[i] - lastPos >= minDis ) {
      cows++ ;
      lastPos = a[i] ;
      if ( cows == c ) return true ;
      }
      }
      return false ;
      }
      int minDistance(int a[] , int n , int c ) {
      sort(a , a+n ) ;
      int low = 0 , high = a[n-1] ;
      int mid ; int ans = 0 ;
      while(low > t;
      while (t-- ) {
      int n , c ; cin >> n >> c ;
      int a[n] ;
      f(i , 0 , n ) cin >> a[i] ;
      cout

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Welcome :)

  • @shamanthnayak6015
    @shamanthnayak6015 4 ปีที่แล้ว

    Sir my doubt is if every time we get some difference value > gap and we can place all cows, that time the minimum value won't be equal to gap right?
    Basically why are we doing >=gap and not just = gap

  • @imshivendra
    @imshivendra 2 ปีที่แล้ว

    What is SPOJ and AGGRCOW? Does it signifies something very important?

  • @shade5554
    @shade5554 3 ปีที่แล้ว +2

    Am I the only one who feeling dumb here?

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      What happened ?

    • @shade5554
      @shade5554 3 ปีที่แล้ว +2

      @@techdose4u Well, I found this concept difficult to understand, but everyone in this comment section seems to understand it rather easily.
      That's the reason I felt dumb, lol

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Ohh...actually this is easy once you get my idea 😅

  • @x_x3557
    @x_x3557 3 ปีที่แล้ว +1

    Your voice sounds familliar 🤔 🤔 🤔

  • @PhoenixRisingFromAshes471
    @PhoenixRisingFromAshes471 2 ปีที่แล้ว

    extermeley bad explanation....totaly messed with gap values