<?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: Abhishek Chandra</title>
    <description>The latest articles on DEV Community by Abhishek Chandra (@abhishekchandra2522k).</description>
    <link>https://dev.to/abhishekchandra2522k</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F506334%2F18e7d3ed-0785-43c5-923e-66b6e049cfde.png</url>
      <title>DEV Community: Abhishek Chandra</title>
      <link>https://dev.to/abhishekchandra2522k</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhishekchandra2522k"/>
    <language>en</language>
    <item>
      <title>Asymptotic Notations</title>
      <dc:creator>Abhishek Chandra</dc:creator>
      <pubDate>Fri, 09 Jul 2021 16:24:37 +0000</pubDate>
      <link>https://dev.to/abhishekchandra2522k/asymptotic-notations-14nn</link>
      <guid>https://dev.to/abhishekchandra2522k/asymptotic-notations-14nn</guid>
      <description>&lt;p&gt;The objective of this article is to explain Analysis of Algorithms and Asymptotic Notations i.e. &lt;strong&gt;Big-O, Omega-Ω, and Theta-Θ.&lt;/strong&gt; These topics are the most basic foundation for Data Structures and Algorithms. &lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis of Algorithms
&lt;/h2&gt;

&lt;p&gt;The idea of &lt;em&gt;analysis of algorithms&lt;/em&gt; is to compare algorithms mainly in terms of running time and space consumed. &lt;br&gt; For example,&lt;br&gt;
To go from city 'M' to city 'Z', there can be many ways to carry-out this task: by &lt;em&gt;flight&lt;/em&gt;, by &lt;em&gt;bus&lt;/em&gt;, by &lt;em&gt;train&lt;/em&gt; and also by &lt;em&gt;bicycle&lt;/em&gt;. Depending on the accessibility and convenience, we choose the one that suits us. Similarly, in computer science we have various algorithms available for solving a same problem, for example we can solve sorting problem by many algorithms like &lt;code&gt;selection-sort&lt;/code&gt;, &lt;code&gt;bubble-sort&lt;/code&gt;, &lt;code&gt;merge-sort&lt;/code&gt;  and &lt;em&gt;many more&lt;/em&gt; but we will choose the one with lesser complexity. (Best algorithm for sorting is &lt;code&gt;quick-sort&lt;/code&gt; with complexity &lt;code&gt;O(nlog(n))&lt;/code&gt; ) &lt;br&gt;&lt;br&gt;
This concludes that Analysis of Algorithm should be used in determining which method is more efficient and also which method is good in terms of time and space consumed.&lt;/p&gt;

&lt;p&gt;Prior to learning Asymptotic Notations, Let's see what is &lt;strong&gt;&lt;em&gt;rate of growth&lt;/em&gt;&lt;/strong&gt; of an algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate of growth&lt;/strong&gt; is nothing but the rate at which the run time complexity of the algorithm increases as a function of the input.&lt;br&gt;&lt;br&gt;
Let's suppose we have to purchase a laptop and a mouse. If someone asks you what are you purchasing, then in general you will say buying a laptop and ignore buying the mouse part. This is because, cost of laptop is too big compared to cost of mouse. So, we can say&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Total Cost = cost_of_laptop + cost_of_mouse&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Total Cost ≈ cost_of_laptop (approximately)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the above mentioned example, we can represent the cost of laptop and cost of mouse as a function and for a given function, we can ignore the lower order terms (that are relatively insignificant for large value of input size, n). Let us consider another example in terms of algebra, let n&lt;sup&gt;4&lt;/sup&gt;, n&lt;sup&gt;2&lt;/sup&gt;, 100n and 5 are individual costs of some function, here we can approximate this function to n&lt;sup&gt;4&lt;/sup&gt; i.e. the highest rate of growth.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;n&lt;sup&gt;4&lt;/sup&gt; + n&lt;sup&gt;2&lt;/sup&gt; + 100n + 5 ≈ n&lt;sup&gt;4&lt;/sup&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here's the list of most common rate of growths.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Constant&lt;/td&gt;
&lt;td&gt;accessing an array element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;log(n)&lt;/td&gt;
&lt;td&gt;Logarithmic&lt;/td&gt;
&lt;td&gt;finding an element in a &lt;em&gt;sorted&lt;/em&gt; array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;n&lt;/td&gt;
&lt;td&gt;Linear&lt;/td&gt;
&lt;td&gt;finding an element in a &lt;em&gt;unsorted&lt;/em&gt; array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;nlog(n)&lt;/td&gt;
&lt;td&gt;Linear Logarithmic&lt;/td&gt;
&lt;td&gt;sorting n items with &lt;em&gt;divide-and-conquer&lt;/em&gt; (Merge Sort)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;n&lt;sup&gt;2&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Quadratic&lt;/td&gt;
&lt;td&gt;shortest path between two nodes in a graph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;n&lt;sup&gt;3&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Cubic&lt;/td&gt;
&lt;td&gt;matrix multiplication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;n&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Exponential&lt;/td&gt;
&lt;td&gt;tower of hanoi problem&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before going to Asymptotic Notations, We also need to know about Types Of Analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Analysis
&lt;/h3&gt;

