{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/timeComplexity","result":{"data":{"blog":{"frontmatter":{"title":"TIME COMPLEXITY","thumbnail":"blog55","date":"April 13, 2021","dsaCppCodeFile":null},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Finding out time complexity of your code helps you to develop\n                  better programs that run faster. Some programs are easy to\n                  analyze, but when we have loops, nested functions or\n                  recursion; the situation gets tricky.\n                </p>\n                <p>\n                  The time complexity can be determined by analyzing the\n                  program&apos;s statements going line by line. The most common\n                  metric used to analyze time complexity is Big O Notation.\n                </p>\n                <p>\n                  Big O notation cares about the worst case scenario. For ex: in\n                  linear search of an array of N elements, searching for the\n                  last element is the worst case scenario which will take O(N)\n                  time. <br>\n                  Big O = Big Order Function(Drop the lower order and constant\n                  terms). For ex: O(7n<sup>3</sup>-n<sup>2</sup>+5) =\n                  O(n<sup>3</sup>).\n                </p>\n                <p>\n                  If we plot the most common Big O Notations, we would get the\n                  graph:\n                </p>\n                <div class=\"text-center\">\n                  <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 105.40540540540542%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFRElEQVQ4y21Va1BTRxTee/MggQEhECAQSTAPSAgBjRVKBIqiRR46IgmRgPK0KOCLQbRqFQu1aiFAHiIgECQXJBVpHUZsizoaHlK07bQqUhVGrdpRseNMHfujprsJRO24M9+cc/be/e7uOd/ZCwAcPG4gduWXX3HkD1mGjpm/Pp0NXYEL3TkKx0AIjYRLYCzCMCClkPBI6CNIIeQQTLRujpczeGdYrVabHbtzO+Hi+QvoZSogkQXQBkB4kHGMB20gRBiEEIIPgT7kN8tBTGyy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vlA9Hwb4ZIprY9HkD8X+d8AIZeIksjYn0SGHaxZmdF0KLkksTNNXUakylDiBXVJLqGNivcet2VB/lsRDWdjK8SCeHl4Zlq6bMveupVf6GqStauOJS2X742dD+KFLGzdfA5QhwdgmTIOSPJn5R0/ULiVqD5b2FqVA1K5i+SHCihvCBlkf3FxdG6NOqq1oz6lmdCv2rVblygCKokXWCpi0NZKvbFUCROskTBJaVJvsILDSPmyOGi78UisurZcBNKEwUEVyjfXl1wlkxZtjSc0+THGMkV4KrpQITxdAYU1cyMzZuCFLNSt94yP2sOTZFfJG8LqwnhyBmA62ip5scwJ2aNd5ySV9cZYJC0ylRoOG18EsQjCm7NQTOKFi2l+Qi4F0MguZFc6ZlusKVjy1m/gDMQFR/zMasUNxHdoh0wMw5By3SCQpbyvMPAd8B9D9qfRQ/sdsQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Big O\" title=\"Big O\" src=\"/static/80147a148e3065372a89cc6f688a5ce3/fcda8/bigO.png\" srcset=\"/static/80147a148e3065372a89cc6f688a5ce3/12f09/bigO.png 148w,\n/static/80147a148e3065372a89cc6f688a5ce3/e4a3f/bigO.png 295w,\n/static/80147a148e3065372a89cc6f688a5ce3/fcda8/bigO.png 590w,\n/static/80147a148e3065372a89cc6f688a5ce3/2a195/bigO.png 620w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n                </div>\n                <p>\n                  Let&#x2019;s take a look, how to translate code into time complexity.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Sequential Statements</h4>\n              <div class=\"m-2\">\n                <p>\n                  If we have n statements performing basic operations such as\n                  assignment, reading a variable or comparision then we can\n                  assume the time complexity for each statement is O(1).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = t(statement1) + t(statement2) + ... + t(statementN).\n                </p>\n                <p>\n                  If each statement executes a basic operation then we can say\n                  it takes constant time i.e. O(1), be it 1 or 100 lines of\n                  statements(executing basic operation).\n                </p>\n                <p class=\"alert alert-success\">\n                  Cosider a C++ program to multiply two integers. <br>\n                  <code>\n                    int a=10; <br>\n                    int b=5; <br>\n                    int mul=a*b; <br>\n                    cout&lt;&lt; &quot;Product: &quot; &lt;&lt; mul;\n                  </code>\n                  <br>\n                  These statements are basic operations and each of them will\n                  take O(1) time. So summing up, the time complexity will still\n                  remain O(1) and not 4*O(1).\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Conditional Statements</h4>\n              <div class=\"m-2\">\n                <p class=\"alert alert-success\">\n                  <code>\n                    if (condition) { <br>\n                    statement1; <br>\n                    statement2; <br>\n                    } else { <br>\n                    statement3; <br>\n                    }\n                  </code>\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  The time complexity of this would be: <br>\n                  T(n) = MAX of ([t(statement1) + t(statement2)],\n                  [t(statement3)])\n                </p>\n                <p>\n                  For ex: Suppose statement1 takes O(nlogn), statement2 takes\n                  O(n) and the condition is happened to be true then time\n                  complexity would be O(max of [nlogn,n]) = O(nlogn).\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Looping Statements</h4>\n              <div class=\"m-2\">\n                <h5>Constant time loops:</h5>\n                <p>\n                  If the loop bounds a constant number then the time complexity\n                  would also be constant time i.e. O(1).\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (int i = 0; i &lt; 10; i++) { <br>\n                    cout &lt;&lt; i; <br>\n                    } <br>\n                  </code>\n                  That code is O(1) because it no longer depends on the input\n                  size. It will always run the statement ten times.\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Linear time loops:</h5>\n                <p>\n                  For any loop, we find out the runtime of the block inside them\n                  and multiply it by the number of times the program will repeat\n                  the loop.\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (int i = 0; i &lt; N; i++) { <br>\n                    statement1; <br>\n                    statement2; <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  For this, the loop will execute the block N(input) times and\n                  thus we get the time complexity:\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = n * [ t(statement1) + t(statement2) ]\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Logarithmic time loops</h5>\n                <p>\n                  An algorithm is O(logn) if it takes a constant time to cut the\n                  problem size by a fraction.\n                </p>\n                <p class=\"alert alert-success\">\n                  Consider the following loop where the iterator is doubled\n                  after every loop. <br>\n                  <code>\n                    for(int i = 1; i &lt;= N; i *= 2 ) { <br>\n                    cout &lt;&lt; i; <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  If we observe carefully, the value of i is doubling every\n                  time. Initially i = 1, and in subsequent steps i = 2,4,8 and\n                  so on. Let us assume that the loop is executing some k times.\n                  At kth step 2<sup>k</sup> = n, and at (k + 1)th step we come\n                  out of the loop. Taking logarithm on both sides, gives\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  log(2<sup>k</sup>) = log(n) <br>\n                  klog2 = logn <br>\n                  k = logn <br>\n                  //Assume the base=2\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Nested Loops</h5>\n                <p class=\"alert alert-success\">\n                  Sometimes you will have problems related to 2D arrays then you\n                  will have to use 2 loops to visit all the array elements.\n                  <br>\n                  <code>\n                    for (let i = 0; i &lt; n; i++) { <br>\n                    statement1; <br>\n                    for (let j = 0; j &lt; m; j++) { <br>\n                    statement2; <br>\n                    } <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  Then the time complexity would be O(n*m). If you had n instead\n                  of m then it would have been O(n<sup>2</sup>).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = n * [t(statement1) + m * t(statement2)]\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Function Calls</h5>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (let i = 0; i &lt; n; i++) { <br>\n                    function1(); <br>\n                    for (let j = 0; j &lt; n; j++) { <br>\n                    function2(); <br>\n                    for (let k = 0; k &lt; n; k++) { <br>\n                    function3(); <br>\n                    } <br>\n                    } <br>\n                    } <br>\n                  </code>\n                </p>\n                <p>\n                  Depending on the runtime of each function, we will get\n                  different time complexities.\n                  <br>\n                  If they are all constants, O(1) then final runtime would be\n                  O(n<sup>3</sup>). <br>\n                  Suppose function1, function2 are constant and function3 has\n                  O(n<sup>2</sup>) runtime then this program will have an\n                  overall runtime of O(n<sup>5</sup>).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  In general, <br>\n                  T(n) = n * [ t(fn1()) + n * [ t(fn2()) + n * [ t(fn3()) ] ] ]\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Recursive Calls</h4>\n              <div class=\"m-2\">\n                <p>\n                  Analyzing the runtime of recursive functions might get a\n                  little tricky. There are different ways to do it. One\n                  intuitive way is to explore the recursion tree.\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    function fn(n) { <br>\n                    if (n &lt; 0) return 0; <br>\n                    if (n &lt; 2) return n; <br>\n                    return fn(n - 1) + fn(n - 2); <br>\n                    }\n                  </code>\n                </p>\n                <p>Let&apos;s see how many calls will be made:</p>\n                <ul class=\"pl-4\">\n                  <li>\n                    If n=2, then you will have 3 calls i.e fn(2) which calls\n                    fn(1) &amp; fn(0).\n                  </li>\n                  <li>\n                    If n=3, then you will have 5 calls ie. fn(3) which calls\n                    fn(2)(which again calls fn(1) and fn(0)) and fn(1).\n                  </li>\n                </ul>\n                <p>\n                  The total no. of calls atmost can be 2<sup>n</sup> in worst\n                  case. So the time complexity would be O(2<sup>n</sup>) in this\n                  example.\n                </p>\n              </div>\n            </div>\n        <div class=\"my-2 p-2\">\n                    <strong>Similar posts:</strong>\n                    <a href=\"/blogs/introductionToAlgorithms\">Introduction to Algorithms</a> | <a href=\"/blogs/analysisOfAlgorithms\">Analysis of Algorithms</a>\n                </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Finding out time complexity of your code helps you to develop\n                  better programs that run faster. Some programs are easy to\n                  analyze, but when we have loops, nested functions or\n                  recursion; the situation gets tricky.\n                </p>\n                <p>\n                  The time complexity can be determined by analyzing the\n                  program&apos;s statements going line by line. The most common\n                  metric used to analyze time complexity is Big O Notation.\n                </p>\n                <p>\n                  Big O notation cares about the worst case scenario. For ex: in\n                  linear search of an array of N elements, searching for the\n                  last element is the worst case scenario which will take O(N)\n                  time. <br>\n                  Big O = Big Order Function(Drop the lower order and constant\n                  terms). For ex: O(7n<sup>3</sup>-n<sup>2</sup>+5) =\n                  O(n<sup>3</sup>).\n                </p>\n                <p>\n                  If we plot the most common Big O Notations, we would get the\n                  graph:\n                </p>\n                <div class=\"text-center\">\n                  <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 105.40540540540542%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFRElEQVQ4y21Va1BTRxTee/MggQEhECAQSTAPSAgBjRVKBIqiRR46IgmRgPK0KOCLQbRqFQu1aiFAHiIgECQXJBVpHUZsizoaHlK07bQqUhVGrdpRseNMHfujprsJRO24M9+cc/be/e7uOd/ZCwAcPG4gduWXX3HkD1mGjpm/Pp0NXYEL3TkKx0AIjYRLYCzCMCClkPBI6CNIIeQQTLRujpczeGdYrVabHbtzO+Hi+QvoZSogkQXQBkB4kHGMB20gRBiEEIIPgT7kN8tBTGyyOyQA/KFxOucR6XYIgGA0d0u+INQaHeFpteoo/2yOX2D/6CDVerVsHvjfaBrNC+x5sIXkmCADjANZSQQm9NHRRTFors+NIjXAnY0Xxflcz5ajo4HRzd6MW5rli8GqXVjO5gRbiu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mTIOSPJn5R0/ULiVqD5b2FqVA1K5i+SHCihvCBlkf3FxdG6NOqq1oz6lmdCv2rVblygCKokXWCpi0NZKvbFUCROskTBJaVJvsILDSPmyOGi78UisurZcBNKEwUEVyjfXl1wlkxZtjSc0+THGMkV4KrpQITxdAYU1cyMzZuCFLNSt94yP2sOTZFfJG8LqwnhyBmA62ip5scwJ2aNd5ySV9cZYJC0ylRoOG18EsQjCm7NQTOKFi2l+Qi4F0MguZFc6ZlusKVjy1m/gDMQFR/zMasUNxHdoh0wMw5By3SCQpbyvMPAd8B9D9qfRQ/sdsQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Big O\" title=\"Big O\" src=\"/static/80147a148e3065372a89cc6f688a5ce3/fcda8/bigO.png\" srcset=\"/static/80147a148e3065372a89cc6f688a5ce3/12f09/bigO.png 148w,\n/static/80147a148e3065372a89cc6f688a5ce3/e4a3f/bigO.png 295w,\n/static/80147a148e3065372a89cc6f688a5ce3/fcda8/bigO.png 590w,\n/static/80147a148e3065372a89cc6f688a5ce3/2a195/bigO.png 620w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n                </div>\n                <p>\n                  Let&#x2019;s take a look, how to translate code into time complexity.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Sequential Statements</h4>\n              <div class=\"m-2\">\n                <p>\n                  If we have n statements performing basic operations such as\n                  assignment, reading a variable or comparision then we can\n                  assume the time complexity for each statement is O(1).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = t(statement1) + t(statement2) + ... + t(statementN).\n                </p>\n                <p>\n                  If each statement executes a basic operation then we can say\n                  it takes constant time i.e. O(1), be it 1 or 100 lines of\n                  statements(executing basic operation).\n                </p>\n                <p class=\"alert alert-success\">\n                  Cosider a C++ program to multiply two integers. <br>\n                  <code>\n                    int a=10; <br>\n                    int b=5; <br>\n                    int mul=a*b; <br>\n                    cout&lt;&lt; &quot;Product: &quot; &lt;&lt; mul;\n                  </code>\n                  <br>\n                  These statements are basic operations and each of them will\n                  take O(1) time. So summing up, the time complexity will still\n                  remain O(1) and not 4*O(1).\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Conditional Statements</h4>\n              <div class=\"m-2\">\n                <p class=\"alert alert-success\">\n                  <code>\n                    if (condition) { <br>\n                    statement1; <br>\n                    statement2; <br>\n                    } else { <br>\n                    statement3; <br>\n                    }\n                  </code>\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  The time complexity of this would be: <br>\n                  T(n) = MAX of ([t(statement1) + t(statement2)],\n                  [t(statement3)])\n                </p>\n                <p>\n                  For ex: Suppose statement1 takes O(nlogn), statement2 takes\n                  O(n) and the condition is happened to be true then time\n                  complexity would be O(max of [nlogn,n]) = O(nlogn).\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Looping Statements</h4>\n              <div class=\"m-2\">\n                <h5>Constant time loops:</h5>\n                <p>\n                  If the loop bounds a constant number then the time complexity\n                  would also be constant time i.e. O(1).\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (int i = 0; i &lt; 10; i++) { <br>\n                    cout &lt;&lt; i; <br>\n                    } <br>\n                  </code>\n                  That code is O(1) because it no longer depends on the input\n                  size. It will always run the statement ten times.\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Linear time loops:</h5>\n                <p>\n                  For any loop, we find out the runtime of the block inside them\n                  and multiply it by the number of times the program will repeat\n                  the loop.\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (int i = 0; i &lt; N; i++) { <br>\n                    statement1; <br>\n                    statement2; <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  For this, the loop will execute the block N(input) times and\n                  thus we get the time complexity:\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = n * [ t(statement1) + t(statement2) ]\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Logarithmic time loops</h5>\n                <p>\n                  An algorithm is O(logn) if it takes a constant time to cut the\n                  problem size by a fraction.\n                </p>\n                <p class=\"alert alert-success\">\n                  Consider the following loop where the iterator is doubled\n                  after every loop. <br>\n                  <code>\n                    for(int i = 1; i &lt;= N; i *= 2 ) { <br>\n                    cout &lt;&lt; i; <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  If we observe carefully, the value of i is doubling every\n                  time. Initially i = 1, and in subsequent steps i = 2,4,8 and\n                  so on. Let us assume that the loop is executing some k times.\n                  At kth step 2<sup>k</sup> = n, and at (k + 1)th step we come\n                  out of the loop. Taking logarithm on both sides, gives\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  log(2<sup>k</sup>) = log(n) <br>\n                  klog2 = logn <br>\n                  k = logn <br>\n                  //Assume the base=2\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Nested Loops</h5>\n                <p class=\"alert alert-success\">\n                  Sometimes you will have problems related to 2D arrays then you\n                  will have to use 2 loops to visit all the array elements.\n                  <br>\n                  <code>\n                    for (let i = 0; i &lt; n; i++) { <br>\n                    statement1; <br>\n                    for (let j = 0; j &lt; m; j++) { <br>\n                    statement2; <br>\n                    } <br>\n                    }\n                  </code>\n                </p>\n                <p>\n                  Then the time complexity would be O(n*m). If you had n instead\n                  of m then it would have been O(n<sup>2</sup>).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  T(n) = n * [t(statement1) + m * t(statement2)]\n                </p>\n              </div>\n              <div class=\"m-2\">\n                <h5>Function Calls</h5>\n                <p class=\"alert alert-success\">\n                  <code>\n                    for (let i = 0; i &lt; n; i++) { <br>\n                    function1(); <br>\n                    for (let j = 0; j &lt; n; j++) { <br>\n                    function2(); <br>\n                    for (let k = 0; k &lt; n; k++) { <br>\n                    function3(); <br>\n                    } <br>\n                    } <br>\n                    } <br>\n                  </code>\n                </p>\n                <p>\n                  Depending on the runtime of each function, we will get\n                  different time complexities.\n                  <br>\n                  If they are all constants, O(1) then final runtime would be\n                  O(n<sup>3</sup>). <br>\n                  Suppose function1, function2 are constant and function3 has\n                  O(n<sup>2</sup>) runtime then this program will have an\n                  overall runtime of O(n<sup>5</sup>).\n                </p>\n                <p class=\"text-center alert alert-warning\">\n                  In general, <br>\n                  T(n) = n * [ t(fn1()) + n * [ t(fn2()) + n * [ t(fn3()) ] ] ]\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Recursive Calls</h4>\n              <div class=\"m-2\">\n                <p>\n                  Analyzing the runtime of recursive functions might get a\n                  little tricky. There are different ways to do it. One\n                  intuitive way is to explore the recursion tree.\n                </p>\n                <p class=\"alert alert-success\">\n                  <code>\n                    function fn(n) { <br>\n                    if (n &lt; 0) return 0; <br>\n                    if (n &lt; 2) return n; <br>\n                    return fn(n - 1) + fn(n - 2); <br>\n                    }\n                  </code>\n                </p>\n                <p>Let&apos;s see how many calls will be made:</p>\n                <ul class=\"pl-4\">\n                  <li>\n                    If n=2, then you will have 3 calls i.e fn(2) which calls\n                    fn(1) &amp; fn(0).\n                  </li>\n                  <li>\n                    If n=3, then you will have 5 calls ie. fn(3) which calls\n                    fn(2)(which again calls fn(1) and fn(0)) and fn(1).\n                  </li>\n                </ul>\n                <p>\n                  The total no. of calls atmost can be 2<sup>n</sup> in worst\n                  case. So the time complexity would be O(2<sup>n</sup>) in this\n                  example.\n                </p>\n              </div>\n            </div>\n        <div class=\"my-2 p-2\">\n                    <strong>Similar posts:</strong>\n                    <a href=\"/blogs/introductionToAlgorithms\">Introduction to Algorithms</a> | <a href=\"/blogs/analysisOfAlgorithms\">Analysis of Algorithms</a>\n                </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M28%2095c-6%200-6%200-6%203%200%202%201%202%203%202s2%200%202%207c-1%207%200%207%202%207s3-4%203-11c0-3%200-4%202-4l2-2-1-3-7%201m10%201l-1%2010v8h5c4%200%205%200%206-2%200-2%200-2%201%200%202%201%202%201%203-1l2-2v5h9c9%200%2010-1%2010-3s0-2-4-2c-5%200-5-2-1-3%202%200%203-1%203-2%200-2-1-2-3-2-4%200-4-2%201-2%203-1%204-2%204-4s0-2-3-2l-7%201h-3v14l-1-7c0-6-1-7-3-7l-3%203c-2%205-3%205-3%201-1-4-1-4-7-4-4-1-5-1-5%201m48%200c-7%205-5%2018%202%2018%202%200%207-4%207-6s-2-2-4%200c-4%204-5%200-3-6%201-3%203-4%202-1%200%202%200%202%202%202%205%200%205-6%200-8-3-1-3-1-6%201m13%201c-7%209%200%2023%208%2014%203-3%203-12%201-15-3-3-6-3-9%201m44-1l-1%2010v8h12c12%200%2013-1%2013-3s0-2-4-2-5-1-2-2l3-1%202-2c0-2-1-2-3-2-6%200-5-2%201-2%204-1%204-1%204-3s-1-2-6-2c-8%200-7%200-8%207v6h-7v-7c0-6%200-7-2-7l-2%202m35%200c-2%203-2%203-3%201s-3-3-5-1c-1%201%200%202%201%205l2%203-2%204c-4%206-4%206-1%206l4-2%201-2%202%202c1%202%205%203%205%201l-2-5c-1-4-1-4%201-8%202-3%202-3%201-5-2-1-2-1-4%201m7%200l-1%2010v8h2c3%200%203%200%203-5%200-7%201-9%203-9s2%201%202%207c0%207%200%207%203%207%202%200%202%200%202-7l1-8%203-1c2%200%205%209%205%2013%200%203%200%203%203%203%202%200%202%200%202-3l3-9c3-7%203-7%200-7-2%200-3%201-4%203l-1%204-2-4c0-2-2-3-3-3h-12c-7-1-9%200-9%201m-74%204l-1%2010c0%204%200%204%202%204l4-1c0-1%201-2%202-1%201%200%202%200%203-2%201-1%201-1%201%201s0%203%205%203%205%200%205-3c0-2%201-3%203-3%208-2%208-13%200-13-6%200-7%201-7%207v7l-1-7c0-6-1-7-3-7l-4%204-1%204-1-4c-1-3-2-4-4-4-3%200-3%200-3%205m188%206l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/6490137613b01fc0fdb72b80c57561c4/ee604/blog55.png","srcSet":"/static/6490137613b01fc0fdb72b80c57561c4/69585/blog55.png 200w,\n/static/6490137613b01fc0fdb72b80c57561c4/497c6/blog55.png 400w,\n/static/6490137613b01fc0fdb72b80c57561c4/ee604/blog55.png 800w,\n/static/6490137613b01fc0fdb72b80c57561c4/f3583/blog55.png 1200w,\n/static/6490137613b01fc0fdb72b80c57561c4/e4d72/blog55.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"timeComplexity","thumbnail":"thumbnails/blog55.png"}},"staticQueryHashes":["2987289216","63159454"]}