<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nemanja Milosavljevic</title>
    <description>The latest articles on DEV Community by Nemanja Milosavljevic (@nemanyam).</description>
    <link>https://dev.to/nemanyam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454640%2Fd01657f8-b543-446f-97bf-a5b33a86870b.jpg</url>
      <title>DEV Community: Nemanja Milosavljevic</title>
      <link>https://dev.to/nemanyam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nemanyam"/>
    <language>en</language>
    <item>
      <title>Big O</title>
      <dc:creator>Nemanja Milosavljevic</dc:creator>
      <pubDate>Mon, 11 Oct 2021 15:25:47 +0000</pubDate>
      <link>https://dev.to/nemanyam/big-o-579p</link>
      <guid>https://dev.to/nemanyam/big-o-579p</guid>
      <description>&lt;p&gt;Do you know what the Big O stands for?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Yes! Erm... I have a friend his name is Orestis he is big enough, therefore, we call him Big O, is it not?!&lt;/li&gt;
&lt;li&gt;Well, not really.&lt;/li&gt;
&lt;li&gt;Erm... Big Omar? Or, no, no, Big Otto, right?&lt;/li&gt;
&lt;li&gt;Well, you are almost there. Big O can be our friend, it usually is, but its name is neither Orestis nor Otto or Omar.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Big O notation is a mathematical notation that describes the limiting behaviour of a function when the argument tends towards a particular value or infinity. It is a member of a family of notations invented by Paul Bachmann, Edmund Landau, and others collectively called Bachmann–Landau notation or asymptotic notation.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;That is what Wikipedia is saying, not me, all possible complaints to them then.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simplified, &lt;code&gt;Big O&lt;/code&gt; describes the complexity through mathematical terms, precisely algebraic. We can see it in an example like this. &lt;code&gt;O(n²)&lt;/code&gt;, which is called &lt;code&gt;Big O squared&lt;/code&gt;. The letter &lt;code&gt;n&lt;/code&gt; is nothing but an input size of given elements, let's say an array of n integers, and the function &lt;code&gt;f(n) = n²&lt;/code&gt; shows us how big the input is. Quadratic time complexity has a growth rate of &lt;code&gt;n²&lt;/code&gt;. If the input is size 2, it will do four operations, if the size is 4 it will do 16 operations and so on.&lt;/p&gt;

&lt;p&gt;Shall we write an example for an Algorithm that runs in n2 time complexity right now and then discuss it in the detail? Just bear with me here. I promise it is going to be fun.&lt;/p&gt;

&lt;p&gt;A typical example would be Selection Sort. For the definition and more detailed functionality of the Selection sort, you can read &lt;a href="https://en.wikipedia.org/wiki/Selection_sort"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now then
&lt;/h2&gt;

&lt;p&gt;..back to the implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;minIndex&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, let's look at this sorting algorithm in detail. Shall we determine what &lt;code&gt;Big O&lt;/code&gt; it has and why?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;minIndex&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we look at the outer for loop of our algorithm, that would mean that our algorithm will run at &lt;code&gt;O(n)&lt;/code&gt; time, at least at the first glance. Why? For the n input integers, for loop will repeat n-times. That has been defined in the loop condition, it is straightforward, and that's all about the outer &lt;code&gt;for loop.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;minIndex&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, let's tackle the last portion of the code now. We would find that the inner loop will repeat for &lt;code&gt;1+2 … + n&lt;/code&gt; times, which equals &lt;code&gt;n(n-1)/2&lt;/code&gt; times. If we multiply this out, we will end up getting &lt;code&gt;n²/2-n/2&lt;/code&gt;.&lt;br&gt;
When we calculate big O notation, we only care about the &lt;strong&gt;&lt;em&gt;dominant terms&lt;/em&gt;&lt;/strong&gt;, and we do not care about the coefficients, therefore we can avoid all the values except for &lt;code&gt;n²&lt;/code&gt; which will point to our definite time complexity range. We write it as &lt;code&gt;O(n²)&lt;/code&gt; as our &lt;code&gt;big O&lt;/code&gt; value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition of dominant term
&lt;/h3&gt;

&lt;p&gt;The mathematical term greater in absolute value than any other (as in a set) or than the sum of the others.&lt;/p&gt;