&lt;p&gt;If we have to analyse an algorithm, we need to know on what inputs the algorithm takes less time, and on what inputs the algorithm is taking more time. That means we can represent algorithms with multiple expressions: one for the case where it takes &lt;em&gt;less time&lt;/em&gt; and other for the case where it takes &lt;em&gt;more time&lt;/em&gt;. &lt;br&gt;&lt;br&gt;
In general, when the algorithm takes less time it is called as the &lt;strong&gt;&lt;em&gt;best case&lt;/em&gt;&lt;/strong&gt; and when the algorithm takes more time than it is called as the &lt;strong&gt;&lt;em&gt;worst case&lt;/em&gt;&lt;/strong&gt;. &lt;br&gt;
To analyse an algorithm we need some kind of syntax and that forms the base for asymptotic analysis / notation. &lt;br&gt;
There are three types of analysis:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst Case&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;defines the input for which algorithm takes longest time to execute.&lt;/li&gt;
&lt;li&gt;algorithm runs slower.&lt;/li&gt;
&lt;li&gt;algorithm which executes maximum number of steps on input data of size n.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;defines the input for which algorithm takes lowest time to execute.&lt;/li&gt;
&lt;li&gt;algorithm runs fastest.&lt;/li&gt;
&lt;li&gt;algorithm which executes least number of steps on input data of size n.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Average Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;provides a prediction about the running time of the algorithm.&lt;/li&gt;
&lt;li&gt;assumes that the input is random.&lt;/li&gt;
&lt;li&gt;algorithm which performs average number of steps on input data of size n.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Lower Bound &amp;lt;= Average Time &amp;lt;= Upper Bound&lt;/code&gt;&lt;/p&gt;



&lt;h1&gt;
  
  
  Asymptotic Notations
&lt;/h1&gt;

&lt;p&gt;Asymptotic Notations is having an expressions for the best, average and worst cases, for all the three cases we need to identify the upper and lower bounds. To represent these upper and lower bounds we need some kind of syntax and that is the following discussion. Let us assume that for a given algorithm, it can be represented in the form of function &lt;em&gt;f(n)&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big - O Notation :
&lt;/h2&gt;

&lt;p&gt;This notation gives the &lt;strong&gt;&lt;em&gt;tight upper bound&lt;/em&gt;&lt;/strong&gt; of the given algorithm / function &lt;em&gt;f(n)&lt;/em&gt;. It is represented as &lt;/p&gt;

&lt;p&gt;&lt;code&gt;f(n) = O(g(n))&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It means, for larger values of n, the &lt;em&gt;upper bound&lt;/em&gt; of function &lt;em&gt;f(n)&lt;/em&gt; is a function &lt;em&gt;g(n)&lt;/em&gt;. &lt;br&gt;&lt;br&gt;
Here &lt;em&gt;upper bound&lt;/em&gt; means, value of f(n) cannot exceed g(n) after a particular value of n. (represented as n0 in the graphical approach). &lt;br&gt;&lt;br&gt;
Let's see this with a graphical approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625574434388%2Fc5xYTEjgFV.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625574434388%2Fc5xYTEjgFV.jpeg" alt="asymptotic_notations-1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After &lt;em&gt;n = n0&lt;/em&gt;, value of g(n) is always greater than or equal to the given algorithm's rate of growth f(n). &lt;/p&gt;

&lt;p&gt;Also, for example, if &lt;em&gt;f(n)&lt;/em&gt; = n&lt;sup&gt;4&lt;/sup&gt; + 100n + 50 is the given algorithm, then n&lt;sup&gt;4&lt;/sup&gt; is &lt;em&gt;g(n)&lt;/em&gt;. That means, &lt;em&gt;g(n) = n&lt;sup&gt;4&lt;/sup&gt;&lt;/em&gt; gives the maximum rate of growth for &lt;em&gt;f(n)&lt;/em&gt; at larger values of n. &lt;/p&gt;

&lt;p&gt;O-Notation can be also be defined as &lt;strong&gt;O(g(n))&lt;/strong&gt; = { &lt;strong&gt;f(n)&lt;/strong&gt; : there exists positive constant c and n0 such that &lt;code&gt;0 ≤ f(n) ≤ cg(n)&lt;/code&gt; for all  n ≥ n0 }. &lt;/p&gt;

&lt;p&gt;Our objective is to get smallest rate of growth g(n) which is greater than or equal to f(n)'s rate of growth.&lt;br&gt;&lt;br&gt;
Generally we discard lower values of n. That means the rate of growth at lower values of n is not important. In the graph, n0 is the point from which we need to consider the rate of growths for a given algorithm. Below n0 the rate of growths could be different.&lt;/p&gt;

