Тёмный

Subarray Divisibility (AtCoder) 

Errichto Algorithms
Подписаться 306 тыс.
Просмотров 23 тыс.
50% 1

Given a string with digits, count subarrays divisible by 2019. This is a programming problem from Atcoder atcoder.jp/con.... It's a bit similar to "Subarray Sum Equals K" which I already covered • LeetCode Day 22 - Suba...
Check out Code NCode channel for a good tutorial on math needed for competitive programming / channel & codeforces.com...
Subscribe for more educational videos on algorithms, coding interviews and competitive programming.
- Github repository: github.com/Err...
- Live streams on 2nd YT channel and on Twitch: / errichto2 & / errichto
- FB and Twitter: / errichto & / errichto
- Frequently Asked Questions: github.com/Err...
#Coding #Programming

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 152   
@deepraval3406
@deepraval3406 4 года назад
Codechef may long challenge Chef and bitwise product. After contest ends*
@tanujnamdeo
@tanujnamdeo 4 года назад
TRPLSRT Also. I got partial acceptance in both of them
@yashdeepbachhas6438
@yashdeepbachhas6438 4 года назад
yes please Errichto can you please look over may long challenge 2020
@coolankush100
@coolankush100 4 года назад
Rachit Jain is covering the bitwise question. Check out his channel
@Errichto
@Errichto 4 года назад
As per this request, I wanted to cover the "Chef and bitwise" problem and record a video today. As @Ankush says, a fellow youtuber Rachit Jain already made a video so go watch him ;) ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE--F7cHQ-gWS4.html
@godspeed2816
@godspeed2816 3 года назад
really cool technique. These type of questions shows the beauty of cp.
@GauravDwivedi-yr8iq
@GauravDwivedi-yr8iq 4 года назад
You are only and only genuine programming youtuber. Most of them turned into late night shows.
@Errichto
@Errichto 4 года назад
Your handle suggests that you like at least one other youtuber :D
@Rajansharma-cd3cd
@Rajansharma-cd3cd 4 года назад
@@Errichto 😂😂🤣
@Rajansharma-cd3cd
@Rajansharma-cd3cd 4 года назад
Talking about gaurav sen🤣🤣
@kunal_chand
@kunal_chand 3 года назад
Whats the logic behind : "answer += cnt_suf[suf]"
@vishwanthkandibanda4711
@vishwanthkandibanda4711 4 года назад
nice development of answer
@MizardXYT
@MizardXYT 4 года назад
The forwards algorithm is very similar to your backwards algorithm. You only have to change two lines after reversing the loop. power_of_ten = 202 * power_of_ten % MOD; // 202 = 10^-1 (mod 2019) suf = (suf + digit * power_of_ten) % MOD;
@Errichto
@Errichto 4 года назад
In almost every problem, there is some harder or more complicated solution. I wanted to avoid modular inverse in my video if possible ;)
@shivankchaturvedi4817
@shivankchaturvedi4817 4 года назад
Hey bro please make different series full of practice questions specially dedicated to graphs and recursion please.....
@codeWithBatta
@codeWithBatta 4 года назад
Errichto can you make a video for those who cannot understand blogs ... i mean how to learn then ? via videos ?
@IbrahimElmourchidi
@IbrahimElmourchidi 4 года назад
thank you very much : )
@nachiketkanore
@nachiketkanore 4 года назад
How to approach constructive algorithm problems?
@himanshushekharchaurasia5832
@himanshushekharchaurasia5832 4 года назад
What are constructive algorithms?
@nachiketkanore
@nachiketkanore 4 года назад
@@himanshushekharchaurasia5832 You need to generate some data depending on some constraints given
@himanshushekharchaurasia5832
@himanshushekharchaurasia5832 4 года назад
@@nachiketkanore could you please link me a few questions to get started? (If you don't mind)
@nachiketkanore
@nachiketkanore 4 года назад
@@himanshushekharchaurasia5832 Recent codeforces div. 4 last two questions
@himanshushekharchaurasia5832
@himanshushekharchaurasia5832 4 года назад
@@nachiketkanore thank you so much. I'll check them out.
@rishabhagrawal9987
@rishabhagrawal9987 4 года назад
could you do codechef may challange 2020 last 4-5 questions they are pretty good questions ....it would a great help
@LeandroCoutinho
@LeandroCoutinho 4 года назад
Very nice Errichto. But why do use MOD here: power_of_ten = 10 * power_of_ten % MOD; ?
@mohammedsanaullah1065
@mohammedsanaullah1065 4 года назад
Well mod here is a reference to 2019 number, So rather than typing 2019 everywhere why not use a simple MOD
@vishwanthkandibanda4711
@vishwanthkandibanda4711 4 года назад
make nanobots in may long challenge code chef
@sakshamsethi4123
@sakshamsethi4123 4 года назад
DP on trees please!
@siddharthgaur7919
@siddharthgaur7919 4 года назад
pot = 10 * pot % M, why do we have mod M here?
@Errichto
@Errichto 4 года назад
To avoid overflows
@laskdkmgful
@laskdkmgful 4 года назад
Want to know why power_of_ten also need to mod 2019? Thanks!
@barongrmel3797
@barongrmel3797 4 года назад
Also because it will overflow because N is 1e5.
@himanshubisht349
@himanshubisht349 4 года назад
doubt: line 20 => power_of_ten = 10 * power_of_ten % MOD, why we have to take MOD?????
@xTofitaZz
@xTofitaZz 4 года назад
because A*B mod something = A mod something * B mod something, the % mod for power of then is unecessary but really smart optimisation
@himanshubisht349
@himanshubisht349 4 года назад
that's fine, but let's say after some iteration power_of_ten will become 1000, then on next iteration power_of_ten will be (1000 * 10) % 2019, which equals to 1924, what does this mean. i didn't understand the meaning of this line, maybe its silly but i didn't get it :)@@xTofitaZz
@xTofitaZz
@xTofitaZz 4 года назад
the thing you have to understand is modular arithmetic, the reste of a*b % 2019 is the same thing as a%2019 * b%2019
@Errichto
@Errichto 4 года назад
We care about subarray value modulo 2019 so we compute it modulo 2019, including all variables contributing to our subarray value.
@himanshubisht349
@himanshubisht349 4 года назад
@@Errichto now I got it, Thankyou
@vaibhaves
@vaibhaves 4 года назад
Codechef contest editorials, please give this a few likes so that erichitto can see!
@cahyapython
@cahyapython 4 месяца назад
10:52 , shouldn't you check if(s[i]='0') you cant add answer to it? ex: 2019 will be 2 not 1 because 019 and 19 have the same suffix edit: lol my bad, i didnt read the problem constraint carefully
@semalsherathia1034
@semalsherathia1034 4 года назад
Really Needed To Understand This Problem For A Long Time. Thanks Errichto 🔥
@baziusok
@baziusok 4 года назад
8:59 said every programmer
@ajitshiva9193
@ajitshiva9193 4 года назад
Thanks errichito for tell about codeNcode... He is very hardworking to help beginners like me
@Mobarakexplains-
@Mobarakexplains- Месяц назад
but if I have a sample like s="111" and number is 2 then the provided method will output 3 although it should output 0 because no substring is divisable by 2 , can anyone explain to me if I 'm wrong ?
@saketthota398
@saketthota398 4 года назад
Let us consider s="019" , the value of suf=19 when we are at "1" and after that cnt_suf[19] will be one , then for "0" again suf value will be suf=19 and now answer will be incremented , but actually the answer should be 0. @Errichto can you explain this ?
@CSBVASAMMANJUNATHKASHIRAM
@CSBVASAMMANJUNATHKASHIRAM 4 года назад
yes please!
@shubhangsingh4161
@shubhangsingh4161 4 года назад
S is a string consisting of digits from 1 through 9 - This is the statement from the question So S will not be containing the digit 0 so your test case will not be asked here. But I do think if this was given in question that S can contain 0 too than quickest possible way would be O(n^2)
@saketthota398
@saketthota398 4 года назад
@@shubhangsingh4161 Oh..Thanks not noticed actually : )
@samarthgupta9983
@samarthgupta9983 4 года назад
Is there a way to handle the case when the modulo is not coprime with 10?
@YUVRAJSINGH-iz9gt
@YUVRAJSINGH-iz9gt 4 года назад
Sir I didn't get that how you wrote count of suffix equal to answer ...? Anybody plzz explain
@subodhrai7614
@subodhrai7614 4 года назад
Thank you for this video I am really struggling for the understandable logic😁
@starc701
@starc701 4 года назад
bro u r so down to the earth person. i did'nt watched this video but liked it, because nice people are very less in the world.
@harshmunda4959
@harshmunda4959 4 года назад
A day in the life of errichto
@ZaheerAbbas.
@ZaheerAbbas. 4 года назад
Please teach data structures and algorithms from the beginning & also please teach us how to master all these through competitive programming . Please. Guys please like this comment if you are a beginner & want the videos on these topics
@jindammadhav2189
@jindammadhav2189 4 года назад
can anyone explain me why he is adding cnt_suf[suf] to answer.he should keep track of numbers only divisible by 2019 right
@Errichto
@Errichto 4 года назад
I'm looking for two suffixes with the same value modulo 2019. Subtracting them gives a number divisible by 2019
@jimwoodward7293
@jimwoodward7293 4 года назад
Thanks Errichto -- very clearly explained, I always learn something from your videos.
@gentleman7060
@gentleman7060 Год назад
Video was cool but still didnt find any reason why from prefix it should not work. This is 100% unclear to me.
@gentleman7060
@gentleman7060 Год назад
Why man! I can go from prefix and each time doing like s[i]-'0' * 10 + new_ - '0' it should work the same.
@josuealeman853
@josuealeman853 2 года назад
It seems that Code NCode doesn't exist anymore :(
@ayush.kumar.13907
@ayush.kumar.13907 4 года назад
how to solve the harder version of this problem when M is not coprime with 10, eg, like you showed with M=50?
@KappaHakka
@KappaHakka 4 года назад
I think you could do it in O(N*MOD) but I'm not sure
@happyhappyguy5034
@happyhappyguy5034 4 года назад
If M =50, the question will be very easy. Because for example 149820, we know that 100000, 40000, 9000, 800 must be divisible by 50. Also you will have to handle the cases where the first two digit is 50 or 00, which should be pretty easy. Complexity is O(n)
@happyhappyguy5034
@happyhappyguy5034 4 года назад
Also whether M is coprime or not with 10 doesn't matter in this problem as it doesn't affect how you compute the prefix or suffix. So solution in the video should work
@wasimahmad4813
@wasimahmad4813 4 года назад
Handle m = 2,5,10 cases differently for 2,5,10 counting number of substrings divisible by them is easy because for 2 we only care about the last digit being divisible by 2. Loop from backwards and count number of (2,4,6,8,10) found till now from current digit to all of these digits found now will be divisible by 2 so increase answer by count. Likewise for 5 and 10
@shreyasingh1258
@shreyasingh1258 3 года назад
what to do if k=8, it is not coprime with 10.please reply
@boas_
@boas_ 5 месяцев назад
you can loop over the string and check if the last three digits are divisible by 8. If so, all substrings ending with those 3 digits will be divisible by 8
@techbarikcom
@techbarikcom 4 года назад
Great video! Helps me a lot
@mananshah7379
@mananshah7379 4 года назад
Morse Code - Codeforces
@mingmingtan1686
@mingmingtan1686 4 года назад
Hi Errichto, is it possible to cover the solutions to 2019 Kick Start Round A Contention and 2019 Kick Start Round C Circuit Board problems. I am struggling to understand the analysis provided. Thank you.
@ambarishbanerjee414
@ambarishbanerjee414 4 года назад
Hey , can you cover the may Codechef questions . The bitwise question was still easier but after that the questions were really really difficult. Specifically please cover the question on sorting vases and and the one on buying strings. The editorial talks of aho corassick and building automatons but can it be done easily ? Would like to know your approach. Please do if possible atleast one of them
@szarusz
@szarusz 4 года назад
Don't you actually have to add cnt_suf[0] to ans? It seems to me that the subarrays that are already divisible should count 2: once for all the combinations among them (as with any other) and once for themselves.
@Liocattechtips
@Liocattechtips 4 года назад
*PLEASE ANSWER ME* Hi Errichto, I would like to know something from you that is right now I am learning node.js and I wanted to know that what is the right time to start with comparative programming for interview perpose. Should I learn node.js completely and then start with competitive programming. Or should I start right now along with my node.js *By the way I am learning node.js on my on. SO PLEASE ANSWER ME.
@Liocattechtips
@Liocattechtips 4 года назад
@@ayushagrawal1975 I know he is a God of codeforces and I also know at what level he do competitive programming . And 🥰 thanks for the advise I will start my compatitive programming very soon.
@ayushagrawal1975
@ayushagrawal1975 4 года назад
@@Liocattechtips sorry for that kind of reply...anyways its good to start competitive programming early and daily do some questions or learn some algo.
@Liocattechtips
@Liocattechtips 4 года назад
@@ayushagrawal1975 its ok bro at least you replied though. Wish me a bright future as I wish for you my Indian bro.
@himanshushekharchaurasia5832
@himanshushekharchaurasia5832 4 года назад
@Liocat hey you should do competitive programming side by side man. It really polishes your problem solving skills and the data structures and algos that you learn will be helpful in interview too. I'm also learning node.js and doing cp side by side.
@Liocattechtips
@Liocattechtips 4 года назад
@@himanshushekharchaurasia5832 thanks bro 🤩😍
@aser2535
@aser2535 4 года назад
Do leetcode hard problems boil down to discrete math? How important is discrete math when it comes to solving Coding problems?
@mackexr
@mackexr 3 года назад
Hey errichto would love it if you made a video on open.kattis.com/problems/matrica very confusing for me since for an efficient solution you should only calculate the required colums.
@mdisrafil2491
@mdisrafil2491 4 года назад
How Can use this method for divisiblity of 4 . codeforces.com/problemset/problem/628/B
@gatoradeee
@gatoradeee 4 года назад
Alternatively you could reverse() the number string first and then use prefix sums.
@himanshu298
@himanshu298 4 года назад
I think you should give a shoutout to “take u forward” also. He is creating excellent codeforces editorials.
@singerpranavmodi
@singerpranavmodi 2 года назад
Hi Errichto. Can you please explain why for String s = 7325 and MOD = 2 code gives ans as 6 but it should give 3 ?
@boas_
@boas_ 5 месяцев назад
I think the problem is 2 is not coprime with 10
@irenef22
@irenef22 4 года назад
Thank you Errichto. Great video! As all others on this channel.
@AMANSHARMA-lp2ry
@AMANSHARMA-lp2ry 4 года назад
which IDE for c++ you personally use
@Daniel-xaogjeyh
@Daniel-xaogjeyh 4 года назад
why does M has to be coprime with 10?
@Errichto
@Errichto 4 года назад
otherwise the subarray might be not divisible by 10 but our difference would be divisible. Example: M = 25, subarray = 5, we append two zeros and have a difference of 500 instead. It's divisible by M and we would incorrectly count it.
@Daniel-xaogjeyh
@Daniel-xaogjeyh 4 года назад
@@Errichto got it thanks
@divyanshgoel4850
@divyanshgoel4850 4 года назад
Can you make a video on segment trees and fenwick trees?
@krasimiracle
@krasimiracle 4 года назад
Saw you solving "221.Maximal square" the other day. It would be interesting if you can solve "85.Maximal Rectangle" which is similar but a lot harder. The solution with histogram is very cool.
@Errichto
@Errichto 4 года назад
I'm aware of that problem and will eventually cover it. But that's enough of Leetcode for quite some time.
@Ecylo
@Ecylo 4 года назад
Poruszyłbyś temat maximum agreement subtree problem?
@LatinoGames
@LatinoGames 4 года назад
Thanks for doing this videos
@sasmitshubham9424
@sasmitshubham9424 4 года назад
codechef long challenge solution; after contest ends;
@darkcrusader739
@darkcrusader739 4 года назад
I didn't understand answer += cnt_suf[suf] Is the vector initialised to zero in the beginning? Thank you
@Errichto
@Errichto 4 года назад
Yes, the vector is filled with 0 by default.
@florentinalexandruiftimie8214
@florentinalexandruiftimie8214 4 года назад
best teacher ever
@kalaivanan6543
@kalaivanan6543 4 года назад
hi i dont understand line 23 and 24 . what does that ++ operation will do in vector ?? sorry i dont kow c++ im a python coder!! it will be good if some one covert the same solution to python
@shivangtiwari8694
@shivangtiwari8694 4 года назад
It is the increment operator. It will increase the value of the variable(in this case the element of the vector) by one.
@vivekdubey2068
@vivekdubey2068 4 года назад
Very natural and excellent explanation. Thanks Errichto! But you are making me lazy though.
@Errichto
@Errichto 4 года назад
Lazy? :D You still need to practice if you want to get good. Watching videos alone is not enough.
@unk5037
@unk5037 4 года назад
Leetcode trapping rain water 2
@MS-gt9yu
@MS-gt9yu 4 года назад
please make video on string theory. string algorithm and some question based on them ..thanks for this video.
@Errichto
@Errichto 4 года назад
String theory stinks. (I just don't like it)
@raihankhan197
@raihankhan197 4 года назад
I just copied your code and tried 20192019 here we should get answer 2. As 2019 and 2019 both subarray are divisible by 2019. But it gives answer 5. Where is the problem? Am i gettung it wrong?
@PriyaSingh-sw9lf
@PriyaSingh-sw9lf 4 года назад
5-> 2019,2019,20192019,0,0
@raihankhan197
@raihankhan197 4 года назад
Thanks. Now it make sense to me.
@mahdi7d1rostami
@mahdi7d1rostami 4 года назад
I assume when we think about time complexity we don't consider some functions. For instance the % operator follows an alghorithm to calculate and return value but we dont consider it in our code's time complexity. My question is which parts don't effect time complexity strongly that we can ignore them?
@TheVovkaGAMER
@TheVovkaGAMER 4 года назад
% operator time complexity is constant
@Errichto
@Errichto 4 года назад
In short, CPU can handle all operations on primitive types like int and char in constant time.
@rajdave9862
@rajdave9862 4 года назад
Share discode link
@rajav4861
@rajav4861 4 года назад
Please solve Codechef Challenges
@himanshubisht349
@himanshubisht349 4 года назад
It will always be more helpful. if you do a dry run of the code. :)
@Errichto
@Errichto 4 года назад
Hmm, maybe I will start doing that for easy problems in the future.
@vigneshsekar2001
@vigneshsekar2001 4 года назад
Needed it thanks
@ANKITVERMA-fl1zn
@ANKITVERMA-fl1zn 4 года назад
Can you please make a video on this problem I just learned fft and was stuck at this problem for long it would be really helpful codeforces.com/problemset/problem/993/E
@Errichto
@Errichto 4 года назад
I will cover FFT eventually but it won't be easy for me to do. You need to wait a few weeks at least ;)
@ANKITVERMA-fl1zn
@ANKITVERMA-fl1zn 4 года назад
@@Errichto Thanks for considering my request. I am waiting eagerly it.
@ajitshiva9193
@ajitshiva9193 4 года назад
Can we also find the multiple of 2020 or any numbers by this method will it works????
@Errichto
@Errichto 4 года назад
Not in a simple way. The last few digits would matter a lot in that case. I guess it can be handled but just needs more work.
@CarrotCakeMake
@CarrotCakeMake 4 года назад
2020 = 2^2 * 5 * 101. So you want to count all (i, j) such that simultaneously A[i .. j] = 0 mod 101 and A[i .. j] = 0 (mod 2^a 5^b). To check if A[ .. j] is divisible by 2^a 5^b requires only looking back max(a, b) digits. So only record the Suffix[j] % M values in the collection if A[ .. j] is divisible by 2^a5^b and record how far back you have to look to make that divisibility happen, and then only accept the Suffix[i] = Suffix[j] (mod 101) such that i is sufficiently preceding the j to make the A[i .. j] divisible by 2^a 5^b. If that is confusing, try asking yourself first how you would solve the problem for M = 1300, that is, simultaneously finding A[i .. j] = 0 mod 13 and A[i .. j] = 0 mod 100.
@vicky.3267
@vicky.3267 4 года назад
Challenge: Show shortly that your solutions on problems will pass every test before you submit !
@Errichto
@Errichto 4 года назад
I'm not sure what exactly you would like to see.
@vicky.3267
@vicky.3267 4 года назад
@@Errichto Prove that your solution works for every possible test case before you submit it.
@II_xD_II
@II_xD_II 4 года назад
IoI prepration path algos tips
@manishraj5911
@manishraj5911 4 года назад
Is it pigeonhole principle ?
@Errichto
@Errichto 4 года назад
I see no similarity whatsoever. Why do you ask that?
@manishraj5911
@manishraj5911 4 года назад
Sorry I got confused. I compared this problem with the problem where a array is given of a certain size and it is asked to find number of subarrays divisible by the size of the given array
@Errichto
@Errichto 4 года назад
@@manishraj5911 I don't think that your other problem is related to pigeonhole either :D
@tomriddle2427
@tomriddle2427 4 года назад
Hey Errichto I have a question, I don't know c++ lang but I know python but on every competitive coding sites/platform I have seen the coders mostly use c++ infact you also (some also use java, like petr) I know the reason behind using c++ it is very fast, vast STL library etc. and on the other hand Python is slow and there are various other cons about it. So what do you suggest should I learn c++ or go with python or learn any other new lang which has both the combination of c++ and python language (like which will help me in CP and as a developer also in future). There are various news coming about different programming language which will replace python so keeping all this in mind what do you suggest, plz reply if you see the comment and I thought this would be the right place to ask my question as you are very active here. Thanks.
@askaraskar1472
@askaraskar1472 4 года назад
I think if you want to compete in high ranks cp, you should learn c++, but if your aim is learn solving problems and become a developer, python is enough. Imho
@tomriddle2427
@tomriddle2427 4 года назад
@@askaraskar1472 thanks man
@Errichto
@Errichto 4 года назад
C++ is needed if and only if you want to be one of the best in CP.
@uzdik.student
@uzdik.student 4 года назад
I had exactly the same question when I started CP and learned C++ just by solving CP problems, it's not difficult at all
@tomriddle2427
@tomriddle2427 4 года назад
@@uzdik.student plz guide more
@HarmoniusE
@HarmoniusE 4 года назад
It would’ve been great if you added some background music such lofi hip hop or whatever
@gauravnimrani7021
@gauravnimrani7021 4 года назад
no
@HarmoniusE
@HarmoniusE 4 года назад
Gaurav Nimrani can you formulate your argument why not?
@gauravnimrani7021
@gauravnimrani7021 4 года назад
@@HarmoniusE it becomes a little irritating sometimes
@HarmoniusE
@HarmoniusE 4 года назад
Gaurav Nimrani just sometimes(and I didn’t say to make the music loud but a little background music won’t be distracting)
@Errichto
@Errichto 4 года назад
You can open some music in the background yourself ;)
Далее
Three Leetcode problems - Queens, Dice, Frequency
19:31
Subarray Sums Divisible by K - Leetcode 974 - Python
16:41
FATAL CHASE 😳 😳
00:19
Просмотров 1,3 млн
КВН 2024 Встреча выпускников
2:00:41
Binary Exponentiation
15:13
Просмотров 99 тыс.
[CSES][Sorting and Searching] Subarray Sums II
19:58
Просмотров 4,1 тыс.
SUBARRAY SUM EQUALS K | LEETCODE 560 | PYTHON SOLUTION
16:32
C++ Bitsets in Competitive Programming
15:35
Просмотров 119 тыс.
Leetcode Biweekly #34 Screencast & Commentary
20:25
Просмотров 4,1 тыс.
Fast Inverse Square Root - A Quake III Algorithm
20:08
FATAL CHASE 😳 😳
00:19
Просмотров 1,3 млн