<?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: Chrysanthus</title>
    <description>The latest articles on DEV Community by Chrysanthus (@chrys).</description>
    <link>https://dev.to/chrys</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1441070%2F1fceef60-320e-4248-8bec-e6cf24ff427f.png</url>
      <title>DEV Community: Chrysanthus</title>
      <link>https://dev.to/chrys</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chrys"/>
    <language>en</language>
    <item>
      <title>1.2 Time Complexity Explained in C</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Sun, 21 Jun 2026 04:28:48 +0000</pubDate>
      <link>https://dev.to/chrys/12-time-complexity-explained-in-c-5c2l</link>
      <guid>https://dev.to/chrys/12-time-complexity-explained-in-c-5c2l</guid>
      <description>&lt;h2&gt;
  
  
  1. Time and Space Complexity in C
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Full Course on Data Structures and Algorithms in C&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.&lt;/p&gt;

&lt;p&gt;Time Complexity refers to how fast a piece of code (a function for example, and not the whole program) runs from beginning to completion. Since different computers have different microprocessor clock frequencies and different sizes of memory, the same code will take different durations to run in different computers. Because of this issue and others, time complexity is given as relative speed.&lt;/p&gt;

&lt;p&gt;Time complexity is the maximum number of main operations the piece of code needs, to solve a type of problem, in the worst case situation. There is Constant Time Complexity, Linear Time Complexity, Logarithmic Time Complexity, Quadratic Time Complexity, Exponential Time Complexity and Factorial Time Complexity.&lt;/p&gt;

&lt;p&gt;A typical way to express time complexity, is with the Big-O notation, of which examples are given below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constant Time - O(1)
&lt;/h2&gt;

&lt;p&gt;Consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two statements in the body of the function, solution(). After compilation, these statements are in binary code, of more than two lines, but still not of many lines. These two statements are executed in a short time. This is known as constant time, written in Big-O notation as O(1), where the 1 in parentheses means, short duration or constant time. The exact time in milliseconds (seconds), is not emphasized upon, in the topic, time complexity. Constant time operation is best considered as one main operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linear Time - O(n)
&lt;/h2&gt;

&lt;p&gt;The above two statements can be considered as the main operation for the above function code. Consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="p"&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="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The for-loop displays 8 numbers, which are:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 2 3 4 5 6 7 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Though the statement, "printf("%i ", i);" takes slightly longer to execute than a previous statement, "int N = 8;" or the last statement, "printf("\n");" , it is the statement of interest, and it is executed (N = 8) times. The statement of interest, "printf("%i ", i);" is the main operation, in this function. &lt;/p&gt;

&lt;p&gt;Time complexity is the number of main operations in some piece of code. It is actually the maximum possible number of main operations in the code. In this situation, the first and last statements, "int N = 8;" and "printf("\n");" respectively, are ignored, when quoting the time complexity. They are also ignored, because they do not repeat. The repeating main operation, is called the dominant operation. Using the Big-O notation, the time complexity for this function is given as,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;where n in this case is 8. This is linear time, which can be appreciated as one complete scan of a list (consecutive counting).&lt;/p&gt;

&lt;h2&gt;
  
  
  Logarithmic Time – O(log n)
&lt;/h2&gt;

&lt;p&gt;The mathematical symbol, =&amp;gt;, means “this implies that”.&lt;/p&gt;

&lt;p&gt;Now,&lt;br&gt;
      8 = 2 x 2 x 2&lt;br&gt;
=&amp;gt;    8 = 2&lt;sup&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;In this second math statement, 2 is called the base, 3 is called the index and 8 is just called the number (number of interest). So 8 equals 2 raised to the power (index) 3, because 2 is multiplied by itself 3 times to give 8. &lt;/p&gt;

&lt;p&gt;The second mathematical statement above, can be rewritten as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        log₂ 8 = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;read as, log to the base 2 of 8 equals 3. The word, log, is of the math topic, logarithm. There are three mathematical statements above (in this section). The second and third are two different ways of writing the same thing. The two different ways, each has its pros and cons.&lt;/p&gt;

&lt;p&gt;From out of 8 possible repetitions of the main operation in a function, if the main operation repeats 3 times, then the time complexity is written as:&lt;/p&gt;

&lt;p&gt;O(log₂ n)&lt;/p&gt;

&lt;p&gt;using the Big-O notation, where n in this case is 8. The subscript 2 is optional to write. The logarithmic time, O(log n) is usually less than half the linear time, O(n), e.g 3 is less than half of 8, which is 4.&lt;/p&gt;

&lt;p&gt;For code example of logarithmic time, with the number 8, consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;N&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//integer division&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i, "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main operation consists of the two statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;            &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&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;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//integer division&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i, "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With integer division (N / 2), the remainder is abandon after each division. The output is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4, 2, 1,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That is, out of 8 possible operations, there are 3 operations, corresponding to the 3 numbers at the output. With time complexity, what matters is the number of main operations, and not necessarily, the number of numbers at the output. In this situation, the number of numbers at the output happens to be the same as the number of main operations (number of round loopings (iterations)). For each loop repeat, the main operation executes in constant time, O(1). For the 3 repeats, the function takes logarithmic time, O(3) as opposed to linear time O(8), out of 8 operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quadratic Time - O(n²)
&lt;/h2&gt;

&lt;p&gt;In the alphabet, A, B, C, D, E, F, G, H are characters, and each is less than (comes before) 'I'. Consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&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="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&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="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two for-loops, with one nested in the other. The inner-loop prints a row of 8 characters. The outer-loop enables the printing of 8 of these rows. The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
    A B C D E F G H 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an 8-by-8 table. The number of characters are 64 = 8 x 8. The main operation is 'printf("%c ", j);', which executes 8 x 8 times = 64 = 8²; that is 8 square. Squaring means quadratic. So the time complexity for this function is written as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;where n is 8 in this case. 8² = 8 x 8. This is quadratic time complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  n.log₂n Time
&lt;/h2&gt;

&lt;p&gt;n.log₂n means, n x log₂n . The mathematical symbol, ∴ means, therefore.&lt;/p&gt;

&lt;p&gt;From above,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      8 = 2 x 2 x 2
=&amp;gt;    8 = 2³
=&amp;gt;   log₂ 8 = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    if n = 8, then O(n²) = 64 operations = O(8²).

    if n = 8, it means log2 8 = 3;
    ∴ n x log2n = 8 x 3 = 24

64 = 8²
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 24 main operations for (8xlog₂ 8) time. 24 is less than half of 64, which is 32. 24 is greater than 3, which equals log₂8&lt;/p&gt;

&lt;p&gt;So, O(n.log₂n) time complexity lies between O(log₂n) and half of O(n²) , generally.&lt;/p&gt;

&lt;p&gt;For code example of O(n.log₂n) time, with the number 8, consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="p"&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;Q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;N&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//integer division&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i, "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main operation consists of the two statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;                &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&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;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//integer division&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i, "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With integer division, the remainder is abandon after each division. The while-loop does the main operation, 3 times. The for-loop does what is in the while-loop 8 times. This makes 8 x 3 = 24. The exact time complexity from O(n.log₂n) here, is O(8.log₂ 3) . The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1, 
    4, 2, 1,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 8 rows and 3 columns, giving 24 output entries; in this case, corresponding to 24 main operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  n + m Linear Time : O(n + m)
&lt;/h2&gt;

&lt;p&gt;For code example of n + m linear time, with the number n = 8, consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="n"&gt;start&lt;/span&gt;&lt;span class="p"&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&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="sc"&gt;'J'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//'I' is 9th letter in alphabet&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main operation is "printf("%i ", i);" or "printf("%c ", i);" for either non-nested for-loops. The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1 2 3 4 5 6 7
    A B C D E F G H I
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first for-loop in the code, produces 7 out of 8=n main operations. The second for-loop in the code, produces 9 out of 8=n main operations. The first set of main operations is referred to as, n operations; and the second set of main operations is referred to as, m operations. Here, n + m = 7 + 9 = 16 (or about 16).&lt;/p&gt;

&lt;p&gt;Here, n + m = 16 operations lies between the particular time complexity O(log₂ 8) = 3 operations, and the particular time complexity O(8.log₂ 8) = 24 operations. n + m time generally lies between O(log₂ n) time and O(n.log₂ n) time.&lt;/p&gt;

&lt;p&gt;Even if there were three non nested for-loops, as in the following code, the time complexity would still be given as O(n + m) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="n"&gt;start&lt;/span&gt;&lt;span class="p"&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&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="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//'I' is 9th capital letter in alphabet&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&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="sc"&gt;'j'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//'j' is 10th small letter in alphabet&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1 2 3 4 5 6 7
    A B C D E F G H 
    a b c d e f g h i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Nuance to do with ½n, 2n, etc.
&lt;/h2&gt;

&lt;p&gt;In an algorithm where the upper limit is 8 = n (linear time), 4 main operations = ½n or 16 main operations = 2n, is given simply as O(n). In other-words, fractions and multiples (coefficients) in front of time complexity expressions are avoided.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exponential Time Complexity : O(2ⁿ)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;21 = 2
22 = 2 x 2
23 = 2 x 2 x 2
24 = 2 x 2 x 2 x 2
25 = 2 x 2 x 2 x 2 x 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is generally written as:&lt;/p&gt;

&lt;p&gt;2ⁿ = 2 x 2 x 2 - - - x nᵗʰ2&lt;/p&gt;

&lt;p&gt;2ⁿ is called the nᵗʰ power of 2. Time complexity of O(2⁵) means, there are 2⁵ = 2 x 2 x 2 x 2 x 2 = 32 main operations. In general, O(2ⁿ) means, there are 2ⁿ operations (maximum).&lt;/p&gt;

&lt;p&gt;Multiplication is continuous addition; so 2⁵ = 2 x 2 x 2 x 2 x 2 = 32, is the same as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1 + 1 +
    1 + 1 + 
    1 + 1 + 1 + 1 + 
    1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 
    1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main operation is just addition. There are 31 addition operations here. However, as concerns time complexity, the number of additions are assumed to be 32 (upper limit).&lt;/p&gt;

&lt;p&gt;The following function is supposed to be for 2⁵ main operations, of time complexity, O(2⁵), and generalized as O(2ⁿ) . The first two main operations are outside the nesting loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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;2&lt;/span&gt;&lt;span class="p"&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="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    &lt;span class="c1"&gt;//last 3 rows above, approached in halves&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;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&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;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is 32. The statements, "int number = 1 + 1;" and "int sum = 1 + 1;" form the first two main operations in the code of interest. There are twenty eight more coded additions, each of which is just the addition of 1. The statements in the parentheses of the nesting for-loops are ignored, in determining the time complexity. The statements, "sum = number;" and 'printf("%i\n", number);' are also ignored. The reader should read through the function again, to really understand what is going on.&lt;/p&gt;