&lt;h4&gt;
  
  
  Some Big - O Examples:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;f(n) = n&lt;sup&gt;2&lt;/sup&gt; + 1&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;n&lt;sup&gt;2&lt;/sup&gt; + 1 ≤ 2n&lt;sup&gt;2&lt;/sup&gt;, for all n ≥ 1&lt;/p&gt;

&lt;p&gt;Therefore, n&lt;sup&gt;2&lt;/sup&gt; + 1 = O(n&lt;sup&gt;2&lt;/sup&gt;), with c = 2 and n0 = 1&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;f(n) = 2n&lt;sup&gt;3&lt;/sup&gt; - 2n&lt;sup&gt;2&lt;/sup&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2n&lt;sup&gt;3&lt;/sup&gt; - 2n&lt;sup&gt;2&lt;/sup&gt; ≤ 2n&lt;sup&gt;3&lt;/sup&gt;, for all n ≥ 1&lt;/p&gt;

&lt;p&gt;Therefore, 2n&lt;sup&gt;3&lt;/sup&gt; - 2n&lt;sup&gt;2&lt;/sup&gt; = O(2n&lt;sup&gt;3&lt;/sup&gt;), with c = 2 and n0 = 1&lt;/p&gt;

&lt;p&gt;There is one more thing related to values of n0 and c, that is, there is no isolated set of values for n0 and c in finding the asymptotic bounds. Let us see an example, &lt;br&gt;
100n + 5 = O(n). For this function, there can be multiple values for n0 and c, giving us an asymptotic solution / bound.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 1&lt;/strong&gt;: 100n + 5 ≤ 100n + n = 101n, for all n ≥ 5, n0 = 5 and c = 101. &lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution 2&lt;/strong&gt;: 100n + 5 ≤ 100n + 5n = 105n, for all n ≥ 1, n0 = 1 and c = 105.&lt;/p&gt;

&lt;p&gt;Both possibilities are correct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; Most of the time we are interested in knowing the Big - O Notation i.e. Tight Upper Bound of an algorithm because it allows us to estimate weather an algorithm is feasible for our application or not, by telling us that this algorithm will not take more than such-and-such amount of memory or time when run on an input of size n. &lt;/p&gt;

&lt;h2&gt;
  
  
  Omega - Ω Notation :
&lt;/h2&gt;

&lt;p&gt;This notation gives the &lt;strong&gt;&lt;em&gt;tight lower bound&lt;/em&gt;&lt;/strong&gt; of the given algorithm / function &lt;em&gt;f(n)&lt;/em&gt;. We can represent it as &lt;/p&gt;

&lt;p&gt;&lt;code&gt;f(n) = Ω(g(n))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It means, for larger values of n, g(n) is the &lt;em&gt;lower bound&lt;/em&gt; of function f(n). &lt;br&gt;&lt;br&gt;
Here &lt;em&gt;lower bound&lt;/em&gt; means, rate of growth of &lt;em&gt;f(n)&lt;/em&gt; is always greater than or equal to the rate of growth of function &lt;em&gt;g(n)&lt;/em&gt; after a particular value of n i.e. n0. &lt;br&gt;&lt;br&gt;
Let's see this with a graphical approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625827947954%2FnqxScttcq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625827947954%2FnqxScttcq.jpeg" alt="asymptotic_notations-2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After n = n0, value of g(n) is always smaller than or equal to the given algorithm's rate of growth f(n). &lt;/p&gt;

&lt;p&gt;Ω Notation can also be defined as &lt;strong&gt;Ω(g(n))&lt;/strong&gt; = { &lt;strong&gt;f(n)&lt;/strong&gt; : there exists positive constants n0 and c such that* &lt;code&gt;0 ≤ cg(n) ≤ f(n)&lt;/code&gt; * for all n ≥ n0 }. &lt;/p&gt;

&lt;p&gt;Here, our objective is to get largest rate of growth g(n) which is less than or equal to f(n)'s rate of growth. g(n) is asymptotic lower bound for the given algorithm's rate of growth f(n).&lt;/p&gt;

&lt;h4&gt;
  
  
  Some Ω Examples :
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;f(n) = 5n&lt;sup&gt;2&lt;/sup&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;∃ (there exists) c, n0, such that: 0 ≤ cn ≤ 5n&lt;sup&gt;2&lt;/sup&gt;  =&amp;gt; c = 1 and n0 = 1&lt;/p&gt;

