{"id":72,"date":"2025-05-12T12:18:18","date_gmt":"2025-05-12T06:48:18","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=72"},"modified":"2026-04-04T14:42:39","modified_gmt":"2026-04-04T09:12:39","slug":"time-complexity-and-space-complexity","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/","title":{"rendered":"Time Complexity and Space Complexity in DSA | Explained with Examples"},"content":{"rendered":"\n<p>Time complexity and space complexity, both measure an algorithm&#8217;s efficiency. Where timecomplexity shows how the <strong>running time increases with input size<\/strong>. Space complexity tracks <strong>memory usage<\/strong>.<\/p>\n\n\n\n<p>Both are essential for optimizing algorithms. Especially when dealing with large datasets or limited resources.<\/p>\n\n\n\n<p>In this article, we will explore these two concepts of algorithm analysis. Along with their examples, cases, best practices and more.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-cc1ed405-ebc8-445e-9a8b-0f1742e1e5be\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#0-what-is-time-complexity\" style=\"\">What is Time Complexity?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#1-representing-the-time-complexity-of-any-code\" style=\"\">Representing the Time Complexity of Any Code<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#2-step-by-step-analysis\" style=\"\">Step-by-Step Analysis<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#3-generalizing-for-n\" style=\"\">Generalizing for N<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#4-why-shouldnt-we-use-machine-time-to-evaluate-code\" style=\"\">Why Shouldn&#8217;t We Use Machine Time to Evaluate Code?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#5-why-manual-counting-isnt-feasible\" style=\"\">Why Manual Counting Isn&#8217;t Feasible?<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#6-rules-for-calculating-time-complexity\" style=\"\">Rules for Calculating Time Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#7-lets-see-the-rules-for-calculating-time-complexity-with-an-example\" style=\"\">Let&#8217;s See the Rules for Calculating Time Complexity with an Example<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#8-step-by-step-explanation\" style=\"\">Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#9-1-best-case\" style=\"\">1. Best Case<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#10-2-worst-case\" style=\"\">2. Worst Case<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#11-3-average-case\" style=\"\">3. Average Case<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#12-worst-case-time-complexity\" style=\"\">Worst-Case Time Complexity<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#13-why-focus-on-the-worst-case\" style=\"\">Why Focus on the Worst Case?<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#14-avoid-including-the-constant-terms\" style=\"\">Avoid Including the Constant Terms<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#15-step-by-step-analysis\" style=\"\">Step-by-Step Analysis<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#16-why-ignore-constant-terms\" style=\"\">Why Ignore Constant Terms?<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#17-what-is-space-complexity\" style=\"\">What is Space Complexity?<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#18-example\" style=\"\">Example<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#19-using-arrays\" style=\"\">Using Arrays<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#20-good-coding-practice-avoid-input-manipulation\" style=\"\">Good Coding Practice: Avoid Input Manipulation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#21-points-to-remember-for-competitive-programming\" style=\"\">Points to Remember for Competitive Programming<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#22-key-guidelines-\" style=\"\">Key Guidelines:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/time-complexity-and-space-complexity\/#23-conclusion-time-complexity-and-space-complexity\" style=\"\">Conclusion [Time Complexity and Space Complexity]<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\">Understand by watching video<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Time and Space Complexity? - DSA Python Course 2025 - Part 2 [Hindi] | Code &amp; Debug\" width=\"814\" height=\"611\" src=\"https:\/\/www.youtube.com\/embed\/bLm0wgPU-6A?start=1367&#038;feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-what-is-time-complexity\">What is Time Complexity?<\/h2>\n\n\n\n<p>When solving a problem, there may be <strong>multiple approaches (codes)<\/strong> to achieve the desired result. The concept of time complexity helps us evaluate &amp; compare these approaches.<\/p>\n\n\n\n<p>In a way, allowing us to determine which one is more efficient. It&#8217;s a crucial metric that often becomes a key focus during <strong>technical interviews<\/strong>. Mainly because it demonstrates the effectiveness of a solution.<\/p>\n\n\n\n<p>At first glance, it might seem to represent the time a machine takes to execute a piece of code. However, this is a misconception. It&#8217;s more about the code than the machine.<\/p>\n\n\n\n<p>Time complexity measures how the <strong>time required to execute a code changes <\/strong>as the <strong>size of the input grows<\/strong>. It is independent of the machine used to execute the code &amp; focuses solely on the algorithm&#8217;s behavior with increasing input size. This makes it a universal metric to compare algorithms.<\/p>\n\n\n\n<p>Let\u2019s understand this using the following diagram:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-1024x576.png\" alt=\"\" class=\"wp-image-73\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/time-and-space-complexity-graph.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-representing-the-time-complexity-of-any-code\">Representing the Time Complexity of Any Code<\/h2>\n\n\n\n<p>Let\u2019s understand this using the following example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>i = 1\nwhile i &lt;= 5:\n\u00a0 \u00a0 print(\"Code and Debug\")\n\u00a0 \u00a0 i += 1<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">i = <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> i &lt;= <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Code and Debug&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 i += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The time complexity of this code can be determined by analyzing the number of steps it takes to execute.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-step-by-step-analysis\">Step-by-Step Analysis<\/h3>\n\n\n\n<p>In terms of <strong>Big O notation<\/strong>, time complexity reflects the number of steps relative to the input size. Let\u2019s break down the steps for this code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization:<\/strong> The variable i is assigned an initial value of 1<\/li>\n\n\n\n<li><strong>Comparison:<\/strong> The condition i &lt;= 5 is checked<\/li>\n\n\n\n<li><strong>Execution:<\/strong> The statement print(&#8220;Code and Debug&#8221;) is executed<\/li>\n\n\n\n<li><strong>Increment:<\/strong> The value of i is increased by 1 using i += 1<\/li>\n<\/ol>\n\n\n\n<p>This flow repeats, and the condition i &lt;= 5 is re-checked after each increment. The process continues until the condition becomes false, i.e., when i exceeds 5.<\/p>\n\n\n\n<p>Let\u2019s analyze the steps further:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The loop runs <strong>5 times<\/strong> since i starts at 1 and increments by 1 until it reaches 6.<\/li>\n\n\n\n<li>For each iteration, three core steps are executed:\n<ul class=\"wp-block-list\">\n<li>Checking the condition<\/li>\n\n\n\n<li>Executing the print statement<\/li>\n\n\n\n<li>Incrementing i<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Thus, the total number of steps can be calculated as 5 \u00d7 3 = 15. In terms of <strong>Big O notation<\/strong>, this is <strong>O(15)<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-generalizing-for-n\">Generalizing for N<\/h3>\n\n\n\n<p>If we replace the constant 5 with a variable <strong>N<\/strong>, the loop will run <strong>N<\/strong> times. The number of steps would then become:<\/p>\n\n\n\n<p><strong>N\u00d73=3N<\/strong><\/p>\n\n\n\n<p>When expressed in terms of Big O, we ignore constant factors. So the time complexity becomes <strong>O(N)<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-why-shouldnt-we-use-machine-time-to-evaluate-code\">Why Shouldn&#8217;t We Use Machine Time to Evaluate Code?<\/h3>\n\n\n\n<p>The actual execution time of a code snippet can vary greatly depending on the machine it runs on. For instance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Running the same program on an older, low-performance computer like an older Windows PC. And on a high-end machine like the latest MacBook, will yield significantly different results.<\/li>\n\n\n\n<li>The high-performance machine will likely execute the code faster due to better hardware. While the older machine will take longer.<\/li>\n<\/ul>\n\n\n\n<p>These differences in execution time arise from hardware configurations. Such as the processor speed, available memory &amp; system architecture.<\/p>\n\n\n\n<p>As a result, evaluating code solely based on the time it takes to execute on a specific machine is not a reliable method of comparison.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-why-manual-counting-isnt-feasible\">Why Manual Counting Isn&#8217;t Feasible?<\/h3>\n\n\n\n<p>Manually counting the steps works only for simple cases like these. But becomes impractical for more complex code. Consider scenarios where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The loop runs millions or billions of times<\/li>\n\n\n\n<li>Nested loops or complex operations are involved within the loop<\/li>\n<\/ul>\n\n\n\n<p>In such cases, manually counting the steps would be cumbersome and error-prone. Hence, a systematic approach to calculate time complexity is essential.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-rules-for-calculating-time-complexity\">Rules for Calculating Time Complexity<\/h2>\n\n\n\n<p>To simplify the process, we follow these key rules:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Worst-case Scenario:<\/strong> Always analyze the worst-case scenario. To ensure the algorithm performs well under all conditions.<\/li>\n\n\n\n<li><strong>Ignore Constant Factors:<\/strong> Constants like <strong>3<\/strong> in <strong>3N<\/strong> are ignored. Because they don&#8217;t significantly impact scalability as <strong>N<\/strong> grows.<\/li>\n\n\n\n<li><strong>Focus on Growth Rate:<\/strong> Lower-order terms or small constant values are omitted. As they have negligible impact on the algorithm&#8217;s efficiency for large inputs.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-lets-see-the-rules-for-calculating-time-complexity-with-an-example\">Let&#8217;s See the Rules for Calculating Time Complexity with an Example<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-a192c86ca8e371a5a12def0b3238cdb9\"><em>Always calculate time complexity in terms of worst case.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>Let\u2019s discuss the rules individually with respect to the given code:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>marks = int(input(\"Enter your marks = \"))\n\nif marks >= 90:\n\u00a0 \u00a0 print(\"A\")\nelif marks >= 80 and marks &lt; 90:\n\u00a0 \u00a0 print(\"B\")\nelif marks >= 70 and marks &lt; 80:\n\u00a0 \u00a0 print(\"C\")\nelif marks >= 60 and marks &lt; 70:\n\u00a0 \u00a0 print(\"D\")\nelif marks >= 50 and marks &lt; 60:\n\u00a0 \u00a0 print(\"E\")\nelse:\n\u00a0 \u00a0 print(\"Fail\")<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">marks = <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">input<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Enter your marks = &quot;<\/span><span style=\"color: #D4D4D4\">))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> marks &gt;= <\/span><span style=\"color: #B5CEA8\">90<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;A&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> marks &gt;= <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> marks &lt; <\/span><span style=\"color: #B5CEA8\">90<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;B&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> marks &gt;= <\/span><span style=\"color: #B5CEA8\">70<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> marks &lt; <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;C&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> marks &gt;= <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> marks &lt; <\/span><span style=\"color: #B5CEA8\">70<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;D&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> marks &gt;= <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> marks &lt; <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;E&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\u00a0 \u00a0 <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Fail&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-step-by-step-explanation\">Step-by-Step Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: The code starts by taking an integer input for marks.<\/li>\n\n\n\n<li><strong>Condition Checks<\/strong>: The input value is then compared against several thresholds (&gt;= 90, &gt;= 80, etc.) to determine which grade to assign.<\/li>\n\n\n\n<li><strong>Output<\/strong>: Based on the condition that evaluates to True, the corresponding grade is printed &amp; the remaining conditions are skipped.<\/li>\n<\/ol>\n\n\n\n<p>Now let\u2019s evaluate the code for the three scenarios:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-1-best-case\">1. Best Case<\/h3>\n\n\n\n<p>The <strong>best case<\/strong> occurs when the first condition evaluates to True. Meaning that the code executes the least number of steps. For instance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If marks = 95, the condition marks &gt;= 90 is satisfied immediately<\/li>\n\n\n\n<li>Steps involved:\n<ol class=\"wp-block-list\">\n<li>Input is taken<\/li>\n\n\n\n<li>The condition marks &gt;= 90 is checked<\/li>\n\n\n\n<li>The grade A is printed<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p>In total, <strong>3 steps<\/strong> are executed. This represents the <strong>minimum execution time<\/strong> for this code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-2-worst-case\">2. Worst Case<\/h3>\n\n\n\n<p>The <strong>worst case<\/strong> occurs when none of the conditions in the if &amp; elif blocks evaluate to True. So the code has to check every condition until it reaches the else block. For instance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If marks = 45, all the if and elif conditions are evaluated &amp; found to be False, and the program executes the else block<\/li>\n\n\n\n<li>Steps involved:\n<ol class=\"wp-block-list\">\n<li>Input is taken<\/li>\n\n\n\n<li>The conditions marks &gt;= 90, marks &gt;= 80, marks &gt;= 70, marks &gt;= 60, and marks &gt;= 50 are all checked<\/li>\n\n\n\n<li>The else block executes, and Fail is printed<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p>In total, <strong>7 steps<\/strong> are executed. This represents the <strong>maximum execution time<\/strong> for this code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-3-average-case\">3. Average Case<\/h3>\n\n\n\n<p>The <strong>average case<\/strong> lies somewhere between the best and worst cases. Depending on the distribution of marks. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If marks = 75, the conditions marks &gt;= 90 and marks &gt;= 80 are checked. Both evaluate to False &amp; the third condition marks &gt;= 70 and marks &lt; 80 is satisfied.<\/li>\n\n\n\n<li>Steps involved:\n<ol class=\"wp-block-list\">\n<li>Input is taken<\/li>\n\n\n\n<li>The conditions marks &gt;= 90 and marks &gt;= 80 are checked<\/li>\n\n\n\n<li>The condition marks &gt;= 70 is satisfied, and C is printed<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p>In this case, <strong>5 steps<\/strong> are executed. The actual <strong>average case<\/strong> depends on the likelihood of each condition being met, which may vary based on real-world data.<\/p>\n\n\n\n<div class=\"wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Watch our python dsa course on youtube (200+ questions)<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12-worst-case-time-complexity\">Worst-Case Time Complexity<\/h2>\n\n\n\n<p>For a generalized input size <strong>N<\/strong>, the number of steps is determined by the number of conditions in the code. Since we always analyze the <strong>worst-case scenario<\/strong> for time complexity:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The worst-case scenario involves evaluating all conditions sequentially, followed by executing the else block<\/li>\n\n\n\n<li>Assuming there are <strong>k<\/strong> conditions, the worst-case time complexity is <strong>O(k)<\/strong>, where <strong>k = 6<\/strong> in this example<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-why-focus-on-the-worst-case\">Why Focus on the Worst Case?<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Ensures Robustness<\/strong>: Evaluating the worst case ensures the algorithm handles the most demanding situations efficiently.<\/li>\n\n\n\n<li><strong>Predicts Scalability<\/strong>: It provides insights into how the code performs as the number of conditions (k) increases.<\/li>\n\n\n\n<li><strong>Standard Practice<\/strong>: In algorithm analysis, the worst-case scenario is used as a benchmark to compare different solutions.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-avoid-including-the-constant-terms\">Avoid Including the Constant Terms<\/h2>\n\n\n\n<p>Let\u2019s check into the rule of <strong>ignoring constant terms<\/strong> when calculating time complexity. Using an example to understand why this approach simplifies the analysis without losing accuracy.<\/p>\n\n\n\n<p>Consider the time complexity expression:<\/p>\n\n\n\n<p class=\"has-text-align-center has-vivid-purple-color has-text-color has-link-color wp-elements-3ce859b9c066553910b20f4dced066f5\"><strong>T(N) = 5N<\/strong><strong><sup>10 <\/sup><\/strong><strong>+ 8N<\/strong><strong><sup>3 <\/sup><\/strong><strong>+ 2<\/strong><\/p>\n\n\n\n<p>Here, T(N) represents the total number of operations performed by the algorithm as a function of the input size <strong>N<\/strong>. Let\u2019s analyze what happens when <strong>N<\/strong> becomes very large.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-step-by-step-analysis\">Step-by-Step Analysis<\/h3>\n\n\n\n<p>Suppose <strong>N = 10<\/strong><strong><sup>6<\/sup><\/strong> (a very large number). Substituting this value into the equation, we get:<\/p>\n\n\n\n<p class=\"has-text-align-center has-vivid-purple-color has-text-color has-link-color wp-elements-67afac26d1c92ef4d070454273d2c56a\"><strong>T(N) = 5(10<\/strong><strong><sup>60<\/sup><\/strong><strong>) + 8(10<\/strong><strong><sup>18<\/sup><\/strong><strong>) + 2<\/strong><\/p>\n\n\n\n<p>Breaking this down:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>5(10<sup>60<\/sup>):<\/strong> This is an extremely large term, dominating the overall value of T(N)<\/li>\n\n\n\n<li><strong>8(10<sup>18<\/sup>):<\/strong> This term, while significant on its own, is much smaller compared to 5(10<sup>60<\/sup>)<\/li>\n\n\n\n<li><strong>2:<\/strong> This constant term is negligible in comparison to the other terms<\/li>\n<\/ol>\n\n\n\n<p>When <strong>N<\/strong> becomes large, the term 5(10<sup>60<\/sup>) overwhelmingly dictates the growth of <strong>T(N)<\/strong>. While <strong>8(10<sup>18<\/sup>)<\/strong> and <strong>2<\/strong> contribute very little to the overall value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-why-ignore-constant-terms\">Why Ignore Constant Terms?<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insignificant Contribution:<\/strong> As <strong>N<\/strong> grows, the constant terms &amp; smaller-order terms (like <strong>8N<sup>3<\/sup><\/strong>) become insignificant in comparison to the highest-order term <strong>(5N<sup>10<\/sup>)<\/strong>.<\/li>\n\n\n\n<li><strong>Focus on Growth Rate:<\/strong> Time complexity analysis is concerned with how the algorithm scales with input size. And not with the exact number of operations. Ignoring constants simplifies the expression while retaining key insights about the algorithm\u2019s behavior.<\/li>\n\n\n\n<li><strong>Standardization:<\/strong> In Big O notation, we represent only the dominant term without its coefficient. For <strong>T(N) = 5N<sup>10 <\/sup>+ 8N<sup>3 <\/sup>+ 2<\/strong>, the dominant term is <strong>N<sup>10<\/sup><\/strong>, so the time complexity is written as: <strong>O(N<sup>10<\/sup>)<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>Now that we have fully understood time complexity. Let&#8217;s move on to the next important part.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-what-is-space-complexity\">What is Space Complexity?<\/h2>\n\n\n\n<p>Space complexity refers to the amount of memory required by a program during its execution. This includes all the memory used by variables, data structures &amp; any additional memory allocated while solving the problem.<\/p>\n\n\n\n<p>Since memory usage can vary depending on the machine. We represent space complexity using <strong>Big O notation<\/strong>, rather than standard units like MB or GB.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Formal Definition:<\/strong><\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-b536f82ddfbd049c9d3d6f31e820a6ce\"><em>Space complexity is the sum of two components:<\/em><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-15c62cc559ee8123eaf549b62b494159\"><em><strong>Input Space:<\/strong> Memory required to store the input data<\/em><\/li>\n\n\n\n<li class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-d8d6be6f28f60a2f29e12ab86015f061\"><em><strong>Auxiliary Space:<\/strong> Additional memory required to solve the problem. Including temporary variables, data structures, or function call stacks.<\/em><\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-example\">Example<\/h3>\n\n\n\n<p>Consider the following code:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>a = int(input())\u00a0 # Input Space\nb = int(input())\u00a0 # Input Space\nc = a + b \u00a0 \u00a0 \u00a0 \u00a0 # Auxiliary Space\nprint(c)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">a = <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">input<\/span><span style=\"color: #D4D4D4\">())\u00a0 <\/span><span style=\"color: #6A9955\"># Input Space<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">b = <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">input<\/span><span style=\"color: #D4D4D4\">())\u00a0 <\/span><span style=\"color: #6A9955\"># Input Space<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">c = a + b \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"color: #6A9955\"># Auxiliary Space<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(c)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>a and b are used to store the inputs, which account for the <strong>input space<\/strong><\/li>\n\n\n\n<li>c is a temporary variable used to store the result, accounting for the <strong>auxiliary space<\/strong><\/li>\n<\/ul>\n\n\n\n<p>The total space complexity is O(3), as three variables are used. However, when written in Big O notation, constants are ignored. So, the space complexity simplifies to <strong>O(1)<\/strong> (constant space).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"19-using-arrays\">Using Arrays<\/h2>\n\n\n\n<p>If we use an array of size <strong>N<\/strong> in a program, the space complexity becomes <strong>O(N)<\/strong>, as <strong>N<\/strong> elements are stored in memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"20-good-coding-practice-avoid-input-manipulation\">Good Coding Practice: Avoid Input Manipulation<\/h2>\n\n\n\n<p>If asked to solve a problem such as adding two numbers <strong>a<\/strong> and <strong>b<\/strong>. One way to reduce space complexity is by modifying the input directly, like this:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>b = a + b<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">b = a + b<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This approach eliminates the need for an additional variable c, reducing space usage. However, this is generally <strong>not considered good practice<\/strong>. Especially in coding interviews, for the given reasons:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Input Integrity:<\/strong> The original input values might be required for other purposes in the program or system.<\/li>\n\n\n\n<li><strong>Company Standards:<\/strong> Many companies reuse input data for multiple operations. Altering inputs may lead to unintended consequences.<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-6a36f8a858592f05f16d713231d8658c\"><strong>Rule of Thumb:<\/strong><em> Avoid manipulating input data unless explicitly instructed to do so by the interviewer or problem statement. Always prioritize clarity &amp; integrity of your code over reducing space complexity.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-points-to-remember-for-competitive-programming\">Points to Remember for Competitive Programming<\/h2>\n\n\n\n<p>When participating in competitive programming or solving problems on platforms like Leetcode<strong> <\/strong>or GeeksforGeeks. Your code is executed on online servers. These servers generally handle about <strong>10<sup>8<\/sup><\/strong> operations per second.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-key-guidelines-\"><strong>Key Guidelines<\/strong>:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>If the time limit is 2 seconds, your code should ideally perform no more than <strong>2 \u00d7 10<sup>8<\/sup><\/strong> operations<\/li>\n\n\n\n<li>For a 5-second time limit, the threshold increases to <strong>5 \u00d7 10<sup>8<\/sup><\/strong> operations<\/li>\n<\/ol>\n\n\n\n<p>To ensure your code executes within the time limit:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep the time complexity around <strong>O(10^8)<\/strong> operations, ignoring constants &amp; smaller terms<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"23-conclusion-time-complexity-and-space-complexity\">Conclusion [Time Complexity and Space Complexity]<\/h2>\n\n\n\n<p>By <strong>understanding <\/strong>&amp; <strong>optimizing <\/strong>both time complexity and space complexity. You can write <strong>efficient<\/strong>, <strong>scalable <\/strong>code that adheres to best practices.<\/p>\n\n\n\n<p>Always maintain input integrity unless explicitly told. Otherwise, be mindful of server execution constraints in competitive programming.<\/p>\n\n\n\n<p>Mastering these points not only improves your coding skills. But also prepares you for real-world scenarios &amp; coding interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-points-to-remember-for-competitive-programming\">Some Questions for You<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1775292526933\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">What is the difference between time complexity and space complexity?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Time complexity measures how the execution time of an algorithm grows with input size, while space complexity measures how much memory the algorithm uses. Both help evaluate the efficiency of a solution from different perspectives.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775292590310\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Why do we use Big O notation in complexity analysis?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Big O notation is used to represent the growth rate of an algorithm by focusing on the dominant term and ignoring constants. This makes it easier to compare algorithms and understand their scalability for large inputs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775292600019\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Why is worst-case time complexity usually considered?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Worst-case time complexity ensures that the algorithm performs efficiently even in the most demanding scenarios. It provides a guarantee of performance, making it the standard for comparing different solutions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775292622241\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Can time complexity vary on different machines?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>The actual execution time may vary across machines due to hardware differences, but time complexity remains the same. This is because it measures the algorithm\u2019s growth pattern, not real-time execution.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775292635506\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">What is auxiliary space in space complexity?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Auxiliary space refers to the extra memory used by an algorithm apart from the input data. This includes temporary variables, data structures, and recursion stack space used during execution.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<div class=\"wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Watch our python dsa course on youtube (200+ questions)<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time complexity and space complexity, both measure an algorithm&#8217;s efficiency. Where timecomplexity shows how the running time increases with input size. Space complexity tracks memory usage. Both are essential for optimizing algorithms. Especially when dealing with large datasets or limited resources. In this article, we will explore these two concepts of algorithm analysis. Along with<\/p>\n","protected":false},"author":1,"featured_media":74,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":{"0":"post-72","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Time-Complexity-and-Space-Complexity.webp","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":12,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions\/1129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/74"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}