&lt;p&gt;The code does 30 coded additions in the nested for-loop, and two additions outside the nesting for-loop; but the 5 rows of manual additions of 1’s, has 31 addition operations. That is how time complexity is, sometimes. The time complexity for this situation, is given as O(2⁵), and generalized as O(2ⁿ).&lt;/p&gt;

&lt;h2&gt;
  
  
  Factorial Time Complexity : O(n!)
&lt;/h2&gt;

&lt;p&gt;Now,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      5! = 5 x 4 x 3 x 2 x 1 = 120
=&amp;gt;    5! = 120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that x1 (times 1) does not change the previous product of x2.&lt;/p&gt;

&lt;p&gt;Multiplication is continuous addition; so the above is the same as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 20 +

    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 40 +

    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 60 +

    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 80 +
    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 100 +
    (1 + 1 + 1 + 1 + 1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) + (1 + 1 + 1 + 1 +1) = 120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the 1’s counted in this series gives 120. However, there are 119 addition operations in the series. Accept that.&lt;/p&gt;

&lt;p&gt;The 1’s are in two groups for x2 (times 2). The first group has 1’s in three rows for x3. Each row is five 1’s times four. The second group also has 1’s in three rows . Each row is five 1’s times four.&lt;/p&gt;

&lt;p&gt;The following function is for 5! = 120 main operations, of time complexity, O(5!), and generalized as O(n!) . The first main operation is outside the nesting for-loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;newAddition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;oldAddition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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;newAddition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;newAddition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;oldAddition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;oldAddition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&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="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//5 x 4 = 20, then 20 x 3 = 60. then 20 x 2 = 120. x1 (times 1) does not change the previous product of x2&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is 120. There are 119 addition operations in the code. The time complexity is given as O(120), and not O(119), and generalized as O(n!). Accept that. The main operation is "factorial = factorial + 1;", where factorial is just a variable; and the rest of the operation statements in the code, are ignored, in the quotation of time complexity – accept that. If n = 4, then all the values of 5 in the code, have to be changed to 4. The reader should read through the function to really understand what is going on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time limit
&lt;/h2&gt;

&lt;p&gt;Nowadays, an average computer can perform 10⁸ operations in less than a second. Sometimes the programmer has the information he needs about the expected time complexity, but at other times the programmer does not.&lt;/p&gt;

&lt;p&gt;During contests, the candidate is often given a limit on the size of data; and therefore he can estimate the time complexity within which the task should be solved. This is usually a great convenience because he can look for a solution that works in a specific time complexity, instead of worrying about a faster solution. For example, at codility.com, if the size of the data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    -  n &amp;lt;= 1 000 000, the expected time complexity is O(n) or O(n log n),
    -  n &amp;lt;= 10 000, the expected time complexity is O(n² ),
    -  n &amp;lt;= 500, the expected time complexity is O(n³ ).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, these limits are not precise. They are just approximations, and will vary depending on the specific task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approximation of Time Complexity
&lt;/h2&gt;

&lt;p&gt;Time complexity, of O(10) may actually have 8 or 9 operations, or even 11 or 12 operations, or the 10 itself, though the maximum limit of 8 is aimed at. In many cases it would have 8 operations or less.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Problem 1:&lt;/strong&gt; You are given an integer n. Count the total of 1 + 2 + . . . + n, in O(n²) time.&lt;br&gt;
&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
The following program has one test case in the main() function (read the code and comments in the whole program). In the function, 1 is 1; 2 is 1 + 1; 3 is 1 + 1 + 1; 4 is 1+1+1+1; etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;slow_solution&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//using zero based indexing &lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="p"&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="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//i=0 or j=0 refers to the first element, 1&lt;/span&gt;
                &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;//i=1 or j=1 refers to the second element, 2, etc.&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow_solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is 10, for input of N = 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem 2:&lt;/strong&gt; Repeat the above problem for the time complexity of O(n).&lt;br&gt;
&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
The following program has one test case in the main() function (read the comments in the whole program). The function does 1 + 2 + 3 + 4 = 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fast_solution&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//using zero based indexing &lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="p"&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="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&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="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//1=0+1; 2=1+1; 3=2+1; etc.&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;//1 + 2 + 3 + 4 = 10&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast_solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is 10; same as above, for input of N = 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem 3:&lt;/strong&gt; Repeat the above problem for the constant time complexity, O(1).&lt;br&gt;
&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
This is a mathematical Arithmetic Progression, with the first term, a = 1, and common difference, d = 1. The sum, Sₙ of the first n terms, of an arithmetic progression, is given by,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Sₙ = n/2[2a + (n-1)d]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This formula is one main operation and should be coded as such. The following program has one test case in the main() function (read the comments for the whole program):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;model_solution&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//formula: sum of arithmetic progression, with first element, 1 and difference, 1&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&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;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&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;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&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="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//one operation, without repetition in any loop&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//sum = n/2*(2*1 + (n-1)*1)&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model_solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%i&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is 10; same as above, for input of N = 4. There is only one main operation here.&lt;/p&gt;

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

&lt;p&gt;Memory limits provide information about the expected space complexity. The programmer can estimate the number of variables that he can declare in his programs. In short, if he has constant numbers of variables, he also has constant space complexity: in Big-O notation this is O(1). If he needs to declare an array with n elements, he has linear space complexity - O(n).&lt;/p&gt;

&lt;p&gt;More specifically, space complexity is the amount of memory needed to perform the computation. It includes all the variables, both global and local, dynamic pointer data-structures, and in the case of recursion, the contents (space) of the stack. Depending on the convention, input data may also be included. Space complexity is more tricky to calculate than time complexity, because not all of these variables and data-structures may be needed at the same time. Global variables exist and occupy memory all the time; local variables (and additional information kept on the stack) will exist only during invocation of the function. There are also temporary variables needed by the algorithm.&lt;/p&gt;

&lt;p&gt;In many problems, it is the time complexity that is expected to be met, and the space complexity limit is not indicated.&lt;/p&gt;

&lt;p&gt;Whatever is the case, space complexity is explained in the next part of the series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The time complexity of a piece of code, is the number of main operations in the code. This is a good indication of how fast the code will run on a particular system (computer). The main operation may consist of one, or two or three simple statements.&lt;/p&gt;

&lt;p&gt;If the main operation consists of many statements, then single statements sprinkled here and there in the code, will be ignored in the quotation of the time complexity. &lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>c</category>
      <category>time</category>
      <category>complexity</category>
    </item>
    <item>
      <title>1.1 Looping in C</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Fri, 19 Jun 2026 04:22:02 +0000</pubDate>
      <link>https://dev.to/chrys/11-looping-in-c-1ib0</link>
      <guid>https://dev.to/chrys/11-looping-in-c-1ib0</guid>
      <description>&lt;h2&gt;
  
  
  1. Time and Space Complexity in C
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Full Course on Data Structures and Algorithms in C&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.&lt;/p&gt;

&lt;p&gt;In programming, iterating means repeating some part of the code to obtain different results. This involves looping.&lt;/p&gt;

&lt;p&gt;A loop is a compound statement whose body can be executed again and again. The body of the loop has a number of statements. A loop needs an initial state or initial statement, from which the loop will execute the first time, before repeating. Repeating, means all the statements in the body of the loop are re-executed, in order, again and again. In order for the loop to repeat after the first pass or any pass of the body, there has to be a statement that will cause it to repeat. In order for a loop to stop repeating, there has to be a condition that will cause the loop not to repeat./p&amp;gt;&lt;/p&gt;

&lt;p&gt;This tutorial presents basic programming constructs that allow iterations to be performed in C (in a loop). The names of the different loops are: the do-while loop, the while-loop and the for-loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do-while Loop Syntax
&lt;/h2&gt;

&lt;p&gt;The syntax for the do-while loop construct is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//initial statement here (initialization)
do {
    //statements
    //cause for next iteration (continue Statement)
} while (condition);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This construct should be read as follows: Considering the initial statement, do all the statements in the loop, while the condition allows it. The initial statement ends with a semicolon. The do-compound statement itself, also ends with a semicolon. Note that “while” here, is a reserved word.&lt;/p&gt;

&lt;p&gt;There are three main loops in C: the do-while loop, the while-loop and the for-loop. This tutorial (lesson ) explains the do-while loop, the while-loop and the for-loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  do-while Loop Example
&lt;/h2&gt;

&lt;p&gt;Using the above syntax, an example of a do-while loop coding, is in the following program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0 1 2 3 4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The complete construct begins with “int myInt = 0;” and ends at “while (myInt &amp;lt; 5);”. There are two simple statements in the braces. The first statement in the braces, prints out the value of the integer, myInt. The second statement increments myInt, by adding 1 to it. The condition is “while (myInt &amp;lt; 5)”. So, while myInt is less than 5, the compound statement is re-executed.&lt;/p&gt;

&lt;p&gt;This loop has just one main simple statement, which is to print the value of myInt. The second simple statement is to cause the next iteration. The curly brackets can have more than one main simple statement. The following do-while loop has two main simple statements. The first one adds 2 to myInt, and the second one prints the result of the addition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 5 8 11 14&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This output needs explanation. First of all, note that the while condition has been changed to "while (myInt &amp;lt; 13)".&lt;/p&gt;

&lt;p&gt;When myInt is 0, 2 is added to it, and myInt becomes 2. Two is printed. The increment adds 1 to myInt and it becomes 3, at the beginning of the next pass. In the next iteration (pass) myInt is 3. Two is added to it again and it becomes 5. The increment adds 1 to myInt and it becomes 6. In the next iteration myInt is 6. Two is added to it again and it becomes 8. The increment adds 1 to myInt and it becomes 9. In the next iteration myInt is 9. 2 is added to it again and it becomes 11. The increment adds 1 to myInt and it becomes 12. In the next iteration myInt is 12. 2 is added to it again and it becomes 14. The increment adds 1 to myint and it becomes 15.&lt;/p&gt;

&lt;p&gt;After each iteration, the while condition is checked. At this point, that the while condition is checked, the myInt is 15, above 13, after 14 has been printed. The condition results in false, and the repetition of the block, stops.&lt;/p&gt;

&lt;h2&gt;
  
  
  while-loop
&lt;/h2&gt;

&lt;p&gt;The syntax for the while-loop is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//initial statement here (initialization)&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//statements&lt;/span&gt;
        &lt;span class="c1"&gt;//cause for next iteration (continue Statement)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main difference between the do-while loop and the while-loop is, that for the while-loop, the condition is checked first, before the block is executed. With the do-while loop, the block is executed first, before the condition is checked. Note that the while-loop construct, does not end with a semicolon.&lt;/p&gt;

&lt;p&gt;The following program repeats the first program above, but with a while-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is the same as for the first program above, that is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0 1 2 3 4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The following program repeats the second program above, but with a while-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is the same as for the second program above, that is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 5 8 11 14&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  for-loop
&lt;/h2&gt;

