{"id":27313,"date":"2018-01-20T20:52:04","date_gmt":"2018-01-20T15:22:04","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27313"},"modified":"2018-01-20T20:52:04","modified_gmt":"2018-01-20T15:22:04","slug":"c-algorithm-stock-span-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-stock-span-problem\/","title":{"rendered":"C Algorithm &#8211; The Stock Span Problem"},"content":{"rendered":"<p>The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock\u2019s price for all n days. <span id=\"more-29347\"><\/span><br \/>\nThe span Si of the stock\u2019s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.<br \/>\nFor example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}<\/p>\n<p><strong>A Simple but inefficient method<\/strong><br \/>\nTraverse the input price array. For every element being visited, traverse elements on left of it and increment the span value of it while elements on the left side are smaller.<\/p>\n<p>Following is implementation of this method.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program for brute force method to calculate stock span values<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ Fills array S[] with span values<br\/>void calculateSpan(int price[], int n, int S[])<br\/>{<br\/>   \/\/ Span value of first day is always 1<br\/>   S[0] = 1;<br\/> <br\/>   \/\/ Calculate span value of remaining days by linearly checking<br\/>   \/\/ previous days<br\/>   for (int i = 1; i &lt; n; i++)<br\/>   {<br\/>      S[i] = 1; \/\/ Initialize span value<br\/> <br\/>      \/\/ Traverse left while the next element on left is smaller<br\/>      \/\/ than price[i]<br\/>      for (int j = i-1; (j&gt;=0)&amp;&amp;(price[i]&gt;=price[j]); j--)<br\/>          S[i]++;<br\/>   }<br\/>}<br\/> <br\/>\/\/ A utility function to print elements of array<br\/>void printArray(int arr[], int n)<br\/>{<br\/>    for (int i = 0; i &lt; n; i++)<br\/>      printf(&quot;%d &quot;, arr[i]);<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int price[] = {10, 4, 5, 90, 120, 80};<br\/>    int n = sizeof(price)\/sizeof(price[0]);<br\/>    int S[n];<br\/> <br\/>    \/\/ Fill the span values in array S[]<br\/>    calculateSpan(price, n, S);<br\/> <br\/>    \/\/ print the calculated span values<br\/>    printArray(S, n);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>1 1 2 4 5 1<\/pre>\n<p>Time Complexity of the above method is O(n^2). We can calculate stock span values in O(n) time.<\/p>\n<p><strong>A Linear Time Complexity Method<\/strong><br \/>\nWe see that S[i] on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i. If such a day exists, let\u2019s call it h(i), otherwise, we define h(i) = -1.<br \/>\nThe span is now computed as S[i] = i \u2013 h(i). See the following diagram.<\/p>\n<p>To implement this logic, we use a stack as an abstract data type to store the days i, h(i), h(h(i)) and so on. When we go from day i-1 to i, we pop the days when the price of the stock was less than or equal to price[i] and then push the value of day i back into the stack.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; The Stock Span Problem &#8211; Stack &#8211; The stock span problem is a financial problem where we have a series of n daily price quotes <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,73012,79607],"tags":[81385,81384,81382,81381,81380,81378,81379,81377,81383],"class_list":["post-27313","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-data-structures","category-stack","tag-best-time-to-buy-and-sell-stock-dynamic-programming","tag-largest-rectangle-under-histogram","tag-stock-geeks","tag-stock-problem-geeksforgeeks","tag-stock-span-problem-geeksforgeeks","tag-stock-span-problem-hackerrank","tag-stock-span-problem-using-stack-in-c","tag-stock-span-problem-wiki","tag-what-is-stock-recommendation-problem"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27313"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}