&lt;p&gt;For instance, &lt;code&gt;n²&lt;/code&gt; grows faster than &lt;code&gt;n&lt;/code&gt;, so if we have the following quadratic equation &lt;strong&gt;&lt;em&gt;an² + bn + c&lt;/em&gt;&lt;/strong&gt;. Where &lt;code&gt;a,b,c&lt;/code&gt; are known numbers, and &lt;code&gt;a ≠ 0&lt;/code&gt;. We only care about the dominant term for numerators and denominators in the end. Finally, it'll lead us to the final big &lt;code&gt;O(n²)&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;At this point, we ask ourselves if there is a &lt;code&gt;little O&lt;/code&gt; or something similar?&lt;br&gt;
Well, Yes, there is indeed, and we will most certainly mention it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Big O, Little O, and the rest of the gang..
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Big O (O())&lt;/code&gt; stands for the upper bound of the complexity.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Little o (o())&lt;/code&gt; stands for the upper bound excluding the exact bound.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Omega (Ω())&lt;/code&gt; stands for the lower bound of the complexity.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Theta (Θ())&lt;/code&gt; stands for the exact bound of the complexity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is about time to look at the complexity order across the BigO, from the least complex to the most complex ones.&lt;/p&gt;

&lt;p&gt;It is about time to look at the complexity order across the BigO, from the least complex to the most complex ones. &lt;/p&gt;

&lt;h3&gt;
  
  
  Big O Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;O(1)&lt;/code&gt; has the least complexity (constant complexity)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O(log(n))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O(n)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O(nlog(n))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O(n²)&lt;/code&gt;...&lt;code&gt;O(n⁴)&lt;/code&gt;, &lt;code&gt;O(n⁵)&lt;/code&gt;...&lt;code&gt;O(n⁹⁹)&lt;/code&gt;..and so on&lt;/li&gt;
&lt;li&gt;At some point here, we will face some inefficient algorithms with the big O of &lt;code&gt;O(N!)&lt;/code&gt; &lt;em&gt;factorial&lt;/em&gt;, which one can rarely face in day to day algorithm solving problems, especially coding ones due to its high inefficiency.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;p&gt;So far, we have only been discussing the time complexity of the algorithms or how fast is an algorithm. However, what also matters is the space the program takes to complete the task. The space complexity is related to how much memory the program will use, and therefore is also an important factor to take into count.&lt;/p&gt;

&lt;p&gt;The space complexity works similarly to time complexity. For example, the selection sort that we have worked with so far has a space complexity of &lt;code&gt;O(1)&lt;/code&gt;. Because it only stores one minimum value and its index for comparison, the maximum space used does not increase with the input size. It means that it doesn't need any additional memory space to store values while running. Of course, some algorithms can and will use additional space, and its big O can be &lt;code&gt;O(n)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;An example of such an algorithm would be the Bucket Sort. Bucket sort sorts the array by creating a sorted list of all the possible elements in an array, then increments the count whenever the element is encountered. In the end, the sorted array will be the sorted list elements repeated by their counts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bucket Sort Visually:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OOUb2ImT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tn9ya3x6othgo5t872q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OOUb2ImT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tn9ya3x6othgo5t872q.png" alt="Alt Text" width="332" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*Image taken from &lt;a href="https://en.wikipedia.org/wiki/Bucket_sort"&gt;Wikipedia&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Bucket sort works as follows:&lt;/p&gt;

&lt;p&gt;Set up an array of initially empty buckets.&lt;br&gt;
Go over the original array, putting each object in its bucket.&lt;br&gt;
Sort each non-empty bucket.&lt;br&gt;
Visit the buckets in order and put all elements back into the original array.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Last but not least&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Complexity Expectance
&lt;/h2&gt;