&lt;p&gt;The syntax for the for-loop is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block of the for-loop does not end with a semicolon. The output is the same as for the first program above, that is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0 1 2 3 4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is no semicolon after the increment statement (last statement) in the parentheses.&lt;/p&gt;

&lt;p&gt;The following program repeats the second program above, but with a for-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is the same as for the second program above, that is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 5 8 11 14&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The do-while loop in C, repeats the execution of its block, as long as a condition is true. The do-while loop needs an initial statement (state), before the block. The do-while loop needs a cause-for-next-iteration (increment) statement, usually towards, the end of its block. The main difference between the do-while loop and the while-loop, is that, with the do-while loop, the block is always executed before the condition is checked; while with the while-loop, the condition is always checked before the block is executed. Both the do-while loop and the while-loop do essentially the same thing. The for-loop is a concise construct for the do-while loop or while-loop.&lt;/p&gt;

</description>
      <category>c</category>
      <category>loop</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Explanations for Tasks and Solutions in C++, JavaScript, etc. for the 17 Algorithm Lessons at app-codility-com/programmers</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 05 Jan 2026 03:06:49 +0000</pubDate>
      <link>https://dev.to/chrys/explanations-for-tasks-and-solutions-in-c-javascript-etc-for-the-17-algorithm-lessons-at-27gp</link>
      <guid>https://dev.to/chrys/explanations-for-tasks-and-solutions-in-c-javascript-etc-for-the-17-algorithm-lessons-at-27gp</guid>
      <description>&lt;h2&gt;
  
  
  Task score : 100% - Correctness : 100% ; Performance : 100%
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Detected time complexity : - See particular solution.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Click Links for Explanations of Tasks and Solutions below.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in C++ , for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Cpp-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Cpp-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in JavaScript, for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-JavaScript-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-JavaScript-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in PHP, for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-PHP-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-PHP-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in C, for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations%20in%20C%20for%2017%20Algorithm%20Lessons%20at%20app-codility-com-programmers%20with%20Problems%20and%20Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations%20in%20C%20for%2017%20Algorithm%20Lessons%20at%20app-codility-com-programmers%20with%20Problems%20and%20Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in Java, for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Java-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Java-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in Go, for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Go-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-Go-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanations for Tasks and Solutions in C# (C-Sharp), for the 17 Algorithm Lessons at app-codility-com/programmers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is part of Complete Data Structures and Algorithms Course.&lt;br&gt;
Do not forget that you should be paid for reading this. &lt;br&gt;
If you have registered at broad-network website, and you have not received any feedback, complain here.&lt;br&gt;
The link is (click):&lt;br&gt;
&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-CS-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm" rel="noopener noreferrer"&gt;https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Explanations-in-CS-for-17-Algorithm-Lessons-at-app-codility-com-programmers-with-Problems-and-Solutions.htm&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>javascript</category>
      <category>php</category>
      <category>go</category>
    </item>
    <item>
      <title>9. Red-Black Trees and B-Trees in C</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 15:15:18 +0000</pubDate>
      <link>https://dev.to/chrys/9-red-black-trees-and-b-trees-in-c-5eao</link>
      <guid>https://dev.to/chrys/9-red-black-trees-and-b-trees-in-c-5eao</guid>
      <description>&lt;h2&gt;
  
  
  9. At toptal-com : Algorithm : What are Red-Black Trees and B-Trees? What is the best use case for each of them? Use C if necessary. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;p&gt;Both Red-Black Trees and B-Trees are balanced search trees that can be used on items that can have a comparison operator defined on them. They allow operations like minimum, maximum, predecessor, successor, search, insert, and delete, in O(log N) time (with N being the number of elements). Thus, they can be used for implementing a map, priority queue, or index of a database, to name a few examples.&lt;/p&gt;

&lt;p&gt;Red-Black Trees are an improvement upon Binary Search Trees. Binary Search Trees use binary trees to perform the named operations, but the depth of the tree is not controlled - so operations can end up taking a lot more time than expected. Red-Black Trees solve this issue by marking all nodes in the tree as red or black, and setting rules of how certain positions between nodes should be processed. This method guarantees that the longest branch isn’t more than twice as long as the shortest branch, so each branch is shorter than 2*log_base2(N).&lt;/p&gt;

&lt;p&gt;Read black tree is the ideal structure for implementing ordered maps and priority queues. B-Trees branch into K-2K branches (for a given number K) rather than into 2, as is typical for binary trees. Other than that, they behave pretty similarly to a binary search tree. This has the advantage of reducing access operations, which is particularly useful when data is stored on secondary storage or in a remote location. This way, we can request data in larger chunks, and by the time we finish processing a previous request, our new request is ready to be handled. This structure is often used in implementing databases, since they have a lot of secondary storage access.&lt;/p&gt;

</description>
      <category>red</category>
      <category>black</category>
      <category>b</category>
      <category>trees</category>
    </item>
    <item>
      <title>8. Design an algorithm that finds the number of ways in which you can traverse N meters</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 15:06:49 +0000</pubDate>
      <link>https://dev.to/chrys/8-design-an-algorithm-that-finds-the-number-of-ways-in-which-you-can-traverse-n-meters-13hl</link>
      <guid>https://dev.to/chrys/8-design-an-algorithm-that-finds-the-number-of-ways-in-which-you-can-traverse-n-meters-13hl</guid>
      <description>&lt;h2&gt;
  
  
  8. At toptal.com : Algorithm : Design an algorithm that finds the number of ways in which you can traverse N meters by doing jumps of 1, 2, 3, 4, or 5 meter lengths. Assume that N can be a very large number. What is the resulting complexity? Use C. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Dynamic Programming will be used. &lt;/p&gt;

&lt;p&gt;The sequence of jumps can be of the set m={1} with 1 being maximum.&lt;br&gt;
The sequence of jumps can be of the set m={1, 2} with 2 being maximum.&lt;br&gt;
The sequence of jumps can be of the set m={1, 2, 3} with 3 being maximum.&lt;br&gt;
The sequence of jumps can be of the set m={1, 2, 3, 4} with 4 being maximum.&lt;br&gt;
The sequence of jumps can be of the set m={1, 2, 3, 4, 5} with 5 being maximum.&lt;br&gt;
The sequence of jumps can be of the set m={1, 2, 3, 4, 5, 6} with 6 being maximum.&lt;br&gt;
And so on . . .&lt;/p&gt;

&lt;p&gt;The question uses the sequence of jumps, {1, 2, 3, 4, 5} with 5 being maximum. The dynamic programming table (2D array) below, provides sequences from m={} to m={1, 2, 3, 4, 5}. The maximum of each sequence is quoted as m in the table, and even in the code.&lt;/p&gt;

&lt;p&gt;A jump means reaching the end of a meter. Traversing a meter also means reaching the end of the meter. For example, given m={1, 2} and N=2 for length of a path (number of meters), there are two ways of traversing the two meters. One way is to jump by 1 step to reach the end of the first meter and then jump another 1 meter to reach the end of the second meter. The other way is just to jump 2 meters at once, in order to reach the end of the second meter.&lt;/p&gt;

&lt;p&gt;For all traversing, the end of the last meter is not crossed, except for the case when N=0. When N=0, there is only one way to traverse the 0 meter. Accept that without proof. If m={1}, the number of ways is 1 for N=0. If m={1, 2}, the number of ways is still 1 for N=0. If m={1, 2, 3}, the number of ways is still 1 for N=0; and so on. In this case, the 0 meter is crossed, and that is the only case for crossing the last meter, where in this case, the last meter does not even exist.&lt;/p&gt;

&lt;p&gt;"&lt;strong&gt;N can be a very large number&lt;/strong&gt;" means that N is at least 10 times m, where m here, refers to the largest number in the set of m. &lt;/p&gt;
&lt;h2&gt;
  
  
  Illustration
&lt;/h2&gt;

&lt;p&gt;In this section, it is explained how to produce the 2D dp array using examples of m and N. "dp" means dynamic programming. The main set used is m={1, 2, 3}.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;m={1, 2, 3} and different values of N&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2, 3} and N=0&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
When m={1, 2, 3} and N=0, there is only one way to traverse the N meters (see above).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2, 3} and N=1&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
There is only one way which is the jump:&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2, 3} and N=2&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Two ways:&lt;br&gt;
either &lt;br&gt;
1, 1&lt;br&gt;
or&lt;br&gt;
2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2, 3} and N=3&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Four ways:&lt;br&gt;
either &lt;br&gt;
1, 1, 1&lt;br&gt;
or&lt;br&gt;
2, 1&lt;br&gt;
or&lt;br&gt;
1, 2&lt;br&gt;
or&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2, 3} and N=4&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Seven ways:&lt;br&gt;
either &lt;br&gt;
1, 1, 1, 1&lt;br&gt;
or&lt;br&gt;
2, 1, 1&lt;br&gt;
or&lt;br&gt;
1, 2, 1&lt;br&gt;
or&lt;br&gt;
1, 1, 2&lt;br&gt;
or&lt;br&gt;
2, 2&lt;br&gt;
or&lt;br&gt;
3, 1&lt;br&gt;
or&lt;br&gt;
1, 3&lt;/p&gt;

&lt;p&gt;Note that here, N=4 = 3+1 = m+1, where m in this case is the highest value in the set for m. When m={1, 2, 3} and N=4, the number of ways is the sum of the 3 previous ways, with N=3, N=2 and N=1; which is four + two + one = seven. &lt;/p&gt;

&lt;p&gt;With this trend, when m={1, 2, 3} and N=5, the number of ways should be the sum of the 3 previous ways, with N=4, N=3, and N=2; which is seven + four + two = thirteen.&lt;/p&gt;

&lt;p&gt;The above illustration has been made with m={1, 2, 3} for different values of N. In the code, these different values of N is actually referred to, with the variable k. The next and last illustration will be done with m={1, 2} for different values of N.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;m={1, 2} and different values of N&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2} and N=0&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When m={1, 2} and N=0, there is only one way to traverse the N meters (see above). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2} and N=1&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
There is only one way which is the jump:&lt;br&gt;
1.&lt;br&gt;
A jump of 2 has excess.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2} and N=2&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Two ways:&lt;br&gt;
either &lt;br&gt;
1, 1&lt;br&gt;
or&lt;br&gt;
2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2} and N=3&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Three ways:&lt;br&gt;
either &lt;br&gt;
1, 1, 1&lt;br&gt;
or&lt;br&gt;
2, 1&lt;br&gt;
or&lt;br&gt;
1, 2&lt;/p&gt;