&lt;p&gt;Therefore, 5n&lt;sup&gt;2&lt;/sup&gt; = Ω(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;2n = Ω(n), n&lt;sup&gt;3&lt;/sup&gt; = Ω(n&lt;sup&gt;3&lt;/sup&gt;), logn = Ω(logn)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*&lt;em&gt;Note : *&lt;/em&gt;  Lower bounds are of great use as well. Lower bounds can give information about whether we can improve our algorithm or is it feasible. We can also know that our algorithm is optimal, if our lower bound is equal to the upper bound. &lt;/p&gt;

&lt;h2&gt;
  
  
  Theta - Θ Notation :
&lt;/h2&gt;

&lt;p&gt;This notation gives a range of upper bound and lower bound and determines whether the upper bound and lower bound of the given algorithm are same. The average running time of an algorithm is always between the &lt;strong&gt;lower bound&lt;/strong&gt; (&lt;em&gt;Omega - Ω&lt;/em&gt;) and &lt;strong&gt;upper bound&lt;/strong&gt; (&lt;em&gt;Big - O&lt;/em&gt;) of the function. If the upper bound and lower bound of a function gives the same result (rate of growth) then Theta - Θ will also have the same rate of growth. &lt;/p&gt;

&lt;p&gt;For example, assume f(n) = 10n + n, then its tight upper bound is O(n) and the lower bound is Ω(n). &lt;br&gt;
In this case, rate of growths in the best case and worst case are same. As a result, the average case will also be the same. &lt;br&gt;
If for a given algorithm, the rate of growths O and Ω are not same then the rate of growth for Θ may not be same. In this case, we have to consider all possible time complexities and take average of those. (for example, &lt;em&gt;quick sort&lt;/em&gt; average case gives Θ(nlogn) complexity)&lt;/p&gt;

&lt;p&gt;Let us also see this in a graphical approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625828191290%2F0lp6yHxKk.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625828191290%2F0lp6yHxKk.jpeg" alt="asymptotic_notations-3.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After n = n0, the value of f(n) is always between c2g(n) and c1g(n). &lt;/p&gt;

&lt;p&gt;Θ Notation can also be defined as &lt;strong&gt;Θ(g(n))&lt;/strong&gt; = { &lt;strong&gt;f(n)&lt;/strong&gt; : there exists positive constants c1, c2, and n0 such that*  0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) , for all n ≥ n0 }. g(n) is an asymptotic tight bound for f(n).&lt;/p&gt;

&lt;p&gt;Θ(g(n)) is a set of functions with same order of growth as g(n).&lt;/p&gt;