&lt;p&gt;The complexity can be analyzed as best case, worst case, average case and expected case.&lt;br&gt;
Let’s take insertion sort, for example. Insertion sort iterates through all the elements in the list. If the element is larger than its previous element, it inserts the element backwards until it is larger than the previous element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZSt12Tjy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28obn5kh5j1a0l31msdc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZSt12Tjy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28obn5kh5j1a0l31msdc.gif" alt="Alt Text" width="300" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*Image taken from &lt;a href="https://en.wikipedia.org/wiki/Insertion_sort"&gt;Wikipedia&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If an array has been initially sorted, then no swapping will be made. The algorithm will iterate through the array once, which has the time complexity of &lt;code&gt;O(n)&lt;/code&gt;. Therefore, we would say that the best-case time complexity of insertion sort is &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  To Wrap up:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Big O&lt;/code&gt; notation is a mathematical analysis applied by the algorithm. The results may defer, so we always have to take the edge cases into count. At this particular moment, we should be able to understand how and when to use &lt;code&gt;Big O&lt;/code&gt; analysis and manipulate our best performance.&lt;br&gt;
The whole story is based on my own experiences and researches. Please let me know if I have missed something or wrote something that brought you to confusion. There are plenty of articles on this topic, and they are waiting for you to read them Orestis, Omar, and you Otto.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>mathematics</category>
      <category>coding</category>
      <category>complexity</category>
    </item>
    <item>
      <title>Road to algorithm world</title>
      <dc:creator>Nemanja Milosavljevic</dc:creator>
      <pubDate>Tue, 05 Oct 2021 15:36:31 +0000</pubDate>
      <link>https://dev.to/nemanyam/road-to-algorithm-world-4a19</link>
      <guid>https://dev.to/nemanyam/road-to-algorithm-world-4a19</guid>
      <description>&lt;p&gt;What is an algorithm? I like to think that an algorithm is a set of rules specifying how to solve a problem. An algorithm can be defined outside of computer science, in Mathematics, nature or traffic. In computer science, an algorithm is abstract and does not have a machine implementation.&lt;/p&gt;

&lt;p&gt;The most important 'tool' to manipulate while coding is &lt;code&gt;data structures&lt;/code&gt;, and precisely that is the starting point of the road to algorithms. Data structures are simply a way to organize data. However, to be more specific, who'd say better than Wikipedia, right?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A data structure is a collection of data values, the relationships amongst them, and the functions or operations that can be applied to the data.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is the right time to tackle the topic of &lt;code&gt;complexity analysis&lt;/code&gt;. This is a process of determining how efficient an algorithm is. Therefore, we shall focus on two dimensions, time and space complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time complexity&lt;/strong&gt; &lt;em&gt;is a measure of how fast an algorithm runs, and it is expressed using &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt; notation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space complexity&lt;/strong&gt; &lt;em&gt;is a measure of how much auxiliary memory an algorithm takes, and it is also expressed by using &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt; notation.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt; is a big topic, therefore we could define here some base-lines. Let's say that memory is the place, or more precisely a layer where all data is stored. How? Well, there are the following points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data stored in memory is stored in bits and bytes.&lt;/li&gt;
&lt;li&gt;Bytes are able to point to other bytes in memory. (Referencing other data).&lt;/li&gt;
&lt;li&gt;Byte sizes, like 4 bytes, or 8 bytes in the case of 32-bit or 64-bit integers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Big O&lt;/strong&gt; notation is the most important tool to generalize the time or space complexity of an algorithm. These values aren't fixed, so they may vary depending on the input. Therefore, we measure from the most performant to the least performant as, O(1) as constant complexity, or O(log(n)), O(n), O(n^2), O(n^3)..etc..etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data structures&lt;/strong&gt; are generally presented in four forms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Linear: arrays, lists.&lt;/li&gt;
&lt;li&gt;Tree: binary, heaps, space partitioning etc.&lt;/li&gt;
&lt;li&gt;Hash: distributed hash table, hash tree etc.&lt;/li&gt;
&lt;li&gt;Graphs: decision, directed, acyclic etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Having these tools mentioned and collected in our 'backpack', we have pretty much everything to tackle, define, and then write our algorithms in the most efficient way to a given occasion.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hands on..
&lt;/h1&gt;

&lt;p&gt;How we are going to use all these items that we have in our 'techy backpack'? Well, we need a problem to start. Fair enough, here is one, an easy one to begin.&lt;/p&gt;

&lt;p&gt;Here is the problem from &lt;a href="https://leetcode.com/problems/two-sum/"&gt;LeetCode&lt;/a&gt; . Exactly what we need.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;Two Sum&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have exactly one solution, and you may not use the same element twice.&lt;/p&gt;

&lt;p&gt;You can return the answer in any order.&lt;/p&gt;

&lt;p&gt;For an instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nl"&gt;Input:&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="nl"&gt;Output:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nl"&gt;Output:&lt;/span&gt; &lt;span class="nc"&gt;Because&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when we have understood our problem, we shall cheer. &lt;strong&gt;'Yeeees!!!'&lt;/strong&gt;&lt;br&gt;
We know how to tackle the problem, we open our 'backpack' and choose our tools.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
           &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
              &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
              &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstNumber&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
              &lt;span class="o"&gt;}&lt;/span&gt;
           &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;'WooooooW'&lt;/strong&gt;, it works!&lt;/p&gt;

