YouTube2Text

1.5.3 Time Complexity of While and if #3 — Transcript

by Abdul Bari · 3,188 words · 391 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hi all the way I have prepared a video
  2. 0:03how to find the time complexity if there
  3. 0:06is a for-loop now the question is how to
  4. 0:10analyze a loop that is while loop or
  5. 0:14conditional statements see basically in
  6. 0:19C language there are three loops while
  7. 0:23and do-while there is a difference
  8. 0:26though while will execute minimum one
  9. 0:29time but follow fine while loop I say
  10. 0:33that they are same whatever you can do
  11. 0:37using for loop you can do using while
  12. 0:39loop and vice versa but before C
  13. 0:44language he has languages used to
  14. 0:46provide for loop in a different way let
  15. 0:48us see that follow for I assign one to M
  16. 0:56do some statements inside and this is :
  17. 1:04here
  18. 1:05this was the syntax in Pascal language
  19. 1:08now this loop says that I should take
  20. 1:12the values from 1 & 2 and write so
  21. 1:18always it will increase by one at a time
  22. 1:23all right
  23. 1:25so if you want to increase I by two
  24. 1:27times then you should say step two now I
  25. 1:35will be changing by two every time so I
  26. 1:38is 1 then it will become 3 then 5 and so
  27. 1:41on so it will take Y is 1 then 3 then 5
  28. 1:45and so on but anyway I is incrementing
  29. 1:49linearly it is increasing all right so
  30. 1:54this was the only loops that were
  31. 1:56available in those days so that time
  32. 1:58follow and while loop very different
  33. 2:02then when you analyze this for loop
  34. 2:04already we have seen what is the time
  35. 2:06taken if I don't have this step steps
  36. 2:09let us say 1 only then this will execute
  37. 2:12time and this really good for n plus one
  38. 2:14time does the common thing and if you
  39. 2:19have while loop in this case then in a
  40. 2:23language if you have while loop then
  41. 2:26don't know what the condition is it will
  42. 2:30repeat as long as the condition is true
  43. 2:33and it will stop when the condition is
  44. 2:35false so we have to study this loop and
  45. 2:39find out when it is going to stop and
  46. 2:41how many times it is going to iterate
  47. 2:43based on that we have to do analysis and
  48. 2:46in this type of course interacts blindly
  49. 2:49for for loop means n no other time
  50. 2:53except n in this type of syntax and when
  51. 2:57the old languages were used so mostly in
  52. 3:01algorithms some books you will find this
  53. 3:03loop is use so it is an blindly and but
  54. 3:06while you cannot say and unless you
  55. 3:09studied thoroughly and in C language we
  56. 3:14have dou Y which is same as this while
  57. 3:18except that it will execute even if the
  58. 3:21condition is false it will execute for
  59. 3:24one time because it is post tested loop
  60. 3:27post check the loop first it will
  61. 3:30execute the statement inside then
  62. 3:33afterwards it will check the condition
  63. 3:35so if all the D condition is false in
  64. 3:37the beginning only it is false so this
  65. 3:39type will execute one time and this will
  66. 3:41not execute at all but in old languages
  67. 3:46they used to be a loop called repeat
  68. 3:51some statement inside and under some
  69. 3:57condition now these loops were different
  70. 4:03repeat until loops are different this
  71. 4:06will repeat as long as a condition is
  72. 4:09false and once the condition is true it
  73. 4:14will stop so it is similar to do while
  74. 4:17how minimum one time the statement is
  75. 4:22but it is different compared to Dubai
  76. 4:25how do while we'll execute as long as
  77. 4:28condition is true repeat until we'll
  78. 4:31execute as long as condition is false it
  79. 4:34will stop when the become condition
  80. 4:36becomes true do this until this happens
  81. 4:39so if it happens you have to stop so if
  82. 4:43you take English sentence statement
  83. 4:46repeat until this happens so if it
  84. 4:49happens you have to stop if it is not
  85. 4:50happening you have to go on continuing
  86. 4:52so that's the difference between them
  87. 4:54now all these three loops all these
  88. 4:57three loops you have to study them then
  89. 4:59only you can give the time complexity
  90. 5:01their time complexity can be n log N or
  91. 5:05oten it can be anything but for loop we
  92. 5:09can directly say it's order of n now let
  93. 5:12me write few pieces of code or snippets
  94. 5:15where I will use while loop I have
  95. 5:20written a piece of code using by loop I
  96. 5:23have to analyze how many times this will
  97. 5:26execute what is the time complexity of
  98. 5:28this one I want to know how many times
  99. 5:30this statement will execute initially I
  100. 5:33is zero this will take one unit of time
  101. 5:36an I plus plus how many times this will
  102. 5:39repeat how many times this will repeat
  103. 5:42see I plus plus it is just incrementing
  104. 5:45every time so simply without tracing
  105. 5:47this one I can say that this will repeat
  106. 5:50for n times how many times this
  107. 5:53statement will execute again n times
  108. 5:56only how many times this will execute n
  109. 5:58plus one time how n times the condition
  110. 6:05will be true and one time the condition
  111. 6:07is false so if for example n is a ten
  112. 6:10then this is going to repeat for total
  113. 6:13ten times zero to ten it will stop and I
  114. 6:15become stem and how long this will
  115. 6:18execute as long as I is from zero to
  116. 6:20nine so this n time right and that is
  117. 6:24one extra then what is the time taken by
  118. 6:27this piece of code three and plus two is
  119. 6:30three
  120. 6:31and plus
  121. 6:31- so the function f of n s 3 and +2 and
  122. 6:35it is teed off and now order of n now
  123. 6:41the same piece of code in C language I
  124. 6:44can write like this for I assign 0 here
  125. 6:48then I is less than n I is less than n I
  126. 6:53plus plus I plus plus
  127. 6:59no what is the analysis for this fund it
  128. 7:03will be saying if I consider each and
  129. 7:04everything
  130. 7:05this is execute for one time this will
  131. 7:07execute for n plus one time this will
  132. 7:09execute for n time and this will execute
  133. 7:11for n time total how many 3 n plus 2 but
  134. 7:17when I was using for loop I was saying
  135. 7:19that let us ignore these two and just
  136. 7:21take it as n plus 1 so I was saying that
  137. 7:23it was 2 n plus 1 whatever the function
  138. 7:27may be we are not interested in exact
  139. 7:30formula or exact function we are
  140. 7:34interested in the degree of a function
  141. 7:36so we say order of n now that's it now
  142. 7:40you can see whatever I can write using
  143. 7:42while loop I can write it using for loop
  144. 7:44also now our next piece of code here see
  145. 7:48a assign 1 while a is less than B some
  146. 7:52statement and a is multiplied by 2 every
  147. 7:56time there is no n here then how many
  148. 8:01times it was repeat I don't know
  149. 8:03this depends on this condition as long
  150. 8:05as the condition is true it will
  151. 8:07continue so I don't know how many times
  152. 8:10it's going to run let us trace this one
  153. 8:15is initially 1 right the name is
  154. 8:21becoming a into 2 so that is 1 into 2 so
  155. 8:25it is becoming to the next time again
  156. 8:27this is 2 so 2 into 2 again so this will
  157. 8:31become 2 square the next time again this
  158. 8:34is 2 square into 2 that is 2
  159. 8:37q goes on how many times this is going
  160. 8:42to happen I don't know let us assume
  161. 8:46that somewhere it stops and at that time
  162. 8:49this is going to be two parking so it
  163. 8:53will repeat four K times and that's what
  164. 8:54K I have to find out I don't know how
  165. 8:57many times so I am say King K times now
  166. 9:00I say that it has stopped here so you
  167. 9:03should know when it is stopping it will
  168. 9:05terminate when is greater than or equal
  169. 9:12to B it will terminate when a is greater
  170. 9:15than equal to B so what I am saying I
  171. 9:18has reached till 2 power K so since I am
  172. 9:21saying that a is equal to 2 power K now
  173. 9:24so this is 2 power K is greater than
  174. 9:26equal to B let us make it equal 2 power
  175. 9:29K is equals to B so what is K log being
  176. 9:34base 2 so how many time is going to
  177. 9:37execute log B Times log B Times log B
  178. 9:44times rewrite the time complexity in the
  179. 9:47form of log n NV u storm n what have you
  180. 9:50got the answer in B so let us call B
  181. 9:53itself as n so what is the time taken by
  182. 9:56this algorithm it is order of log n
  183. 10:00that's it this is how I can analyze
  184. 10:06already I have shown this using for loop
  185. 10:09that time I was writing the same thing
  186. 10:12by using for loop like this for a assign
  187. 10:171 is less than B a assign a into 2 this
  188. 10:25portion is here that's all already we
  189. 10:29have seen this in follow ups I have
  190. 10:32shown you this one already we know this
  191. 10:36one that time I was not taking a I will
  192. 10:39take ie and again here also I and this I
  193. 10:43will call it as n and this I will call
  194. 10:46it
  195. 10:46I that's it basically this is not proper
  196. 10:54based on full loop because formants it
  197. 10:57should just go on incrementing or it
  198. 10:59should be decrementing it should not
  199. 11:02multiply I but in case of C language it
  200. 11:05is possible you write anything has
  201. 11:07initialization any condition you write
  202. 11:09anything as updation so these three
  203. 11:11pieces you can write whatever you like
  204. 11:14just make sure that the loop terminates
  205. 11:16after some finite number of steps so
  206. 11:20that's all what all you can do using
  207. 11:22while loop you can also do it using for
  208. 11:25loop in C language and the examples what
  209. 11:29I have given in all the examples I have
  210. 11:32used for loop only but in so for loop
  211. 11:35just you can reframe them as while loop
  212. 11:37let us take one more example again a
  213. 11:40loop is there while loop and in this
  214. 11:42while loop I is starting from N and I is
  215. 11:45dickweed getting divided by two every
  216. 11:48time so again the time will be log and I
  217. 11:52made it as a formula already in the
  218. 11:54previous video so this will be log N and
  219. 11:56the same thing can also be done using
  220. 11:58for loop for I assign n I is greater
  221. 12:04than 1 I assign I by 2 some statement
  222. 12:13inside so this type of statement already
  223. 12:15we have analyzed and for loop that same
  224. 12:18thing is using while loop so the time
  225. 12:21will be seen now here I have a loop let
  226. 12:26us see how many times this will execute
  227. 12:28condition is K is less than n and here
  228. 12:33what is K K is 1 and K is getting
  229. 12:36updated by adding ie every time and I
  230. 12:39use incrementing every time so I don't
  231. 12:41know how many times it's going to
  232. 12:42execute so let us take trays this one I
  233. 12:46and K initially both are 1 so 1 is less
  234. 12:50than n we don't know what n is and what
  235. 12:54happens k assign k plus I so this
  236. 12:56becomes 1 plus 1 that is 2 and I plus
  237. 12:59plus I becomes 2
  238. 13:02now as you in case - 2 is less than n
  239. 13:05the next K assign K + I so 2 + 2 right
  240. 13:11and I becomes 3 the next time again K
  241. 13:19let us say it is less than I so then K
  242. 13:21assign K + I so this is 2 + 2 + 3 + I
  243. 13:25becomes 4 next time it will be 2 + 2 + 3
  244. 13:29+ 4 + I becomes 5 so this is going on
  245. 13:33continuing so how many times I don't
  246. 13:36know how many times let us call it as 4
  247. 13:40M times K already I am using it here so
  248. 13:442 + 2 + 3 + 4 + goes on to M if I make
  249. 13:51this as 1 then you can see that is some
  250. 13:54off and natural numbers so this will be
  251. 13:57M into M plus 1 by 2 roughly plus 1 it's
  252. 14:02strong roughly it is this much now this
  253. 14:06is K value will be this much bit it has
  254. 14:09gone till M and K value this much and
  255. 14:13let us assume it has dropped so what is
  256. 14:17the condition K is less than n it will
  257. 14:19continue when K becomes greater than n
  258. 14:21or equal to n it will stop so I assume
  259. 14:24that K became greater than or equal to n
  260. 14:27so what is K I am saying I have stopped
  261. 14:30K at M into n plus 1 by 2 that is
  262. 14:33greater than equal to n this is roughly
  263. 14:35equal to what M square is greater than n
  264. 14:38so M is what root n approximately at
  265. 14:44this root n so it will repeat for M
  266. 14:48times so what is M root and so the time
  267. 14:51complexity of this one is order of root
  268. 14:55n same thing I can write using for loop
  269. 15:01also for K assign 1 comma I assign
  270. 15:07to initializations condition is what K
  271. 15:12less than n updation is what I plus plus
  272. 15:17and one more updation is there that I
  273. 15:21will do inside the statement and K
  274. 15:25assign k plus I there's the same thing
  275. 15:31how long this is executing route and how
  276. 15:35long route and again one two one two
  277. 15:39condition condition updation updation
  278. 15:43the submission in the submission
  279. 15:45everything is as it is so what i wrote
  280. 15:48using vial can also be written using for
  281. 15:52this is little difficult to read that is
  282. 15:55clear and easy to understand
  283. 15:58all right so already we have seen this
  284. 16:01type of loop now I have written it using
  285. 16:04while loop next this piece of code is
  286. 16:07for finding GCD of two numbers m and n
  287. 16:11now how many times it will repeat this
  288. 16:14will repeat for some time until MN n
  289. 16:16becomes equal it will be repeating when
  290. 16:18they become equal it will stop so when
  291. 16:20they are equal so that is a GCD value
  292. 16:22either you say M or you say n how many
  293. 16:25times it will execute I don't know so
  294. 16:27let me trace if M is equals to 6 and n
  295. 16:31is equals to 3 initially then what
  296. 16:36happens n is greater than n yes so M
  297. 16:39minus n so this becomes 6 minus 3 that
  298. 16:42is 3 and is also 3 this one now they
  299. 16:45became equal so it has executed only one
  300. 16:48time if I take M and n both are 5 5 then
  301. 16:58how many times they are equal it will
  302. 17:00not enter inside so it will execute 0
  303. 17:03times so it is minimum executing 0 or 1
  304. 17:06times let's say m is 16 and n is 2 now
  305. 17:14how many times this will execute MN n
  306. 17:17are not equal
  307. 17:18M is greater than n year 16 is greater
  308. 17:20than to enter inside Emma sign and minus
  309. 17:23n so M becomes how much 14 and this will
  310. 17:26be to only now hanging in M is greater
  311. 17:29than n only so again 2 is subtracted so
  312. 17:32this becomes 2l and this is true only
  313. 17:33again 12 is greater than 2 so this
  314. 17:36becomes 10 and this is true only and
  315. 17:39this becomes 8 + 2 6 + 2 4 2 2 2 nor it
  316. 17:48will stop now the both became equal so
  317. 17:51it will stop how many times it has
  318. 17:54executed 1 2 3 4 5 6 7 7 times so almost
  319. 18:0416 by 2 times so it means it will
  320. 18:07execute for n by 2 times so I can say it
  321. 18:11is order of n so the maximum time taken
  322. 18:16by this algorithm is order of N and what
  323. 18:19is the minimum time taken by this
  324. 18:21algorithm it is 1 means 1 time it is
  325. 18:24executing so minimum time is order of 1
  326. 18:29maximum time is order of n I can write
  327. 18:34the same thing even using for loop
  328. 18:36I'll just convert it into for loop for
  329. 18:40nothing initialized and only the
  330. 18:43condition then no objection I can use
  331. 18:50for loop also anyway this is the GCD one
  332. 18:55of the procedure there are other
  333. 18:56procedures also you can find out that
  334. 18:58and in this procedure at the time I am
  335. 19:00getting it as n by 2 so I wrote it as
  336. 19:02order of n so if it is a while loop then
  337. 19:05I have to study it and find out now same
  338. 19:08thing in C language can also be done
  339. 19:10using for loop so even it is for loop in
  340. 19:12C language you must read as while only
  341. 19:14and study it unless and until it is
  342. 19:17obviously order of M you have to study
  343. 19:20it yes I am taking one example here
  344. 19:23which will show what happens if is there
  345. 19:28just a random
  346. 19:29code I have written here this is not
  347. 19:31meaningful it's not doing anything
  348. 19:34algorithm test is taking some parameter
  349. 19:36and if n is less than 5 just print n if
  350. 19:42n this greater than or equal to 5 then
  351. 19:46enter in the else part and repeat this
  352. 19:51loop so how many times this loop is
  353. 19:54going to execute this will execute for n
  354. 19:57times and what about this this will
  355. 20:00execute for one time that's it
  356. 20:05if algorithm is using some conditional
  357. 20:11statements then the conditional
  358. 20:13statements may decide different amount
  359. 20:16of time in depending on the condition if
  360. 20:19the condition is true then it's going to
  361. 20:21execute just one statement so the time
  362. 20:23is order of one and if the condition is
  363. 20:28false means is greater than five then is
  364. 20:30going into repeat for n times so it is
  365. 20:33part of n so this is best case time and
  366. 20:37this is worst case time so I can say
  367. 20:44that if there are so I can say that if
  368. 20:49there is a conditional statement in an
  369. 20:52algorithm then it can decide or give
  370. 20:54different amount of time depending on
  371. 20:57the condition so may be it may become
  372. 21:00those case or best case or of an
  373. 21:02algorithm it's not necessary it's not
  374. 21:05necessary it means it doesn't mean that
  375. 21:08if if is there is not a little formula
  376. 21:11or rule if it's there then definitely it
  377. 21:13will take that much time it's not
  378. 21:14necessary now I think I will make some
  379. 21:18change here G in the condition now now
  380. 21:20this will execute for n times N greater
  381. 21:23than 5 right now there is no minimum or
  382. 21:27maximum there is no best case or worst
  383. 21:29case if n is greater than 5 it will
  384. 21:31execute that's all so as I said that you
  385. 21:36cannot take it as a rule that if
  386. 21:38conditional statement if s there
  387. 21:40the time will be different all right so
  388. 21:45that's all about the loop and the
  389. 21:46conditional statements while loop and
  390. 21:49the conditional statements this is how
  391. 21:51they have analyzed

About this transcript

This page contains the full transcript of 1.5.3 Time Complexity of While and if #3 by Abdul Bari, generated from the public captions YouTube serves with the video. The transcript has 3,188 words across 391 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.