&lt;p&gt;Note that here, N=3 = 2+1 = m+1, where m in this case is the highest value in the set for m. When m={1, 2} and N=3, the number of ways is the sum of the 2 previous ways, with N=2 and N=1; which is two + one = three.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let m={1, 2} and N=4&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Five ways:&lt;br&gt;
either &lt;br&gt;
1, 1, 1, 1&lt;br&gt;
or&lt;br&gt;
2, 1, 1&lt;br&gt;
or&lt;br&gt;
1, 2, 1&lt;br&gt;
or&lt;br&gt;
1, 1, 2&lt;br&gt;
or&lt;br&gt;
2, 2&lt;/p&gt;

&lt;p&gt;With this trend, when m={1, 2} and N=4, as can be seen above, the number of ways is the sum of the 2 previous ways, with N=3 and N=2; which is three + two = five.&lt;/p&gt;

&lt;p&gt;The above illustration has been made with m={1, 2} for different values of N. In the code, these different values of N is actually referred to, with the variable k.&lt;/p&gt;

&lt;p&gt;The strategy for these illustrations can be used to obtain answers for different combinations of the set m and the value N.&lt;/p&gt;
&lt;h2&gt;
  
  
  The 2D dp Array
&lt;/h2&gt;

&lt;p&gt;The above strategy is used to produce the following two dimensional dp array: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; m/k    0    1    2    3    4    5     6     7     8      9      10     11

 0      ∞    ∞    ∞    ∞    ∞    ∞     ∞     ∞     ∞      ∞      ∞      ∞
 1      1    1    1    1    1    1     1     1     1      1      1      1
 2      1    1    2    3    5    8     13    21    34     55     89     144
 3      1    1    2    4    7    13    24    44    81     149    274    504
 4      1    1    2    4    8    15    29    56    108    208    401    773
 5      1    1    2    4    8    16    31    61    120    236    464    912
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Each value for m represents a set for the sequence m, where the variable, m in the table (2D array), represents the highest integer in its set.&lt;/p&gt;

&lt;p&gt;The variable, k represents the different values of N in the table (2D array).&lt;/p&gt;
&lt;h2&gt;
  
  
  The Logical Pattern
&lt;/h2&gt;

&lt;p&gt;Observe from the table that, for a given value of m, the answer for a value of N, is the sum of the previous m answers. &lt;/p&gt;

&lt;p&gt;For example, when m={1, 2, 3, 4, 5} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = 61 = n(7) = 31 + 16 + 8 + 4 + 2 = 61
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;When m={1, 2, 3, 4} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = 56 = n(7) = 29 + 15 + 8 + 4 = 56
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;When m={1, 2, 3} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = 44 = n(7) = 24 + 13 + 7 = 44
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;When m={1, 2} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = 21 = n(7) = 13 + 8 = 21
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;When m={1} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = 1 = n(7) = 1 = 1; just count once    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;When m={} and N=7,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer = ∞ = n(7) = ∞ = ∞   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The first row of the table, with values of infinity, should not be part of the solution (code).&lt;/p&gt;