&lt;p&gt;Hmmm... Yes, it works indeed, but as we are beginners on this road, we have used &lt;code&gt;brute force&lt;/code&gt; technics, which means that we were passing through our array with two integers, respectively &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt;, and then we have tried to sum up every combination between the two. Therefore we have used, two nested for loops to iterate throughout the array. Shall we look at the already mentioned &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt; and its time and space complexity?&lt;br&gt;
Alright, the space complexity we've got is &lt;code&gt;O(1)&lt;/code&gt;. Splendid, couldn't be better, a constant space, which means no additional memory was used to store the data. Now then, we end up with an &lt;code&gt;O(n^2)&lt;/code&gt; time complexity. The reason for this was iterating through two nested loops in regards, to sum up, every combination. Well, we have to say that our algorithm seems to be very slow. What if we would have an n-sized array instead of just four integers in our example. Well, that would be very slow. So, can we do it better, now?&lt;/p&gt;

&lt;p&gt;Let's give it another try.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works again! We are doing fantastic, I must say. Let's talk again to our &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt; friend. Now we have a slightly better situation. We have improved our time complexity from &lt;code&gt;O(n^2)&lt;/code&gt; quadratic time to the &lt;code&gt;O(n)&lt;/code&gt; time. We got rid of the inner for loop, and we iterate only once throughout an array. We have introduced the HashSet data structure, and it helps us to store the potential matching combinations, and after every iteration, we look up the hash table to see if we summed up to the target number. Way faster than the previous solution. We have improved the time complexity significantly, and our algorithm is way faster now. However, by using the HashSet, we allocated some memory. Remember, we had to store our matching combinations somewhere, and that means our space complexity disimproved. It is &lt;code&gt;O(n)&lt;/code&gt; right now, instead of &lt;code&gt;O(1)&lt;/code&gt; in the previous example.&lt;/p&gt;

&lt;p&gt;So what's the better approach now?&lt;/p&gt;

&lt;p&gt;Well, in the first example we had &lt;code&gt;O(n^2) time | O(1)&lt;/code&gt; space complexity, and&lt;br&gt;
now we have &lt;code&gt;O(n) time | O(n)&lt;/code&gt; space complexity. I think we have improved our algorithm in general. Now, let's see if we can do even better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;twoSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third time success! Marvellous manoeuvre! Let's go and talk to our &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt; friend there. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ok, &lt;a href="https://dev.to/nemanyam/big-o-579p"&gt;&lt;code&gt;Big O&lt;/code&gt;&lt;/a&gt;, what do you have for us this time?&lt;/li&gt;
&lt;li&gt;Well, I have good news for you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We might know that a good sorting algorithm like quicksort, mergesort or heapsort can run in &lt;code&gt;O(n*log(n))&lt;/code&gt; time. So that explains sorting an array straight on the top, and then we can find the sum in &lt;code&gt;O(n)&lt;/code&gt; time, and we do not use any additional space, so again we have improved the space complexity back to &lt;code&gt;O(1)&lt;/code&gt;. With this set up we are allowed to do the next thing. As the array has been immediately sorted, we can surely put the left pointer at the beginning of an array, and the right one at the very end of an array. We will sum up the elements in the first cycle, and compare if the sum is equal to the target number. In the case of truth, we simply return the positions of the pointers, accordingly, if the sum is less than the target number we will have to increase the left pointer for one position to increase the sum of pointers. If the sum is, however, bigger than the target number, that would mean that we have to move the right pointer to the one position to the left, in regards to decrease the sum of pointers.&lt;/p&gt;

&lt;p&gt;To sum up, we have sorted an array immediately, and then we have iterated one time throughout the array. Finally, we were able to find our third solution in &lt;code&gt;O(n*log(n)) time | O(1)&lt;/code&gt; space complexity. This is a very cool solution but was it better than the previous one? Well, it depends on what are our needs. Do we want to care more about time or space complexity performance? I'll just let you decide for yourself.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Final thoughts&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;This was a showcase of how we can apply various technics, memory, and data structures to write performant algorithms. We've started with brute force technics which is usually the least performant one, and then we have tried to see what could be the possible improvement, searching for the better tools inside of our 'backpack'.&lt;/p&gt;

&lt;p&gt;I like to use this step-by-step solution. "Try to improve!" An old adage in carpentry is to &lt;code&gt;Measure Thrice, Check Twice and Cut Once!&lt;/code&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>bigo</category>
    </item>
  </channel>
</rss>
