C++ For-Loops Range | Algorithms For Beginners

Поділитися
Вставка
  • Опубліковано 5 чер 2024
  • Not sure if you should use N-1 or N-2? Let's practice how to use indices in algorithmic problems.
    CF Football codeforces.com/problemset/pro...
    CSES Repetitions cses.fi/problemset/task/1069
    LC Repeated DNA Seq leetcode.com/problems/repeate...
    0:00 3 in a row
    2:46 K in a row
    4:34 code
    7:49 homework
    I stream on Twitch: / errichto

КОМЕНТАРІ • 157

  • @codingcouch
    @codingcouch 2 роки тому +132

    We need more such tutorials on different topics. This was really helpful.

  • @sarveshmandewal4749
    @sarveshmandewal4749 2 роки тому +23

    Errichto in AtCoder Hard : I am not going to explain this trivial dp optimisation
    *Now randomly uploads how to use a for loop* 😂😂😂

    • @Errichto
      @Errichto  2 роки тому +30

      Coming next: how to compile your code?

    • @rohithv63
      @rohithv63 2 роки тому

      @@Errichto 😅

    • @AAAAAA-gj2di
      @AAAAAA-gj2di 2 роки тому +1

      @@Errichto 😆 what's next? How to type?

  • @sachinjangir166
    @sachinjangir166 2 роки тому +63

    Also another way for finding condition is like this:
    If we are finding 3 consecutive equals, and we also know that last index of string is (N-1), then we know that
    i+2

    • @abhishek.rathore
      @abhishek.rathore 2 роки тому +18

      You dont even need to rearrange.. We can leave the condition at i + 2 < n

    • @muditkhanna8164
      @muditkhanna8164 Рік тому

      @@abhishek.rathore you are right also by rearranging we are making it more complex in debugging, so we should avoid rearranging.

  • @AlienGodDog
    @AlienGodDog 2 роки тому +5

    Wow, after a long break he came back! Glad to see you back! Thank you!

  • @WillsonMock
    @WillsonMock 2 роки тому

    This is really helpful - especially when you dive into how you think about the problems and approach them. Thank you for sharing!

  • @sachinjangir166
    @sachinjangir166 2 роки тому +4

    Dude, you are so underrated!... your explanations are so good... love to see your videos.

  • @andreyvalverde4780
    @andreyvalverde4780 2 роки тому +1

    Love the format of this video, where you do a very good explanation and leave homework with similar problems :D

  • @AnyVideo999
    @AnyVideo999 2 роки тому +8

    I usually use
    i < length
    But when you have i+2 being checked, or whatever the biggest is, you can avoid rearranging altogether by simply using
    i + 2 < length
    If you like the ≤ operation, your right hand side can remain the same. I prefer modifying the RHS since it is clear what condition is leading us to our inequality.

  • @hridayansh7682
    @hridayansh7682 2 роки тому +3

    I'd love to watch more. Thank you!

  • @herculeskumar9341
    @herculeskumar9341 2 роки тому +1

    Wow such awesome detailed explanation with questions. Please make more of these

  • @aditheorigin4926
    @aditheorigin4926 2 роки тому

    So good to see you are back @Errichto

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 2 роки тому +4

    Happy New Year @Errichto . Good to see you back. How are you doing?? Please keep uploading more videos, we all really missed your presence over UA-cam! Thanks for such a grand come back! 😇
    Also adding these sort of video which seems quite easy but require some amount of generalization and thinking logically how to generalize the problem will really be quite helpful for the beginners to think broader about the problem. Thanks!

  • @kristoabdi4668
    @kristoabdi4668 2 роки тому +3

    Please keep making videos like this, great job!

  • @santhoskumar1369
    @santhoskumar1369 2 роки тому +1

    This might be very basic but this formula helped me in many times. Number of elements in the interval [a,b] = b - a + 1. Where b >=a

  • @mohamedayman-um4he
    @mohamedayman-um4he 2 роки тому +3

    one of the best things today seeing you back errichto 😉☺

  • @mr.anderson5077
    @mr.anderson5077 2 роки тому +6

    Awesome lecture, please bring it up as a series where a beginner gets to hone thier algorithm and ds skills. Thanks

  • @sandun4983
    @sandun4983 2 роки тому

    I really appreciate your work , you teach us how to think logically before the writing the codes .

  • @AlfonsoHdez0823
    @AlfonsoHdez0823 2 роки тому

    This is so useful! Thank you so much for making these videos!

  • @abrarmasumabir3809
    @abrarmasumabir3809 2 роки тому

    Love this....please carry out the good work...

  • @souljarohill8795
    @souljarohill8795 2 роки тому

    This is perfect and i think from this video alone i finally understand the logic behind the sort and search techniques. This video proved the need for me to buy a whiteboard and use some psudeocode whenever im doing loops. The reason being is because im trying to figure out how to test my loop the correct way to get the anwer i want. Doing the psudecode allows me to see what im doing at each postion.

  • @TheXeXKyle
    @TheXeXKyle 2 роки тому

    You're the best man, thank you so much for making these videos

  • @sajidhaque5951
    @sajidhaque5951 Рік тому

    too much prescious knowledge man! Thank you so so much!!

  • @user-qs7dp6yb2x
    @user-qs7dp6yb2x 2 роки тому

    This was very well explained. Thank you!

  • @4hm3d57
    @4hm3d57 2 роки тому

    Glad to see you back Errichto

  • @GustavoSilva-st6zc
    @GustavoSilva-st6zc 2 роки тому

    Thanks for this video! I'm currtently working on improving my coding skills to become a better competitive programmer, this video helps a lot.

  • @shahzan525
    @shahzan525 2 роки тому

    Thank you so much for starting this series for beginners...

  • @PraveenKulkarni1996
    @PraveenKulkarni1996 2 роки тому +31

    Rather than saying i < n - 2, I find it more readable to say i + 2 < n.
    for (int i = 0; i + 2 < n; i++) {
    if (a[i] == a[i+1] and a[i] == a[i+2]) {
    return true;
    }
    }

    • @Errichto
      @Errichto  2 роки тому +13

      That's a very good method!
      EDIT: The small disadvantage of "i + 2 < n" is that you can't use the same method to handle accessing a[i-1] and a[i-2]. You need to start with "int i = 2" so you anyway use my approach of thinking: what index should be considered first or last. Also, python for-loops require the upper limit instead of a condition. That being said, "i + 2 < n" or "i + k - 1 < n" is perfectly fine.

    • @icenberg5908
      @icenberg5908 2 роки тому

      But it might have issues with overflow and hence undefined behavior

    • @themicro8319
      @themicro8319 2 роки тому

      All work that float your boat ⛵

    • @luisoncpp
      @luisoncpp 2 роки тому

      Yep, I was going to say exactly the same, I have been doing that for years.

    • @luisoncpp
      @luisoncpp 2 роки тому +1

      @@icenberg5908 s[i+2] already depends that i + 2 is not overflowing.

  • @whitedracarys7613
    @whitedracarys7613 2 роки тому +1

    Imagine if Errichto had his own Algorithms Bootcamp .
    It would be the best .
    🔥

  • @Bilal_Alasha
    @Bilal_Alasha 2 роки тому

    Glad to see you back

  • @adarshbadagala
    @adarshbadagala 2 роки тому

    It's good to see you back after some time..

  • @ishaankelkar7521
    @ishaankelkar7521 2 роки тому +1

    MAKE MORE LIKE THESE EVERYBODY LOVES THEM

  • @yogeshagarwal8537
    @yogeshagarwal8537 2 роки тому

    Awesome Lecture. Please Keep it continue.

  • @mdyousufgazi4030
    @mdyousufgazi4030 2 роки тому

    glad to see you back errichto

  • @testflash4510
    @testflash4510 2 роки тому

    So glad to see you back

  • @diegonayalazo
    @diegonayalazo Рік тому

    Great tutorials! Keep it up

  • @theprimecoder4981
    @theprimecoder4981 2 роки тому +1

    I really liked your style of explaining, I learned more about index and it's ranges. I already feel like a guru and I'm just a beginner 😆

  • @ankurdahiya1354
    @ankurdahiya1354 2 роки тому

    I couldn’t stop myself from liking this video

  • @violettapidvolotska7530
    @violettapidvolotska7530 2 роки тому +12

    Thanks for the video, all of them are very useful! It looks like now the time complexity is O(n*k) which is not so nice when k is too large. I think we can use in this problem sliding window of size k to have the time complexity O(n)

    • @Errichto
      @Errichto  2 роки тому +8

      Yes, you're 100% correct.

  • @shantanuhallur6825
    @shantanuhallur6825 2 роки тому

    Loved the explanation!! Please can you create more of such basic but important methods of thinking or approaching a problem while coding videos ... !!

  • @MalicePandrodor
    @MalicePandrodor 2 роки тому

    I prefer to use such construction:
    for (int i=k; i

  • @limakbear5292
    @limakbear5292 2 роки тому

    I am very happy to see you ❤️

  • @shreyashachoudhary480
    @shreyashachoudhary480 2 роки тому

    Loved you content 🙌

  • @AbhishekKumar-ux9hv
    @AbhishekKumar-ux9hv 2 роки тому

    This is very helpful, thanks.

  • @alexwexov4298
    @alexwexov4298 2 роки тому

    Great video ! Keep up.

  • @aryanbhat6419
    @aryanbhat6419 2 роки тому

    errichto need more things for beginners and intermediates, You are doing a great job Much love from INDIA.

  • @shivashankar28
    @shivashankar28 2 роки тому

    Thank You sir !!

  • @akshat6176
    @akshat6176 2 роки тому

    a video after sooo long. finally

  • @akashthoriya
    @akashthoriya 2 роки тому

    Thank you!

  • @TheCitrys
    @TheCitrys 2 роки тому

    Thanks for the informative video. Still, practice only makes the difference in understanding the boundaries. Solved all problems and found CSES Repetitions the most challenging. One comment here, I think it would be very nice to continue this lesson by describing of "sliding window" pattern. Cause all these problems match it.

  • @alexni3524
    @alexni3524 2 роки тому

    im glad that the way we think are similar

  • @Gamin_duniya
    @Gamin_duniya 2 роки тому

    We need more such. Love u

  • @alyaaahmed4462
    @alyaaahmed4462 2 роки тому

    Great,thanks 👍☺️

  • @Gzzzzzz111
    @Gzzzzzz111 Рік тому

    Hey Errichto, I found your algo videos very easy to understand. Especially the ones on DP. Are you not making more videos on algo anymore?

  • @josealejandrovaroncarreno1692
    @josealejandrovaroncarreno1692 2 роки тому +1

    @Errichto thanks for a new video, it was weird that you disappeared for a month, I thought you were in a traveling competition😂

  • @alinasser2799
    @alinasser2799 2 роки тому +23

    We want a series that covers everything about your sheet in C++ such as these videos and takes us to a professional in competitive programming, please.

    • @ITACHIUCHIHA-dr8sz
      @ITACHIUCHIHA-dr8sz 2 роки тому +10

      You need to understand early in your journey that there is no "course" for cp, it's all practice. Sure you can have some guidance on what and how to practice which errichto and many other people have already provided.

    • @vedhasbalaji7648
      @vedhasbalaji7648 2 роки тому +2

      There is no such series, just go practice

    • @alinasser2799
      @alinasser2799 2 роки тому +1

      @@ITACHIUCHIHA-dr8sz Yes, I fully understand that experience comes with practice in solving problems, but we mean such videos that teach us how to errichto think so that we acquire that skill, thank you for your advice brother

    • @howlingfjord1266
      @howlingfjord1266 2 роки тому

      He recommended Competitive Programmer's Handbook, check this ua-cam.com/video/xAeiXy8-9Y8/v-deo.html

    • @ru2979
      @ru2979 Рік тому

      @@ITACHIUCHIHA-dr8sz hi hitachi :)

  • @davisito70
    @davisito70 2 роки тому

    I follow a slightly different approach. I usually make sure that all access operations are within bound. For example, for s[i + 2] to be correct, we need to make sure that 0

  • @user-pi2zd4xj8z
    @user-pi2zd4xj8z 3 місяці тому

    U nailed the very basic but common problem to all age experience coders.

  • @sahilanand30
    @sahilanand30 2 роки тому +1

    King is back

  • @micheltarget1509
    @micheltarget1509 2 роки тому

    Thanks man

  • @jiwatomo
    @jiwatomo 2 роки тому

    long not see, my teacher

  • @flamendless
    @flamendless 2 роки тому +1

    Please more of this algo for beginners :)

  • @blancosj
    @blancosj 2 роки тому

    Welcome back!

  • @saitech2452
    @saitech2452 2 роки тому

    Hello sir these type of video are really helpful can you make more of this

  • @michaeleduardogarciaabello2180
    @michaeleduardogarciaabello2180 2 роки тому

    beautiful, thank you so much for sharing Errichto :)

  • @anupestuff
    @anupestuff 2 роки тому

    This is very helpful! Can you tag some more practice problems for loops? I sometimes struggle with getting the loops correct in interview pressure.
    I see the problem you solved was category :( implementation + strings + 900). Are those the categories I should look for?

  • @felipe2637
    @felipe2637 2 роки тому

    Hi Errichto, thank you for the amazing video. A quick question,
    Wouldn't it be better to define an integer outside of the scope of the loop saying
    int z = n - k;
    Or
    const int z = n - 1; ?
    I remember some college teacher telling me/reading somewhere/have the feeling that I should always avoid letting operations inside the loop definition, since it will likely lead to performance issues. Also, other thing i remember reading about, is that, when possible, we should choose instead of = since the former performs only one operation and the later does two. In the example you showed us, I totally and understand the use of

  • @Angtong_Enghee
    @Angtong_Enghee 2 роки тому

    Long time back bro

  • @alpt1980
    @alpt1980 2 роки тому

    omg 10 mins ago i checked if ur back :D

  • @thehummelballkickers4485
    @thehummelballkickers4485 2 роки тому

    Hey there errichto, is this a new series on your yt channel? This seems good to help those get into cp.

  • @gauravsrivastava17
    @gauravsrivastava17 2 роки тому

    Waiting for more tutorial like these

  • @rajendrakumar-oj3km
    @rajendrakumar-oj3km 2 роки тому

    Please continue this

  • @atharvasalokhe5168
    @atharvasalokhe5168 2 роки тому

    Errichto is back 🤩

  • @strzix
    @strzix 2 роки тому +1

    Completely off topic, but can you tell me what program you were using for the notes at the beginning of the video? It looked like OneNote, but you are on ubuntu; unless it might be the electron version?

  • @anish9376
    @anish9376 2 роки тому

    I subscribed... Cause last time I came I forgot😅 I watched and went.. But now I am gonna implement... And I am gonna leave this comment here to make sure I keep doing what I said.. That is implementing what I learned.

  • @SkrtlIl
    @SkrtlIl 2 роки тому

    Instead of counting you could also have a flag initialized to true and &= it with the condition, then check if it stayed valid

  • @sandun4983
    @sandun4983 2 роки тому

    Thank you so much sir.Can you make more tutorials like this...

  • @adityadas5015
    @adityadas5015 2 роки тому +3

    When talking about generalization, the input may not be binary, so I think sliding window is a more general solution.

    • @Errichto
      @Errichto  2 роки тому +3

      My code works for any string. The sliding window is indeed better though - because it's O(N) instead of O(N*K).

  • @yasser_hussain
    @yasser_hussain 2 роки тому

    Hi Errichto I am long time follower of yours. Please also make content for intermdiate level people. These beginner videos can get a lot of views but the beginners wont stick around your channel for very long. :)

  • @saurabhsingh-xi7nk
    @saurabhsingh-xi7nk 2 роки тому

    So Errichto finally reset his UA-cam password. 🎊

  • @rifatchowdhury3683
    @rifatchowdhury3683 2 роки тому

    @Errichto need more videos from you.
    (Where is your glass?)

  • @abrarmasumabir3809
    @abrarmasumabir3809 2 роки тому

    Please make a entire series of algorithms for beginners....high schooler like me needs it...please

  • @guidoenr
    @guidoenr 2 роки тому

    I missed you a lot

  • @itz_me_imraan02
    @itz_me_imraan02 2 роки тому

    Want a DSA series 🙏

  • @nicolasceron3222
    @nicolasceron3222 2 роки тому

    moreee a series

  • @salambience12
    @salambience12 2 роки тому

    😈Thanks

  • @josealejandrovaroncarreno1692
    @josealejandrovaroncarreno1692 2 роки тому

    @Errichto thanks for explicate problems easy
    creo que es bueno para los que estan comensado, como yo 😂

  • @maxzaranik2359
    @maxzaranik2359 2 роки тому

    @Errichto what is the name of the program in which you write something to write code?

  • @sohaibeboss
    @sohaibeboss 2 роки тому

    Long time no see

  • @nishaternotes
    @nishaternotes 2 роки тому

    I feel more comfortable with this, but here you have discussed "for loops" for this topic otherwise I always use "while loop".
    Is this called "block size technique" or something?
    for (int i = 0;i < N; ){
    int counter = 0;
    char c = s[i];
    for ( ;i < N && c==s[i];i++){
    counter++;
    }
    if (counter >= k)return true;
    }
    return false;
    // Time: O(N)

    • @Errichto
      @Errichto  2 роки тому +2

      Yes, that's it.
      Fors and whiles are very similar. It would be fine to just use a while-loop in your code.
      That being said, there is a more concise solution. One for-loop is enough. I will talk about it in the next video.

  • @FishFind3000
    @FishFind3000 2 роки тому +1

    I forgot I was even subbed to you

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 2 роки тому

    It is interesting, I use to program as s+i == s+i+1. Anyway, it is very dangerous to use s+i+something, you can easily go beyond of the array boundary.

  • @Anonymous-kp7xv
    @Anonymous-kp7xv 2 роки тому

    Make some hard series on dp and graph,binary search ....we need it.

  • @patrykfilipak5297
    @patrykfilipak5297 2 роки тому

    True.

  • @igorleitner8814
    @igorleitner8814 2 роки тому +4

    Hey, I am starting with my competetive programming journey with python (I know c++ is better but i am not planning on winning ICPC, i just want to have a strong CP background so it helps me with interviews and to just master python) and i cant really find any help on CP thats written in python. Like every tutorial is being learned in C++ and it kinda discouraged me from CP. Can you suggest any website, tutorial or anything that teaches python for CP. If you know of any you can also suggest some books but id prefer something online for now while im still a begginer although if there is a must have book for python than please let me know. Books or courses can also be in polish.

    • @divingdabbler2035
      @divingdabbler2035 2 роки тому +2

      "Data Structures and Algorithms in Python by Michael H. Goldwasser, Michael T. Goodrich, and Roberto Tamassia" is a nice book

    • @Errichto
      @Errichto  2 роки тому +4

      You should be fine if you can understand C++ code. Most tutorials are about algorithms and the implementation doesn't matter that much - it's there just to show some details.

  • @mr.k6831
    @mr.k6831 2 роки тому

    Hello, erricto sir. I do not understand how to determine the k value in CSES Repetitions. I am able to solve it in such a way.
    int n = s.size();
    int ma = 0;
    for (int i = 0; i < n;) {
    int cnt = 0, j = i;
    while(s[i] == s[j]) cnt++, j++;
    i = j;
    ma = max (ma, cnt);
    }
    cout

    • @Errichto
      @Errichto  2 роки тому +1

      Yes, that's a correct solution for repetitions. Try to use it in CF Football now.
      (I said that finding block size is an alternative solution)

  • @MoeLester4444
    @MoeLester4444 2 роки тому

    Its been a long time

  • @gnet888
    @gnet888 2 роки тому

    Cool

  • @piyushv5739
    @piyushv5739 2 роки тому

    Never been so early 🔥

  • @pantherwolfbioz13
    @pantherwolfbioz13 2 роки тому +7

    Why something like this suddenly? From dp and graph algorithms to for loops is a big leap😂😂

    • @Errichto
      @Errichto  2 роки тому +7

      My last Twitch live-stream was a month ago and it had similar difficulty. I want to cover some topics and problems from the "100 Easy Problems" training contest codeforces.com/blog/entry/97058
      Don't worry, I will get back to non-easy topics eventually.

    • @masternobody1896
      @masternobody1896 2 роки тому +1

      @@Errichto how do you write code out of thin air do you have magic power or something?

    • @masternobody1896
      @masternobody1896 2 роки тому +1

      @@Errichto I tried to make a snake game with never seen a code I could not do it. you can write it easily how do you do that? can you give us advice

    • @Errichto
      @Errichto  2 роки тому +5

      @@masternobody1896 It's just like using English. If you know it well enough, you can type whatever you think.

    • @FishFind3000
      @FishFind3000 2 роки тому +3

      @@masternobody1896 you cant just jump into high level coding like making a game with having never coded before. That's like trying to do quantum physics and you don't even understand gravity.
      You need to start from the beginning and learn your way there. Ive taken 2 years of programming classes and I still don't know a lot.

  • @bsal5347
    @bsal5347 2 роки тому

    I have a question . In competitive programming , do they really care about the time complexity or not ?

    • @KiraleosAkis
      @KiraleosAkis 2 роки тому +1

      It only matters if your solution doesn't complete in a given timeframe. Your solution can have a very bad time complexity, but if it still completes in the timeframe given by the problem author, then it doesn't matter.