&lt;p&gt;In general,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n[k] = n[k-1] + n[k-2] +  - - - + n[k-m]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This needs a one dimensional array called n, and not a two dimensional array as anticipated above. In the case of m={1, 2, 3, 4, 5} for the question,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n[k] = n[k-1] + n[k-2] + n[k-3] + n[k-4] + n[k-5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The code solution for the question, should address the last row in the table above (2D dp array), from m=5.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer for given m and N&lt;/strong&gt;&lt;br&gt;
The next question in search of the complete solution, is how to get the next answer (2D array dp cell value) for a particular value of m and N.&lt;/p&gt;

&lt;p&gt;The solution for the problem, is to find all the values for a row in the dp table. In the course of doing that, each cell value for a particular m and N is obtained.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Explanation with Row 3 *&lt;/em&gt;&lt;br&gt;
Here, k can take different values up to N (inclusive), but m remains at 3. For quick reference by the reader, row 3 above is retyped here; thus:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1    1    2    4    7    13    24    44    81     149    274    504
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;where the first number on the left is for k=0. (do not forget that when k=0, the first number in any row is 1.) &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=0&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (any sequence) to cross 0, in the set m={1, 2, 3} is given as 1 (see above). Only when N=0, the path can be crossed. Otherwise, the path (traversing) ends at the end of the last meter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k = 1&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (1,) to sum to 1, in the set m={1, 2, 3} is 1. Remember, any of the numbers in the set m={1, 2, 3}, can be used more than once. Also, considering the highest sequence number, 3 as m, then it should be noticed that k is less than m here. This stage has a count of 1, which is equal to k, the lesser of k and m.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=2&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (1, 2) to sum to 2, in the set m={1, 2, 3} is 2. Remember, any of the numbers in the set m={1, 2, 3}, can be used more than once. Also, considering the highest sequence number, 3 as m, then it should be noticed that k is less than m here. This stage has a count of 2, which is equal to k, the lesser of k and m.&lt;/p&gt;

&lt;p&gt;The number of ways, 2 can also be obtained by adding the two previous answers of 1 and 1. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=3&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (1, 2, 3) to sum to 3, in the set m={1, 2, 3} is 4. Remember, any of the numbers in the set m={1, 2, 3}, can be used more than once. Also, considering the highest sequence number, 3 as m, then it should be noticed that k is of the same value as m here (so either can be considered the less). This stage has a count of 3, which is equal to k or m (neither of which is the bigger).&lt;/p&gt;

&lt;p&gt;The number of ways, 4 can also be obtained by adding the three previous answers of 1, 1 and 2. Henceforth, with increasing value of k, the number of ways (answer), is the sum of the previous three answers, when m={1, 2, 3}.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=4&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (1, 2, 3) to sum to 7, in the set m={1, 2, 3} is 7. Remember, any of the numbers in the set m={1, 2, 3}, can be used more than once. Also, considering the highest sequence number, 3 as m, then it should be noticed that m is less than k here. This stage has a count of 3, which is equal to m, the lesser of k and m.&lt;/p&gt;

&lt;p&gt;The number of ways, 7 can also be obtained by adding the three previous answers of 1, 2 and 4. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=5&lt;/strong&gt;&lt;br&gt;
Number of ways of arranging (1, 2, 3) to sum to 13, from the set m={1, 2, 3} is 13. Remember, any of the numbers in the set m={1, 2, 3}, can be used more than once. Also, considering the highest sequence number, 3 as m, then it should be noticed that m is less than k here. This stage has a count of 3, which is equal to m, the lesser of k and m.&lt;/p&gt;

&lt;p&gt;The number of ways, 13 can also be obtained by adding the three previous answers of 2, 4 and 7. &lt;/p&gt;

&lt;p&gt;The answers (number of ways) of the higher values of k are similarly obtained.&lt;/p&gt;

&lt;p&gt;The other rows for the 2D dp array are similarly obtained, but with different sets of m (such as m={1,2} and m={1, 2, 3, 4}). &lt;/p&gt;

&lt;p&gt;The question for this article, requires that m={1, 2, 3, 4, 5}. The question does not specify any value for N (k), so N=7 is chosen for the code below.&lt;/p&gt;

&lt;p&gt;The complete algorithm below, produces the last row of the 2D array above, from k=0 to k=7 and not k=11. The last value (on the right) corresponding to k=7  is returned.&lt;/p&gt;
&lt;h2&gt;
  
  
  O(N) Code
&lt;/h2&gt;

&lt;p&gt;Remember that N is very large, in order not to result in N^2 number of operations. A summary for the complete algorithm here, is as follows:&lt;/p&gt;

&lt;p&gt;Beginning from k=1 with n[k] = 0, add all the previous answers, for the new answers of k, until k=m. After that, add all the previous m answers for the new answer, where k is greater than m, until the variable, i=m (see below). The program is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    #include &amp;lt;stdio.h&amp;gt;
    #include &amp;lt;math.h&amp;gt;    //for fmin()

    int  ways(int N, int m) {    //m is the maximum sequence number in set, m
        int n[N+1];

        n[0] = 1;    //first value of array n, for N = 0

        for (int k=1; k&amp;lt;=N; k++) {
            n[k] = 0;    //initial sum to obtain cell value
            for (int i=0; i &amp;lt;= fmin(m, k); i++)
                n[k] = n[k] + n[k - i];
        }

        return n[N]; 
    }

    int main(int argc, char **argv) 
    {
        int ret = ways(7, 5);
        printf("%i\n", ret);

        return 0; 
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not forget to compile the program with a command like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc temp.c -o temp.exe -lm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if linux or its variant (such as Ubuntu) is used, where the switch -lm, ensures that the fmin() function is applicable.&lt;/p&gt;

&lt;p&gt;The output is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;61
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This solution has a time complexity of O(N).&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>number</category>
      <category>ways</category>
      <category>traverse</category>
      <category>nmeters</category>
    </item>
    <item>
      <title>7. How would You find the Length of the Longest Common Subsequence of Elements in two Arrays</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 14:36:25 +0000</pubDate>
      <link>https://dev.to/chrys/how-would-you-find-the-length-of-the-longest-common-subsequence-of-elements-in-two-arrays-4ce2</link>
      <guid>https://dev.to/chrys/how-would-you-find-the-length-of-the-longest-common-subsequence-of-elements-in-two-arrays-4ce2</guid>
      <description>&lt;h2&gt;
  
  
  7. At toptal.com : Algorithm : How would you, in general terms, describe dynamic programming? As an example, how would you find the length of the longest common subsequence of elements in two arrays by using this method? Use C. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Description of Dynamic Programming
&lt;/h2&gt;

&lt;p&gt;Dynamic programming is a paradigm for solving optimization problems. It consists of finding solutions for intermediate subproblems, which can be stored and reused for solving the actual problem. Dynamic programming is the best approach for difficult problems that always become trivial once we know the solution for a slightly easier instance of that problem - the intermediate subproblem. Dynamic programming solutions are best represented by a recursive relation, and easily implemented.&lt;/p&gt;

&lt;p&gt;If the intermediate subproblems are not overlapping, then we have just a case of Divide and Conquer.&lt;/p&gt;

&lt;p&gt;Many dynamic programming problems involves a two dimensional array, also called a matrix or a table. An attempt to deduce a logical pattern in the content of the 2D array is made, before the code of few number of operations, is written from the pattern. &lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson
&lt;/h2&gt;

&lt;p&gt;The longest common subsequence (LCS) is defined as the longest subsequence that is common to all the given sequences, provided that the elements of the subsequence are not required to occupy consecutive positions within the original sequences. The order of the elements should also be maintained.&lt;/p&gt;

&lt;h2&gt;
  
  
  Illustration
&lt;/h2&gt;

&lt;p&gt;Subsequences of "abc" are "", "a", "b", "c", "ab", "ac", "bc" and "abc". In general, a string of length n has 2&lt;sup&gt;n&lt;/sup&gt; subsequences. In this case n is 3. Do not forget that "" of zero length is a subsequence. Note that in a sequence, some characters can be skipped, in the original string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Longest Common Subsequence Examples&lt;/strong&gt;&lt;br&gt;
str1 = "abc"; str2 = "acd"&lt;br&gt;
Output: 2&lt;br&gt;
Explanation: The longest subsequence (lcs) which is present in both strings is "ac".&lt;/p&gt;

&lt;p&gt;str1 = "aggtab"; str2 = "gxtxayb"&lt;br&gt;
Output: 4&lt;br&gt;
Explanation: The longest common subsequence (lcs) which is present in both strings is "gtab".&lt;/p&gt;

&lt;p&gt;str1 = "abc"; str2 = "cba"&lt;br&gt;
Output: 1 &lt;br&gt;
Explanation: There are three longest common subsequences of length 1, "a", "b" and "c".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Illustration&lt;/strong&gt;&lt;br&gt;
Consider the strings: "sandal" with M number of characters, equal to 6, and the string "bananas" with N number of characters equal to 7. Each of these strings is considered an array.&lt;/p&gt;

&lt;p&gt;The longest common subsequence of characters is "ana". For "sandal", 'd' is skipped.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Two Dimensional Dynamic Programming (dp) Array
&lt;/h2&gt;

&lt;p&gt;The dp array is:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 b    a    n    a    n    a    s
     i/j    0    1    2    3    4    5    6    7

     0      0    0    0    0    0    0    0    0
s    1      0    0    0    0    0    0    0    1
a    2      0    0    1    1    1    1    1    1
n    3      0    0    1    2    2    2    2    2
d    4      0    0    1    2    2    2    2    2
a    5      0    0    1    2    3    3    3    3
l    6      0    0    1    2    3    3    3    3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Explanation of the 2D dp Array&lt;/strong&gt;&lt;br&gt;
str1, "sandal" has the index , i from 0 to 6 inclusive, for a length of M+1. Index 0 refers to the empty string, "". Index 1 refers to the character, 's' as well as to the backward string "s". Index 2 refers to the character, 'a' as well as to the backward string "sa". Index 3 refers to the character, 'n' as well as to the backward string "san". Index 4 refers to the character, 'd' as well as to the backward string "sand". Index 5 refers to the character, 'a' as well as to the backward string "sanda". Index 6 refers to the character, 'l' as well as to the complete backward string "sandal". &lt;/p&gt;

&lt;p&gt;str2, "bananas" has the index , i from 0 to 7 inclusive, for a length of N+1. Index 0 refers to the empty string, "". Index 1 refers to the character, 'b' as well as to the backward string "b". Index 2 refers to the character, 'a' as well as to the backward string "ba". Index 3 refers to the character, 'n' as well as to the backward string "ban". Index 4 refers to the character, 'a' as well as to the backward string "bana". Index 5 refers to the character, 'n' as well as to the backward string "banan". Index 6 refers to the character, 'a' as well as to the backward string "banana". Index 7 refers to the character, 's' as well as to the complete backward string "bananas". &lt;/p&gt;

&lt;p&gt;Imagine that there is the string, "b a n a n a s" and the other string "s a n d a l" is sliding under it from left to right. In the following depiction, the common subsequence is the empty string, "":&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            "b a n a n a s"
"s a n d a l"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;as there is no overlapping between the two strings. At this point, the common subsequence string is "",of zero length. If the lower string is shifted as a whole, one place to the right, the overlapping characters would be 'b' and 'l', and that gives rise to still a zero length common subsequence empty string. If the lower string is then shifted as a whole, one place to the right, the overlapping characters would be "ba" and "al", and that gives rise to a common subsequence string of length 1, with common subsequence character, "a". If the lower string is then shifted as a whole, one place to the right, the overlapping characters would be "ban" and "dal", and that gives rise to still a common subsequence string of length 1, with the common subsequence character, still "a". If the lower string is then shifted as a whole, one place to the right, the overlapping characters would be "bana" and "ndal", and that gives rise to a new common subsequence string of length 2, with the common subsequence "na", and 'd' skipped for the lower string part, "ndal". If the lower string is then shifted as a whole, one place to the right, the overlapping characters would be "banan" and "andal", and that gives rise to a common subsequence string of length 3, with the common subsequence "ana", and 'd' skipped for the lower string part, "andal".  From this point, no matter how many steps the lower string is shifted to the right, the length of the common subsequence string will not increase, and their 3 characters will remain the same, in the same order. &lt;/p&gt;

&lt;p&gt;And so, the length of the longest common subsequence is 3. The heading for the columns is "'b'a'n'a'n'a's'", with each letter representing a column. Column zero has no header character. All the 7 cells of column zero, each has the value, zero, because there is no overlapping between the two given strings. &lt;/p&gt;

&lt;p&gt;In the table, there are 7 rows. The heading for the rows is "'s'a'n'd'a'l'", with each letter representing a row. Row zero has no header character. All the 8 cells of row zero, each has the value, zero, because there is no overlapping between the two given strings. &lt;/p&gt;

&lt;p&gt;The variable for the indexes for the rows is i, and the indexes are from 0 to 6. The variable for the indexes for the columns is j, and the indexes are from 0 to 7. Do not confuse between the first useful column of zeros and the row index column with indexes from 0 to 6, in the table. Also, do not confuse between the first useful row of zeros and the column index row with indexes from 0 to 7, in the table.&lt;/p&gt;

&lt;p&gt;For the first row of index 1 (i=1), the 's' alone of "sandal" is shifted one horizontal (j) index at a time from the left end of the row to the right end of the row. For each cell, the length of the common subsequence found is noted. This only occurs at the end of the last cell, with length 1. The overlapping same characters here are, the starting 's' in "sandal" and the last 's' of "bananas". At the end of the algorithm, this common 's' is not part of the longest common subsequence. However, the corresponding cell should have the value, 1. &lt;/p&gt;

&lt;p&gt;For the second row of index 2 (i=2), "sa" of "sandal" is shifted one horizontal (j) index at a time from the left end of the row to the right end of the row. For each cell, the length of the common subsequence found is noted. This is 1 for dp[2][2], dp[2][3], dp[2][4], dp[2][5], dp[2][6] and dp[2][7]. The preceding cell of dp[2][1] has 0. The 1's are for the letter 'a' in "sa" of "sandal" and "ba" of "bananas".&lt;/p&gt;

&lt;p&gt;For the third row (i=3), "san" of "sandal" is moved from the left end to the right end, one horizontal (j) index at a time. The value of 1 in cell dp[3][2] is for the 'a' in "san" of "sandal" and "ba" of "bananas". The rest of the cells in the row have value 2. The 2 is for "an" in "san" of "sandal" and "ban" of "bananas".&lt;/p&gt;

&lt;p&gt;The values for the rest of the row cells can be equally determined by inspection.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Code
&lt;/h2&gt;

&lt;p&gt;It is important to note that the indexes for the words, "sandal" and "bananas", with the indexes for  the dp 2D array, have a difference of 1. For example, the i index for 's' in "sandal" is 0, while the dp  i index for the same 's' in "sandal" is 1. The j index for 'b' in "bananas" is 0, while the dp j index for the same 'b' in "bananas" is 1. With that scheme, the complete table with the two index scales is:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               b(0) a(1) n(2) a(3) n(4) a(5) s(6)
     i/j    0    1    2    3    4    5    6    7 

     0      0    0    0    0    0    0    0    0
s(0) 1      0    0    0    0    0    0    0    1
a(1) 2      0    0    1    1    1    1    1    1
n(2) 3      0    0    1    2    2    2    2    2
d(3) 4      0    0    1    2    2    2    2    2
a(4) 5      0    0    1    2    3    3    3    3
l(5) 6      0    0    1    2    3    3    3    3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Looking at the columns: Index 3 for example, for second 'a' in "bananas" is dp index 4 - 1. So, to go from a column index to a character index in "bananas", subtract 1, for the same position. j is the variable for dp column indexes. &lt;/p&gt;

&lt;p&gt;Looking at the rows: Index 3 for example, for 'd' in "sandal" is dp index 4 - 1. So, to go from a row index to a character index in "sandal", subtract 1, for the same position. i is the variable for dp row indexes. &lt;/p&gt;
&lt;h2&gt;
  
  
  The Logical Pattern
&lt;/h2&gt;

&lt;p&gt;There are two rules to deduce here, which are alternatives to one another. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule 1&lt;/strong&gt;&lt;br&gt;
Let str1 identify "sandal" and str2 identify "bananas". If for a cell in the 2-dimensional array, the corresponding letters in the rows header ("sandal") and columns header ("bananas") are the same, then in order to obtain the number (answer) for that cell, go to the immediate left-top cell; get the number there and add 1 to it. This is true for the following cells:&lt;/p&gt;

&lt;p&gt;For  str1[0] = str2[6] = 's' , dp[1][7] = 1 =  dp[0][6] + 1 .&lt;br&gt;
For  str1[1] = str2[1] = 'a' , dp[2][2] = 1 =  dp[1][1] + 1 .&lt;br&gt;
For  str1[1] = str2[3] = 'a' , dp[2][4] = 1 =  dp[1][3] + 1 .&lt;br&gt;
For  str1[1] = str2[5] = 'a' , dp[2][6] = 1 =  dp[1][5] + 1 .&lt;br&gt;
For  str1[2] = str2[2] = 'n' , dp[3][3] = 2 =  dp[2][2] + 1 .&lt;br&gt;
For  str1[2] = str2[4] = 'n' , dp[3][5] = 2 =  dp[2][4] + 1 .&lt;br&gt;
For  str1[4] = str2[1] = 'a' , dp[5][2] = 1 =  dp[4][1] + 1 .&lt;br&gt;
For  str1[4] = str2[3] = 'a' , dp[5][4] = 3 =  dp[4][3] + 1 .&lt;br&gt;
For  str1[4] = str2[5] = 'a' , dp[5][6] = 3 =  dp[4][5] + 1 .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule 2&lt;/strong&gt;&lt;br&gt;
Rule 2 is the alternative to rule 1. If rule 1 does not hold (the corresponding letters are not the same), then the number (answer) for the cell in question, is the bigger of the two numbers of the immediate left cell and immediate top cell.  Note that if the numbers of these two (left and top) cells are the same, the bigger is still that same number.&lt;/p&gt;

&lt;p&gt;The ultimate answer for the question, is the number (value) in cell dp[M][N] .&lt;/p&gt;
&lt;h2&gt;
  
  
  The Program
&lt;/h2&gt;

&lt;p&gt;And so the code is (read the comments as well):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    #include &amp;lt;stdio.h&amp;gt;
    #include &amp;lt;math.h&amp;gt;    //for fmax()

    int  lcs(char * const str1, char * const str2, int M, int N) {    //M for str1 and N for str2
        int dp[M+1][N+1];    //M for rows and N for columns

for (int i=0; i&amp;lt;(M+1); i++)    //initialize first column
    dp[i][0] = 0;
for (int j=0; j&amp;lt;(N+1); j++)    //initialize first row
    dp[0][j] = 0;

        for (int i=1; i&amp;lt;(M+1); i++) {
            for (int j=1; j&amp;lt;(N+1); j++) {
                if (str1[i-1] == str2[j-1])
                    dp[i][j] = dp[i-1][j-1] + 1;
                else
                    dp[i][j] = fmax(dp[i-1][j], dp[i][j-1]);
            }
        }

        return dp[M][N]; 
    }

    int main() 
    {
        char * const str1 = "sandal";
        char * const str2 = "bananas";

        int ret = lcs(str1, str2, 6, 7);
        printf("%i\n", ret);

        return 0; 
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;br&gt;
    3&lt;br&gt;
Do not forget to compile the program with a command like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc temp.c -o temp.exe -lm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if Linux or its variant (such as Ubuntu) is used, where the switch -lm, ensures that the fmax() function is applicable.&lt;/p&gt;

&lt;p&gt;The time complexity for the solution is O(MxN). The space complexity is also given as O(MxN).&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>longest</category>
      <category>common</category>
      <category>subsequence</category>
      <category>twoarrays</category>
    </item>
    <item>
      <title>6. Find all positive numbers in the array that have their opposites in it as well</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 11:18:28 +0000</pubDate>
      <link>https://dev.to/chrys/6-find-all-positive-numbers-in-the-array-that-have-their-opposites-in-it-as-well-2a9k</link>
      <guid>https://dev.to/chrys/6-find-all-positive-numbers-in-the-array-that-have-their-opposites-in-it-as-well-2a9k</guid>
      <description>&lt;h2&gt;
  
  
  6. At toptal.com : Algorithm : A numeric array of length N is given. We need to design a function that finds all positive numbers in the array that have their opposites in it as well. Describe approaches for solving optimal worst case and optimal average case performance, respectively. Use C. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three approaches will be addressed in this article: approach for O(N&lt;sup&gt;2&lt;/sup&gt;) time, approach for O(N log2 N) time, and approach for O(N) time. Only the approach for O(N.log2N) time will be described in detail.&lt;/p&gt;

&lt;p&gt;Also not that absolute value means the value without sign, which is just the positive value.&lt;/p&gt;

&lt;h2&gt;
  
  
  O(N&lt;sup&gt;2&lt;/sup&gt;) Solution
&lt;/h2&gt;

&lt;p&gt;With this solution, for each positive number, it is verified (compared) if any other number in the array is the opposite. The number of opposite pairs gives the answer. This solution has O(N&lt;sup&gt;2&lt;/sup&gt;) time complexity. Any good computer programmer can code this solution, so it will not be addressed any further, in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  O(N.log2N) Solution
&lt;/h2&gt;

&lt;p&gt;Consider the example list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-7, 4, -3, 2, 2, -8, -2, 3, 3, 7, -2, 3, -2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;By inspection, though inspecting is rather time consuming, the answer is [2, 2, 3, 7]. The subset (-2, -2, 2, 2) produces 2 pairs; the subset (-3 3) produces 1 pair; and the subset (-7 7) produces the last pair, making 4 pairs altogether. The set (-2, -2, 2, 2) has the pairs (-2, 2) and (-2, 2) which is same pair occurring twice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Need for Special Custom Sorting&lt;/strong&gt;&lt;br&gt;
The special custom sorting is as follows:&lt;/p&gt;

&lt;p&gt;If the numbers are not equal or opposite, they are sorted by their absolute value; but if they are (else), they are sorted by their sign. This leads to the following special custom sort (arrangement):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-2, -2, -2, 2, 2, -3, 3, 3, 4, -7, 7, -8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The 4 pairs required can be seen or inferred.&lt;/p&gt;

&lt;p&gt;This is not ordinary sort ascending or ordinary sort descending. A compare function is needed for this special custom sorting. The sort function will use the compare function. The code for this compare function is given along with the principal code below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Principal Code&lt;/strong&gt;&lt;br&gt;
There is an issue with this problem and its solution at toptal.com/algorithms/interview-questions. At toptal.com/algorithms/interview-questions, the answer is [2 3 7 ] instead of [2, 2, 3, 7] . This means that the problem actually requires unique positive numbers and not just positive numbers with opposites. The principal code below produces unique positive numbers.&lt;/p&gt;
&lt;h2&gt;
  
  
  Complete Program
&lt;/h2&gt;

&lt;p&gt;The complete program is (read the comments as well):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    #include &amp;lt;stdio.h&amp;gt;
    #include &amp;lt;stdlib.h&amp;gt;     //for abs() and qsort() functions

    //compare function
    int compareFn(const void *a, const void *b) {
        int int_a = *((int *)a);
        int int_b = *((int *)b);
        if (int_a != int_b &amp;amp;&amp;amp; int_a != -int_b)
        return abs(int_a) &amp;gt; abs(int_b);    //from stdlib.h library
    else
            return int_a &amp;gt; int_b;
    }

    //principal code
    void find_numbers_with_opposites(int A[], int N) {
        qsort(A, N, sizeof(int), compareFn);    //from stdlib.h library
        for (int i=1; i&amp;lt;N; i++) {
        if (A[i] &amp;gt; 0 &amp;amp;&amp;amp; A[i - 1] == -A[i])
                printf("%i ", A[i]);
        }
    }

    int main(int argc, char **argv)
    {
        int A[] = {-7, 4, -3, 2, 2, -8, -2, 3, 3, 7, -2, 3, -2};
        int N = sizeof(A) / sizeof(A[0]);    //no. of elements in A
        find_numbers_with_opposites(A, N);
        printf("\n");

        return 0;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This implementation has an optimal average case runtime complexity of O(N.log2N), with the sorting algorithm being the bottleneck.&lt;/p&gt;

&lt;h2&gt;
  
  
  O(N) Solution
&lt;/h2&gt;

&lt;p&gt;This solution needs a hash function. The C language does not have a hash function. The C++ language has a hash function. So only the pseudo code using a hash function is provided below, so that anyone who has studied C++ can implement it. The pseudo code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FUNCTION find_numbers_with_opposites(numbers)
    table = HashTable&amp;lt;number, number&amp;gt;
    answer = List
    FOR number IN numbers
        IF number == 0
            CONTINUE
        END IF
        key = abs(number)
        IF key not in table
            table[key] = number
        ELSE IF table[key] = -number
            answer.push(key)
            table[key] = 0
        END IF
    END FOR
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>positive</category>
      <category>numbers</category>
      <category>array</category>
      <category>opposites</category>
    </item>
    <item>
      <title>4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 10:42:19 +0000</pubDate>
      <link>https://dev.to/chrys/4-advantages-best-average-and-worst-case-solutions-of-some-sort-26lc</link>
      <guid>https://dev.to/chrys/4-advantages-best-average-and-worst-case-solutions-of-some-sort-26lc</guid>
      <description>&lt;h2&gt;
  
  
  4. At toptal-com : Algorithm : What are the key advantages of Insertion Sort, Quicksort, Heapsort and Mergesort? Discuss best, average, and worst case time and memory complexity. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Memory complexity means space complexity.&lt;/p&gt;

&lt;p&gt;n^2 means n&lt;sup&gt;2&lt;/sup&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Insertion Sort&lt;/strong&gt;&lt;br&gt;
Insertion Sort has an average and worst runtime of O(n^2), and a best runtime of O(n). It doesn’t need any extra buffer, so space complexity is O(n). It is efficient at sorting extremely short arrays due to a very low constant factor in its complexity. It is also extremely good at sorting arrays that are already “almost” sorted. A common use is for re-sorting arrays that have had some small updates to their elements.&lt;/p&gt;

&lt;p&gt;The other three algorithms have a best and average runtime complexity of O(n log n). Heapsort and Mergesort maintain that complexity even in worst case scenarios, while Quicksort has worst case performance of O(n^2).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quicksort&lt;/strong&gt;&lt;br&gt;
Quicksort is sensitive to the data provided. Without usage of random pivots, it uses O(n^2) time for sorting a full sorted array. But by swapping random unsorted elements with the first element, and sorting afterwards, the algorithm becomes less sensitive to data that would otherwise cause worst-case behavior. Though it doesn’t have lower complexity than Heapsort or Merge sort, it has a very low constant factor to its execution speed, which generally gives it a speed advantage when working with lots of random data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Heapsort&lt;/strong&gt;&lt;br&gt;
Heapsort has reliable time complexity and doesn’t require any extra buffer space. As a result, it is useful in software that requires reliable speed over optimal average runtime, and/or has limited memory to operate with the data. Thus, systems with real time requirements and memory constraints benefit the most from this algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Merge Sort&lt;/strong&gt;&lt;br&gt;
Merge sort has a much smaller constant factor than Heapsort, but requires O(n) buffer space to store intermediate data, which is very expensive. Its main selling point is that it is stable, as compared to Heapsort which isn’t. In addition, its implementation is very parallelizable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Equivalent Articles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-CS.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms.  Algorithm, Question, Explanation, Solution, at toptal.com . Use C# (C-Sharp) if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-Java.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use Java if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-Cpp.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use C++ if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-PY.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use Python if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-JavaScript.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use JavaScript if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-Go.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use Go if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Advantages,-Time-and-Memory-Complexity-Solutions-of-Some-Sort-Algorithms-at-toptal-com-algorithms-interview-questions-in-PHP.htm" rel="noopener noreferrer"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms. Algorithm, Question, Explanation, Solution, at toptal.com . Use PHP if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Article in C: &lt;a href="https://dev.to/chrys/6-find-all-positive-numbers-in-the-array-that-have-their-opposites-in-it-as-well-2a9k"&gt;6. Find all positive numbers in the array that have their opposites in it as well&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>advantages</category>
      <category>best</category>
      <category>average</category>
      <category>worstcase</category>
    </item>
    <item>
      <title>3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work?</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 10:28:14 +0000</pubDate>
      <link>https://dev.to/chrys/3-how-do-insertion-sort-heapsort-quicksort-and-merge-sort-work-1blp</link>
      <guid>https://dev.to/chrys/3-how-do-insertion-sort-heapsort-quicksort-and-merge-sort-work-1blp</guid>
      <description>&lt;h2&gt;
  
  
  At toptal.com, Algorithm, Use C if necessary. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Insertion Sort&lt;/strong&gt;&lt;br&gt;
Insertion sort takes elements of the array sequentially, and maintains a sorted subarray to the left of the current point. It does this by taking an element, finding its correct position in the sorted array, and shifting all following elements by 1, leaving a space for the element to be inserted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Heapsort&lt;/strong&gt;&lt;br&gt;
Heapsort builds a binary heap. A binary max heap is a nearly complete binary tree in which each parent node is larger or equal to its children. The heap is formed by sifting elements up in the given binary tree (given array). The heap is stored in the same memory in which the original array elements were. Once the heap is formed, that is binary max heap. The opposite is binary min heap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quicksort&lt;/strong&gt;&lt;br&gt;
The Quick Sort algorithm is notable for its approach to sorting an array. Quick Sort begins by selecting a pivot from the provided list, then separates the remaining elements into two groups - those less than the pivot and those greater than it, keeping their order in the initial array. This process is replicated recursively until the entire list is sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Merge Sort&lt;/strong&gt;&lt;br&gt;
Merge Sort recursively halves the given array. Once the subarrays reach trivial length, merging begins. Merging takes the smallest element between two adjacent subarrays and repeats that step until all elements are taken, resulting in a sorted subarray. The process is repeated on pairs of adjacent subarrays until we arrive at the starting array, but sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-CS.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use C# (C-Sharp) if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-Java.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use Java if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-Cpp.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use C++ if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-PY.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use Python if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-JavaScript.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use JavaScript if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-Go.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use Go if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Summary-of-Insertion-sort,-Heapsort,-Quicksort,-and-Merge-Sort-at-toptal-com-algorithms-interview-questions-in-PHP.htm" rel="noopener noreferrer"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work? Algorithm, Question, Explanation, Solution, at toptal.com . Use PHP if necessary (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Article in C: &lt;a href="https://dev.to/chrys/4-advantages-best-average-and-worst-case-solutions-of-some-sort-26lc"&gt;4. Advantages, Best, Average, and Worst Case Solutions of Some Sort Algorithms-at-toptal-com-algorithms&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>insertion</category>
      <category>heapsort</category>
      <category>quicksort</category>
      <category>mergesort</category>
    </item>
    <item>
      <title>2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in C</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 08:32:17 +0000</pubDate>
      <link>https://dev.to/chrys/2-optimal-calculation-for-pk-where-k-is-a-non-negative-integer-at-toptal-com-3n48</link>
      <guid>https://dev.to/chrys/2-optimal-calculation-for-pk-where-k-is-a-non-negative-integer-at-toptal-com-3n48</guid>
      <description>&lt;h2&gt;
  
  
  2. At toptal.com : Algorithm : How would you optimally calculate p^k, where k is a non-negative integer? What is the complexity of the solution? Use C. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;p^k means p&lt;sup&gt;k&lt;/sup&gt; .&lt;/p&gt;

&lt;p&gt;(p&lt;sup&gt;a&lt;/sup&gt;)&lt;sup&gt;b&lt;/sup&gt; = p&lt;sup&gt;(a*b)&lt;/sup&gt; = p&lt;sup&gt;k&lt;/sup&gt; , where k = a*b and * means, times.&lt;/p&gt;

&lt;p&gt;P&lt;sup&gt;x&lt;/sup&gt; x p&lt;sup&gt;y&lt;/sup&gt; = p&lt;sup&gt;(x+y)&lt;/sup&gt; = p&lt;sup&gt;k&lt;/sup&gt; , where here, k = x+y.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lesson
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Even Number of the Same Base&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;The index, 8 is an even number. There are 7 operations (7 steps) in this mathematical statement (extreme left expression).&lt;/p&gt;

&lt;p&gt;Now,&lt;/p&gt;

&lt;p&gt;3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;=&amp;gt; (3 x 3) x (3 x 3) x (3 x 3) x (3 x 3) = 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; = A x A x A x A = A&lt;sup&gt;4&lt;/sup&gt; = (3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;4&lt;/sup&gt; = 3&lt;sup&gt;8&lt;/sup&gt; = 6561, where A = 3 x 3 = 3&lt;sup&gt;2&lt;/sup&gt; .&lt;/p&gt;

&lt;p&gt;But 4 = 2 x 2&lt;/p&gt;

&lt;p&gt;∴ 3&lt;sup&gt;8&lt;/sup&gt; = (3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;4&lt;/sup&gt; = ((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;i.e. 3&lt;sup&gt;8&lt;/sup&gt; = ((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = (3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;4&lt;/sup&gt; = 3&lt;sup&gt;2x4&lt;/sup&gt; with a = 2 and b = 4. Should be programmed recursively, in&lt;/p&gt;

&lt;p&gt;((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = ((9)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = (81)&lt;sup&gt;2&lt;/sup&gt; = 6561 .&lt;/p&gt;

&lt;p&gt;There are three operations here, which are the powers: x2x2x2 . So, 7 operations (approximately 8 operations) have been reduced to 3 operations.&lt;/p&gt;

&lt;p&gt;So, by dividing 8 (k) by 2 to give 4 and then by 2 to give 2, all the power indexes are obtained, which are the prime number, 2, and for 3 operations.&lt;/p&gt;

&lt;p&gt;Here, k = 8.&lt;/p&gt;

&lt;p&gt;It is done recursively as follows:&lt;/p&gt;

&lt;p&gt;3 x 3 = 9&lt;/p&gt;

&lt;p&gt;9 x 9 = 81&lt;/p&gt;

&lt;p&gt;81 x 81 = 6561 (three operations, i.e. three steps)&lt;/p&gt;

&lt;p&gt;This is because,&lt;/p&gt;

&lt;p&gt;3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;=&amp;gt; (3 x 3) x (3 x 3) x (3 x 3) x (3 x 3) = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;=&amp;gt;      9    x      9     x      9     x     9     = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;=&amp;gt;     (9     x    9)    x      (9    x    9)     = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;=&amp;gt;            81           x            81           = 3&lt;sup&gt;8&lt;/sup&gt; = 6561&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Odd Number of the Same Base&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 = 3&lt;sup&gt;9&lt;/sup&gt; = 19683&lt;/p&gt;

&lt;p&gt;The index, 9 is an odd number. There are 8 operations (8 steps) in this mathematical statement (extreme left expression).&lt;/p&gt;

&lt;p&gt;Now,&lt;/p&gt;

&lt;p&gt;3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 = 3&lt;sup&gt;9&lt;/sup&gt; = 19683&lt;/p&gt;

&lt;p&gt;=&amp;gt; 3 x (3 x 3) x (3 x 3) x (3 x 3) x (3 x 3) = 3 x 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; = 3 x A x A x A x A = 3 x A&lt;sup&gt;4&lt;/sup&gt; = 3A&lt;sup&gt;4&lt;/sup&gt; = 3(3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;4&lt;/sup&gt; = 3 x 3&lt;sup&gt;8&lt;/sup&gt; = 19683, where A = 3 x 3 = 3&lt;sup&gt;2&lt;/sup&gt; .&lt;/p&gt;

&lt;p&gt;But 4 = 2 x 2&lt;/p&gt;

&lt;p&gt;∴ 3&lt;sup&gt;9&lt;/sup&gt; = 3(3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;4&lt;/sup&gt; = 3((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3 x ((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3 x 3&lt;sup&gt;8&lt;/sup&gt; = 3&lt;sup&gt;1&lt;/sup&gt; x 3&lt;sup&gt;8&lt;/sup&gt; = 3&lt;sup&gt;1&lt;/sup&gt; x 3&lt;sup&gt;9-1&lt;/sup&gt; = 19683&lt;/p&gt;

&lt;p&gt;i.e. 3&lt;sup&gt;9&lt;/sup&gt; = 3 x ((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3&lt;sup&gt;1&lt;/sup&gt; x 3&lt;sup&gt;8&lt;/sup&gt; = 3&lt;sup&gt;1&lt;/sup&gt; x 3&lt;sup&gt;9-1&lt;/sup&gt;  with x = 1 and y = 9-1. Should be programmed recursively, in&lt;/p&gt;

&lt;p&gt;3 x ((3&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3 x ((9)&lt;sup&gt;2&lt;/sup&gt;)&lt;sup&gt;2&lt;/sup&gt; = 3 x (81)&lt;sup&gt;2&lt;/sup&gt; = 19683 .&lt;/p&gt;

&lt;p&gt;There are three main operations here, which are 3x and the powers: 2x2x2 . So, 8 operations have been reduced to 3 operations.&lt;/p&gt;

&lt;p&gt;Do not forget that, here, k = 9 here.&lt;/p&gt;
&lt;h2&gt;
  
  
  Remarks
&lt;/h2&gt;

&lt;p&gt;For an even value of k, choosing a = 2 and b = k/2, thus having p^k = (p^2)^(k/2), will reduce the number of required multiplications considerably. For an odd value of k, choosing x = 1 and y=k-1 will result in y being even, and the same process as for the even case, can then simply be repeated. This allows the definition of a recursive function.&lt;/p&gt;
&lt;h2&gt;
  
  
  Time complexity
&lt;/h2&gt;

&lt;p&gt;The time complexity for this scheme is O(log2k), as opposed to the brute-force scheme of O(k).&lt;/p&gt;
&lt;h2&gt;
  
  
  Code in C
&lt;/h2&gt;

&lt;p&gt;The program in C uses a recursive function. The program is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #include &amp;lt;stdio.h&amp;gt;

    int powe(int base, unsigned int exponent) {
        if (exponent == 0)
            return 1;
        else if (exponent % 2 == 0)    //exponent is even
            return powe(base * base, exponent / 2);
        else    //exponent is odd
            return base * powe(base * base, (exponent - 1) / 2);
    }

    int main(int argc, char **argv)
    {
        int k = 8;
        int ret1 = powe(3, k);
        printf("%i\n", ret1);

        int k2 = 9;
        int ret2 = powe(3, k2);
        printf("%i\n", ret2);

        return 0; 
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6561

19683
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and correct. For the first function call, the base is 3 and k is 8. For the second function call, the base is still 3 and k is 9.&lt;/p&gt;

&lt;p&gt;Note that the C predefined function, pow() has not been used. The core of the code is (base * base, exponent / 2) for even k and (base * base, (exponent - 1) / 2) for odd k, either of which has just one multiplication.&lt;/p&gt;

&lt;p&gt;This one multiplication is done three times as follows: 3 x 3 = 9; 9 x 9 = 81; 81 x 81 = 6561.&lt;/p&gt;

&lt;p&gt;For the first multiplication, the exponent, 8 is divided by 2 to give an integer division quotient of 4. For the second multiplication, the new exponent, 4 is divided by 2 to give an integer division quotient of 2. For the third multiplication, the new exponent, 2 is divided by 2 to give an integer division quotient of 1. A fourth division attempted would result in the new exponent, 1 being divided by 2 to give an integer division quotient (whole number) of 0; ending the recursion at the first function body statement. Remember that p&lt;sup&gt;0&lt;/sup&gt; is 1.&lt;/p&gt;

&lt;p&gt;So, the dividing of the exponent is checking the number of times the division has to be done.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-CS.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in C# (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-Java.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in Java (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-Cpp.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in C++ (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-PY.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in Python (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-JavaScript.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in JavaScript (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-Go.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in Go (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Optimal-Calculation-for-pk,-where-k-is-a-non-negative-integer,-at-toptal-com-algorithms-interview-questions-in-PHP.htm" rel="noopener noreferrer"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in PHP (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Article in C: &lt;a href="https://dev.to/chrys/3-how-do-insertion-sort-heapsort-quicksort-and-merge-sort-work-1blp"&gt;3. How do Insertion sort, Heapsort, Quicksort, and Merge sort work?&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>optimal</category>
      <category>calculation</category>
      <category>pk</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>1.What are Divide and Conquer algorithms?</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 10 Nov 2025 02:49:56 +0000</pubDate>
      <link>https://dev.to/chrys/1what-are-divide-and-conquer-algorithms-djp</link>
      <guid>https://dev.to/chrys/1what-are-divide-and-conquer-algorithms-djp</guid>
      <description>&lt;h2&gt;
  
  
  1. At toptal.com : Algorithm : What are Divide and Conquer algorithms? Describe how they work. Can you give any common examples of the types of problems where this approach might be used? Use C if necessary. Question, Explanation, Solution.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
Divide and Conquer algorithms are a paradigm for solving problems that involve several basic steps. First, we divide the problem into smaller pieces and work to solve each of them independently. That is, divide given list into smaller parts, and work to solve each of them independently. Once we’ve solved all of the pieces, we take all of the resulting smaller solutions and combine them into a single integrated comprehensive solution. This second stage is the conquering stage.&lt;/p&gt;

&lt;p&gt;This process can be performed recursively; that is, each "sub problem" can itself be subdivided into even smaller parts if necessary. This recursive division of the problem is performed until each individual problem is small enough to become relatively trivial to solve.&lt;/p&gt;

&lt;p&gt;Some common examples of problems that lend themselves well to this approach are binary search, sorting algorithms (e.g., Merge Sort, Quicksort), optimization of computationally complex mathematical operations (Exponentiation, FFT, Strassen’s algorithm), and others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-CS.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in C# (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-Java.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in Java (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-Cpp.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in C++ (equivalent article)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-PY.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in Python (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-JavaScript.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in JavaScript (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-Go.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in Go (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click:  &lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/Divide-and-Conquer-Algorithms-Description-and-Examples-at-toptal-com-algorithms-interview-questions-in-PHP.htm" rel="noopener noreferrer"&gt;1. What are Divide and Conquer Algorithms? – in PHP (equivalent article)&lt;/a&gt;&lt;/strong&gt; &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Article in C: &lt;a href="https://dev.to/chrys/2-optimal-calculation-for-pk-where-k-is-a-non-negative-integer-at-toptal-com-3n48"&gt;2. Optimal Calculation for pk, where k is a Non Negative Integer, at toptal-com algorithms-interview in C&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>divide</category>
      <category>conquer</category>
      <category>algorithms</category>
      <category>summary</category>
    </item>
    <item>
      <title>OddOccurrencesInArray at app.codility.com/programmers in C Explained: Find value that occurs in odd number of elements.</title>
      <dc:creator>Chrysanthus</dc:creator>
      <pubDate>Mon, 22 Sep 2025 00:29:50 +0000</pubDate>
      <link>https://dev.to/chrys/oddoccurrencesinarray-at-appcodilitycomprogrammers-in-c-explained-find-value-that-occurs-in-odd-3ghl</link>
      <guid>https://dev.to/chrys/oddoccurrencesinarray-at-appcodilitycomprogrammers-in-c-explained-find-value-that-occurs-in-odd-3ghl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Task score : 100% - Correctness : 100% ; Performance : 100%&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Detected time complexity : O(N) or O(N*log(N))&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Lesson 2 at app.codility.com/programmers&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Task, solution and its explanation are given below.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Category of Article : Technology | Computers and Software | Algorithm&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Task
&lt;/h2&gt;

&lt;p&gt;A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.&lt;/p&gt;

&lt;p&gt;For example, in array A such that:&lt;/p&gt;

&lt;p&gt;A[0] = 9  A[1] = 3  A[2] = 9&lt;br&gt;
  A[3] = 3  A[4] = 9  A[5] = 7&lt;br&gt;
  A[6] = 9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    the elements at indexes 0 and 2 have value 9,
    the elements at indexes 1 and 3 have value 3,
    the elements at indexes 4 and 6 have value 9,
    the element at index 5 has value 7 and is unpaired.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Write a function:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int solution(int A[], int N);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.&lt;/p&gt;

&lt;p&gt;For example, given array A such that:&lt;/p&gt;

&lt;p&gt;A[0] = 9  A[1] = 3  A[2] = 9&lt;br&gt;
  A[3] = 3  A[4] = 9  A[5] = 7&lt;br&gt;
  A[6] = 9&lt;/p&gt;

&lt;p&gt;the function should return 7, as explained in the example above.&lt;/p&gt;

&lt;p&gt;Write an efficient algorithm for the following assumptions:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    N is an odd integer within the range [1..1,000,000];
    each element of array A is an integer within the range [1..1,000,000,000];
    all but one of the values in A occur an even number of times.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
The above list has been arranged as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Element:    9   3   9   3   9   7   9
Index :     0   1   2   3   4   5   6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The paired elements are 9 and 3. This can be deceiving. An algorithm code written, based on this setup, would not work for other setups, for the same criteria and conditions of the task (problem). The paired elements could have been 9, 3, and 8; as in the following arrangement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Element:    9   3   9   3   8   7   8
Index :     0   1   2   3   4   5   6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can still be deceiving because, for each pair, the second element occurs after one element. That is: for the pair of 9, the second 9 occurs after 3; for the pair of 3, the second 3 occurs after 9; for the pair of 8, the second pair occurs after 7. An algorithm code written, based on this arrangement, would not work for other arrangements, for the same criteria and conditions, of the task (problem). The arrangement could otherwise have been in still a slightly more deceiving form as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Element:    9   9   3   3   8   8   7
Index :     0   1   2   3   4   5   6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are still other deceiving arrangements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Efficient Algorithm
&lt;/h2&gt;

&lt;p&gt;The last three line conditions in the given problem above, are not really to be verified, by the code of the student (candidate). It should be assumed that they are implied in the given data.&lt;/p&gt;

&lt;p&gt;It can be unnecessarily difficult to write the function in a brute force way. A fast (efficient) algorithm is to sort the vector (array), and then find the element that does not have a pair (adjacent); in a final one scan of the vector. That element would be in an odd number position. The program is (read the code and comments):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int compFn(const void *a, const void *b) {
    int int_a = *((int *)a);
    int int_b = *((int *)b);
    return int_a - int_b;
}

int solution(int A[], int N) {
    if (N == 1)
        return A[0];
    else { 
        qsort(A, N, sizeof(int), compFn);
        for (int i=0; i&amp;lt;N-2; i+=2) {
            if (A[i] != A[i+1])
                return A[i];  //0 is odd for zero based indexing
        }
    }
    return A[N-1];  
}

int main() 
{ 
    int B[] = {9, 3, 9, 3, 9, 7, 9};
    int N = sizeof(B) / sizeof(B[0]);    //divide size of array by size of one (first) element

    int odd = solution(B, N);

    printf("%i\n", odd);

    return 0; 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: the qsort() function is from the stdlib (sub) library.&lt;/p&gt;

&lt;p&gt;Note: with zero based indexing, index 0 is odd, index 2 is odd, index 4 is odd, etc.&lt;/p&gt;

&lt;p&gt;The qsort function is a predefined function in the C standard library (actually the stdlib sub library). It needs a compare function (compFn() in this case) that compares two numbers. The compare function, receives as arguments, two memory addresses, 'a' and b. In the compare function, (int *)a for example, means, put the address as value into a pointer object (variable). *((int *)a) means, return the value pointed to, by the pointer object (int *)a.&lt;/p&gt;

&lt;p&gt;What matters with the compare function is, that a positive value is returned, or zero is returned or a negative value is returned.&lt;/p&gt;

&lt;p&gt;It is the predefined qsort() function that calls the user defined compFn() function, by sending it two references (addresses); and the programmer (developer) should not border about the definition of the references. The last argument of the qsort function call, is the user defined compare function, compFn without the parentheses.&lt;/p&gt;

&lt;p&gt;The output is 7, as expected. Read through the code if that is not already done.&lt;/p&gt;

&lt;p&gt;Remember, at codility.com/programmers, the C main function is not typed (sent). The solution code begins with the determination of the number of elements in the vector. There after, if the array contains only one element, then the one element is not paired, and it is returned.&lt;/p&gt;

&lt;p&gt;Otherwise, the list is sorted, and scanned from the beginning. After sorting, as the list is scanned, paired elements occur next to one another. And so there has to be a for-loop that increments by 2 (i+=2) and not 1. The unpaired element will be found in an odd position. If the last element is the unpaired element, it will be returned by the second return-statement.&lt;/p&gt;

&lt;p&gt;For the given array&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int B[] = {9, 3, 9, 3, 9, 7, 9};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;after sorting, the array is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int B[] = {3, 3, 7, 9, 9, 9, 9};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The element that is not paired is 7, and it is in the third position, which is zero-based index 2 (ordinal position - 1 = zero based index). With zero-based indexing, 0 is in an odd position, which is the first ordinal position; and one is in an even position. The reader should not confuse between zero-based index positioning and normal cardinal positioning of: 1, 2, 3, 4, 5. etc. With normal cardinal positioning, 2 is in an even position, while with zero-based index positioning, 2 is in an odd position (actually third ordinal position).&lt;/p&gt;

&lt;p&gt;Note that the while-condition in the for-loop is (i&amp;lt;N-2).&lt;/p&gt;

&lt;p&gt;If the unpaired value is at the end of the list, it is still returned, because in the body of the for-loop, A[i] is compared with A[i+1] , and zero-based indexing ends at N-1.&lt;/p&gt;

&lt;p&gt;The reader should register at app.codility.com/programmers to be testing his/her own code.&lt;/p&gt;

&lt;p&gt;That is it, for the explanation of the task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For C++ click: ﻿&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/OddOccurrencesInArray-at-app.codility.com-programmers-in-C-Plusplus-Explained---Find-value-that-occurs-in-odd-number-of-elements.htm" rel="noopener noreferrer"&gt;OddOccurrencesInArray at app.codility.com/programmers in C++ Explained: Find value that occurs in odd number of elements.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For JavaScript click: ﻿&lt;a href="https://www.broad-network.com/Software/articles/ChrysanthusForcha/dir0/OddOccurrencesInArray-at-app.codility.com-programmers-in-JavaScript-Explained---Find-value-that-occurs-in-odd-number-of-elements.htm" rel="noopener noreferrer"&gt;OddOccurrencesInArray at app.codility.com/programmers in JavaScript Explained: Find value that occurs in odd number of elements.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next article in the series: ﻿&lt;a href=""&gt;Coming Soon !&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>oddoccurrencesinarray</category>
      <category>codility</category>
      <category>c</category>
      <category>odd</category>
    </item>
  </channel>
</rss>