&lt;h4&gt;
  
  
  Some Θ Examples:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Prove n ≠ Θ(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;c1n&lt;sup&gt;2&lt;/sup&gt; ≤ n ≤  c2n&lt;sup&gt;2&lt;/sup&gt;, only holds for n ≤ 1 / &lt;br&gt;
 c1&lt;/p&gt;

&lt;p&gt;Therefore, n ≠ Θ(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prove 6n&lt;sup&gt;3&lt;/sup&gt; ≠ Θ(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/strong&gt;
c1n&lt;sup&gt;2&lt;/sup&gt; ≤ 6n&lt;sup&gt;3&lt;/sup&gt; ≤  c2n&lt;sup&gt;2&lt;/sup&gt;, only holds for n ≤ c2 / 6&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Therefore, 6n&lt;sup&gt;3&lt;/sup&gt; ≠ Θ(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/p&gt;

&lt;h4&gt;
  
  
  Why is it called Asymptotic Notations?
&lt;/h4&gt;

&lt;p&gt;For all the three notations, O, Ω, Θ, in every case for a given function f(n) we are trying to find other function g(n) which approximates f(n) at large values of n. That means, g(n) is also a &lt;em&gt;curve&lt;/em&gt; which approximates f(n) at large values of n. &lt;/p&gt;

&lt;p&gt;In mathematics, we call such curves as &lt;strong&gt;asymptotic curves&lt;/strong&gt;. In other terms, g(n) is the asymptotic curve for f(n). For this reason, we call algorithm analysis as &lt;strong&gt;asymptotic analysis&lt;/strong&gt; and notations as &lt;strong&gt;asymptotic notations&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Properties of Notations:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Transitivity&lt;/strong&gt; : f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then f(n) = Θ(h(n)). Valid for O and Ω as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reflexivity&lt;/strong&gt; : f(n) = Θ(f(n)). Valid for O and Ω as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Symmetry&lt;/strong&gt; : f(n) = Θ(g(n)) if and only if g(n) = Θ(f(n)).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transpose symmetry&lt;/strong&gt; : f(n) = O(g(n)) if and only if g(n) = Ω(f(n)).&lt;/li&gt;
&lt;/ul&gt;



&lt;h1&gt;
  
  
  Bonus :
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Master Theorem for Divide and Conquer
&lt;/h3&gt;

&lt;p&gt;All divide and conquer algorithms works by dividing the problem into sub-problems, each of which is part of the original problem and then we perform some additional work to compute the final answer. As an example, &lt;em&gt;merge sort&lt;/em&gt; algorithm operates on two sub problems, each of which is half the size of the original problem and then performs O(n) additional work for merging the sub-problems.&lt;/p&gt;

&lt;p&gt;This gives the running time of the equation : &lt;/p&gt;

&lt;p&gt;&lt;code&gt;T(n) = 2T(n / 2) + O(n)&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;Master theorem can be used to determine the running time of divide and conquer algorithms. For a given algorithm, first we try to find the recurrence relation of the problem. If the recurrence relation is of the below form then we can directly give the answer without fully solving it. &lt;/p&gt;

&lt;p&gt;If the recurrence relation is of the form, ** T(n) = aT(n / b) + Θ(n&lt;sup&gt;k&lt;/sup&gt;log&lt;sup&gt;p&lt;/sup&gt;n)**, where a ≥ 1, b &amp;gt; 1, k ≥ 0 and p is a real number, then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If a &amp;gt; b&lt;sup&gt;k&lt;/sup&gt;, then T(n) = Θ(n&lt;sup&gt;log&lt;sup&gt;a&lt;/sup&gt;b&lt;/sup&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a = b&lt;sup&gt;k&lt;/sup&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If p &amp;gt; -1, then T(n) = Θ(n&lt;sup&gt;log&lt;sup&gt;a&lt;/sup&gt;b&lt;/sup&gt;log&lt;sup&gt;p+1&lt;/sup&gt;n).
&lt;/li&gt;
&lt;li&gt;If p = -1, then T(n) = Θ(n&lt;sup&gt;log&lt;sup&gt;a&lt;/sup&gt;b&lt;/sup&gt;loglogn).
&lt;/li&gt;
&lt;li&gt;If p &amp;lt; -1, then T(n) = Θ(n&lt;sup&gt;log&lt;sup&gt;a&lt;/sup&gt;b&lt;/sup&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;If a &amp;lt; b&lt;sup&gt;k&lt;/sup&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If p ≥ 0, then T(n) = Θ(n&lt;sup&gt;k&lt;/sup&gt;log&lt;sup&gt;p&lt;/sup&gt;n) 
&lt;/li&gt;
&lt;li&gt;If p &amp;lt; 0, then T(n) = O(n&lt;sup&gt;k&lt;/sup&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank You!&lt;/p&gt;

</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>Templates in C++</title>
      <dc:creator>Abhishek Chandra</dc:creator>
      <pubDate>Sat, 03 Jul 2021 13:48:08 +0000</pubDate>
      <link>https://dev.to/abhishekchandra2522k/templates-in-c-3hao</link>
      <guid>https://dev.to/abhishekchandra2522k/templates-in-c-3hao</guid>
      <description>&lt;h1&gt;
  
  
  Templates in C++ Language
&lt;/h1&gt;

&lt;p&gt;The keyword &lt;code&gt;template&lt;/code&gt; is used to define function templates and class templates in our C++ program.&lt;br&gt;
It introduces generic programming, which makes a way for programmers to write more efficient code.&lt;/p&gt;

&lt;p&gt;So, now there are two ways in which we can use template, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Function template
&lt;/h3&gt;

&lt;p&gt;Function template is also known as &lt;strong&gt;&lt;em&gt;generic function&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;syntax&lt;/code&gt; creating function template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;template&amp;lt;class type&amp;gt; 
type func_name(type arg1, type arg2, ....){
     ...
};
// here type is a placeholder for generalisation of data type.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It creates a function template named &lt;code&gt;func_name&lt;/code&gt; with any number of arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's learn this with an example&lt;/strong&gt;,&lt;/p&gt;

&lt;p&gt;Program to find the smaller number from the given two numbers* without the use of &lt;code&gt;template&lt;/code&gt;*:&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;iostream&amp;gt;

using namespace std;

// without the use of template keyword.
int small(int a, int b){
    if(a &amp;gt; b){
        return(b);    
    }else{
        return(a);
    }
};

double small(double a, double b){ // function overloading
    if(a &amp;gt; b){
        return(b);
    }else{
        return(a);
    }
};


int main(){
    cout&amp;lt;&amp;lt;small(2,6)&amp;lt;&amp;lt;endl;
    cout&amp;lt;&amp;lt;small(2.4,1.9);
    return 0;
}

/*
OUTPUT
2
1.9
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(32 Lines of Code)&lt;/p&gt;

&lt;p&gt;To avoid using different data types in a function,  &lt;em&gt;Use &lt;code&gt;template&lt;/code&gt;&lt;/em&gt;.&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;iostream&amp;gt;

using namespace std;

// Program using template keyword
template&amp;lt;class X&amp;gt;
X small(X a, X b){ // here X is a generic data type, making 'small' a generic function.
    if(a &amp;lt; b){
        return(b);
    }else{
        return(a);
    }
};

int main(){
    cout&amp;lt;&amp;lt;small(3,4)&amp;lt;&amp;lt;endl;
    cout&amp;lt;&amp;lt;small(3.4,1.6); // no function overloading reduces the lines of code in a program
    return 0;
}

/*
OUTPUT
3
1.6
*/

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

&lt;/div&gt;



&lt;p&gt;(25 Lines of Code)&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;template&amp;lt;class X, class Y&amp;gt;  
X big(X a, Y b){        //different data types
    if(a &amp;gt; b){
        return a;
    }else{
        return b;
    }
};

int main(){
    cout&amp;lt;&amp;lt;big(4,3.4)&amp;lt;&amp;lt;endl;
    cout&amp;lt;&amp;lt;big(1.99,-5);
    return 0;
}

/*
OUTPUT:
4
1.99
*/

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

&lt;/div&gt;



&lt;p&gt;(20 Lines of Code)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Class template
&lt;/h3&gt;

&lt;p&gt;Class template is also known as &lt;strong&gt;&lt;em&gt;generic class&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;template&amp;lt;class type&amp;gt; 
class class_name{
    ...
};
// here type is also a placeholder to generalise a class.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It creates a class template named &lt;code&gt;class_name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's also learn this with an example, &lt;br&gt;
Here we are making a &lt;code&gt;arrayList&lt;/code&gt; class without the use of template.&lt;/strong&gt;&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;iostream&amp;gt;

using namespace std;

class arrayList{
    private:
        struct controlBlock{
            int capacity;
            int *arr_ptr;
        };

    controlBlock *s;

    public:
        arrayList(int capacity){ // constructor of arrayList class
            s = new controlBlock;
            s -&amp;gt; capacity = capacity;
            s -&amp;gt; arr_ptr = new int[s-&amp;gt;capacity];
        }
        void addElement(int index, int data){ // method to add elements to the list
            if(index &amp;gt;= 0 &amp;amp;&amp;amp; index &amp;lt;= s-&amp;gt;capacity - 1){
                s -&amp;gt; arr_ptr[index]  = data;
            }else{
                cout&amp;lt;&amp;lt;"Array index not valid";
            }
        }
        void viewElement(int index, int &amp;amp;data){
            if(index &amp;gt;=0 &amp;amp;&amp;amp; index &amp;lt;= s-&amp;gt;capacity - 1){
                data = s-&amp;gt;arr_ptr[index];
            }else{
                cout&amp;lt;&amp;lt;"Array index not valid";
            }
        }
        void viewList(){ // method to view the list
            int i;
            for(i = 0; i &amp;lt; s-&amp;gt;capacity; i++){
                cout&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;s-&amp;gt;arr_ptr[i];
            }
        }
};

int main(){
    int data;
    arrayList list1(4);
    list1.addElement(0,2);
    list1.addElement(1,12);
    list1.addElement(2,22);
    // list1.addElement(3,32); if not assigned, by default 0 is assigned.
    // list1.viewElement(0,data);
    // cout&amp;lt;&amp;lt;data;
    list1.viewList();
    return 0;
}

/*
OUTPUT:
2
12
22
0
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(61 Lines of Code)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image explanation of code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R4Fo74Ng--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1613036200174/AEfvN3Sbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R4Fo74Ng--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1613036200174/AEfvN3Sbj.png" alt="template_c++.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, templating our above program will result in increased possibilities of input with the same number of lines of code.&lt;/p&gt;

&lt;p&gt;visit : &lt;a href="https://github.com/abhishekchandra2522k/CPPrograms/blob/master/Standard%20Template%20Library%20/arrayList_with_template.cpp"&gt;arrayList_with_template.cpp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go read about STL in &lt;a href="https://dev.to/abhishekchandra2522k/standard-template-library-in-c-2h10"&gt;this article&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Standard Template Library in C++</title>
      <dc:creator>Abhishek Chandra</dc:creator>
      <pubDate>Thu, 10 Jun 2021 17:39:20 +0000</pubDate>
      <link>https://dev.to/abhishekchandra2522k/standard-template-library-in-c-2h10</link>
      <guid>https://dev.to/abhishekchandra2522k/standard-template-library-in-c-2h10</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vector&amp;lt;int&amp;gt; LET_S;
pair&amp;lt;string, int&amp;gt; GET;
list&amp;lt;int&amp;gt; STARTED;
array&amp;lt;int, 3&amp;gt; WITH;
map&amp;lt;int, string&amp;gt; STL;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still don't know what these statement do?&lt;br&gt;&lt;br&gt;
Let's read this article to head start your journey in STL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Standard Template Libraries&lt;/strong&gt; are powerful set of &lt;a href="https://dev.to/abhishekchandra2522k/templates-in-c-3hao"&gt;C++ template classes&lt;/a&gt;. It contains numerous pre-defined classes and are used to make programming easier.&lt;br&gt;&lt;br&gt;
Consider Standard Template Libraries as a helping hand for not writing codes to implement Data Structures like &lt;code&gt;Linked Lists&lt;/code&gt;, &lt;code&gt;Stacks&lt;/code&gt;, &lt;code&gt;Queues&lt;/code&gt;,  &lt;code&gt;Trees&lt;/code&gt; etc.  in every program we require them (mostly in competitive programming questions), we can use these data structures by just including a library and using a pre-defined syntax.&lt;br&gt;&lt;br&gt;
Also, classes in Standard Template Libraries are made through &lt;a href="https://dev.to/abhishekchandra2522k/templates-in-c-3hao"&gt;template classes&lt;/a&gt; so these are generic in nature.&lt;/p&gt;

&lt;p&gt;At the core of the C++ Standard Template Libraries, these are the following three well structured components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Containers.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Algorithms.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterators.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Containers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Containers are used to manage and store &lt;code&gt;collection of objects&lt;/code&gt; of a certain kind.&lt;/li&gt;
&lt;li&gt;Container Library in STL provide containers that are used to create data structure like arrays, linked-list, trees, etc.&lt;/li&gt;
&lt;li&gt;Containers helps us to implement and replicate simple and complex data structures very easily like array, list, trees, associative arrays and many more.&lt;/li&gt;
&lt;li&gt;These containers are generic in nature, they can hold elements of any data types.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Common Containers that replicates simple and complex data structures
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;vector&lt;/code&gt; : replicates arrays&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;queue&lt;/code&gt; : replicates queues&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stack&lt;/code&gt; : replicates stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;priority_queue&lt;/code&gt; : replicated heaps&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list&lt;/code&gt; : replicates linked list&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set&lt;/code&gt; : replicates trees&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; : associative arrays&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Classification of containers
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sequence Containers&lt;/strong&gt; : These containers hold linear storage of data, like arrays, linked list etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Associative Containers&lt;/strong&gt; : Ordered containers like map, multimap, set, multiset are associative containers. (Ordered means the values are stored in the respective container in the same order as inputted by the user)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unordered Associative Containers&lt;/strong&gt; : These are similar to associative containers but have different constraints, the elements are stored unordered due to the use of hash tables while implementing these containers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  How to use containers?
&lt;/h4&gt;

&lt;p&gt;When we use containers to implement data structures, we just have to include a header file and use the respective syntax to initialise the data structure you need.&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;bits/stdc++.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can just include a specific container header file,&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;iostream&amp;gt;

// importing vector header file to implement vectors
#include&amp;lt;vector&amp;gt;

int main(){
 vector&amp;lt;int&amp;gt; first_vector;
 return 0;
}

// vector can be used for creating dynamic arrays of `char`, `int`, `float` and other types.

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

&lt;/div&gt;





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

//  importing list header file to implement lists
#include&amp;lt;list&amp;gt;

int main(){
 list&amp;lt;int&amp;gt; my_list;
 return 0;
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Algorithms
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of container.&lt;/li&gt;
&lt;li&gt;Algorithms Library contains built-in functions that perform complex algorithms on the data structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can reverse a range with reverse() function, sort a range with sort() function, search on a range with binary_search() and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Algorithm Libraries provide abstraction, i.e. you don't necessarily need to know how the algorithm works.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Sorting of a vector&lt;/em&gt;&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;bits/stdc++.h&amp;gt;

using namespace std;

int main(){
 vector&amp;lt;int&amp;gt; second_vector {44,22,66,33,99};

// begin() and end() method returns an iterator to the vector.
 sort(second_vector.begin(), second_vector.end());
 return 0;
}

/* 
Above code will sort the vector to {22,33,44,66,99}.
More about vector will be covered in a separate node.
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Iterators
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Iterators are used to step through the elements of collection of objects. These collections can be containers or subsets of containers.&lt;/li&gt;
&lt;li&gt;Iterators in Standard Template Libraries are used to point to the containers.&lt;/li&gt;
&lt;li&gt;Iterators actually acts as a bridge b/w containers and algorithms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at the above code in algorithms section for reference to iterators, &lt;br&gt;&lt;br&gt;
sort() algorithm or function have two parameters, starting iterator (&lt;code&gt;vector.begin()&lt;/code&gt;) and ending iterator (&lt;code&gt;vector.end()&lt;/code&gt;) pointing to first and last element in the vector respectively, now sort() compares the elements pointed by each of these iterators and arrange them in sorted order, thus it does not matter what is the type of the container and same sort can be used on different types of containers.&lt;/p&gt;
&lt;h4&gt;
  
  
  How to declare an iterator?
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Syntax&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is an iterator of type vector
vector&amp;lt;int&amp;gt; :: iterator itr; 
// itr is the iterator

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

&lt;/div&gt;



&lt;p&gt;Example use of iterator:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Printing elements of a vector&lt;/em&gt;&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;bits/stdc++.h&amp;gt;

using namespace std;

int main(){
    vector&amp;lt;int&amp;gt; third_vector {10,20,30,40,50};
    vector&amp;lt;int&amp;gt; :: iterator itr;
    for(itr = third_vector.begin(); itr != third_vector.end(); itr++){
        cout&amp;lt;&amp;lt;*itr&amp;lt;&amp;lt;" ";
    }
    return 0;
}

/*
OUTPUT : 10 20 30 40 50 
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visit &lt;a href="https://github.com/abhishekchandra2522k/CPPrograms/tree/master/Standard%20Template%20Library%20"&gt;this&lt;/a&gt; to view some Standard Template Libraries programs.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>stl</category>
      <category>competitive</category>
      <category>programming</category>
    </item>
    <item>
      <title>Strings in C++</title>
      <dc:creator>Abhishek Chandra</dc:creator>
      <pubDate>Sat, 13 Mar 2021 15:44:48 +0000</pubDate>
      <link>https://dev.to/abhishekchandra2522k/strings-in-c-6ih</link>
      <guid>https://dev.to/abhishekchandra2522k/strings-in-c-6ih</guid>
      <description>&lt;p&gt;Youkoso!&lt;/p&gt;

&lt;h4&gt;
  
  
  Here's Traditional Way (C Language)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We use &lt;em&gt;null-terminated (&lt;code&gt;\0&lt;/code&gt;) character array&lt;/em&gt;, although it is not technically a data type.&lt;/li&gt;
&lt;li&gt;So, Operators cannot be applied to them, like assignment and comparison operators &lt;code&gt;=, &amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Declaration of character array
char s1[10], s2[10];
s1[10] = "Doge";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0c1rardwthip8zkxs54.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0c1rardwthip8zkxs54.png" alt="s1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Error Full Code (Don't use at home/work)
s2 = s1;
s2 &amp;gt; s1;
s3 = s1 + s2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code results in invalid array operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strings in C++
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;string&lt;/code&gt; class is a expertise class of a more general template called &lt;code&gt;basic_string&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since defining a class in C++ is creating a new data type, string is derived data type.&lt;/li&gt;
&lt;li&gt;This means operators can be overloaded for this class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fefzcbao3d9cmc36wb73h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fefzcbao3d9cmc36wb73h.png" alt="Strings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  syntax
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class string{
   // Variables
   // functions
   // operators
}

string s1;
s1.function();
s1.operator(arguments);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;string operations are safe but time consuming. So, 'char array' (speedy, less operations) concept is &lt;strong&gt;not&lt;/strong&gt; deprecated.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (speed matters)&lt;/code&gt; {&lt;br&gt;
 Use &lt;code&gt;character array&lt;/code&gt;&lt;br&gt;
}&lt;code&gt;else if (safety and easy manipulation matters)&lt;/code&gt; {&lt;br&gt;
 Use &lt;code&gt;string&lt;/code&gt; class&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Here's why &lt;code&gt;string&lt;/code&gt; is safer than character array&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Careful programmers like you, can overrun to the end of 
an array that holds a null terminated (null character &lt;code&gt;\0&lt;/code&gt;) string.&lt;/li&gt;
&lt;li&gt;for example - &lt;em&gt;see below&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;string class handles such issues.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;char s3[10];
strcpy(s3,"Hello careful programmers.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1733oy61mimje3naf4ft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1733oy61mimje3naf4ft.png" alt="s3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  String is also in STL (but concept of string is thought apart from STL concepts)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;string is an another container class.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To use string class, we have to include string header class. (&lt;strong&gt;not&lt;/strong&gt; string.h)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;#include&amp;lt;string&amp;gt;&lt;/code&gt; (for string header class)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#include&amp;lt;string.h&amp;gt;&lt;/code&gt; (in C, for string functions applied on character array)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h5&gt;
  
  
  String class supports many constructors as follows.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;string()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string(const char *str)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string(const string &amp;amp;str)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Operators
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;=&lt;/code&gt; (assignment)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; (concatenation)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+=&lt;/code&gt; (concatenation assignment)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt; (equality)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!=&lt;/code&gt; (inequality)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&lt;/code&gt; (less than)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;=&lt;/code&gt; (less than equal to)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; (greater than)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;=&lt;/code&gt; (greater than equal to)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt; (subscripting)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; (insertion)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; (extraction)&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Mixed Operations
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;We can mix string objects with another string object or C style string.&lt;/li&gt;
&lt;li&gt;C++ string can also be concatenated with character const.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Useful methods
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;assign()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;append()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;insert()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;replace()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;erase()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;find()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rfind()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;compare()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;c_str()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Visit :
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/abhishekchandra2522k/CPPrograms/blob/master/strings.cpp" rel="noopener noreferrer"&gt;strings.cpp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abhishekchandra.hashnode.dev/templates-in-cpp" rel="noopener noreferrer"&gt;Templates in C++&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ittekimasu! :)&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>strings</category>
    </item>
  </channel>
</rss>
