Maximum length of a sub-array with ugly numbers in C++

C++Server Side ProgrammingProgramming

<h2>Problem statement</h2><p>Given an array arr[] of N elements (0 &le; arr[i] &le; 1000). The task is to find the maximum length of the sub-array that contains only ugly numbers.</p><p>Ugly numbers are numbers whose only prime factors are 2, 3 or 5.</p><p>For Example below are the few number from series: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15,&hellip;</p><h2>Example</h2><p>If input array is {1, 2, 7, 9, 120, 810, 374} then answer is 3 as &minus;</p><p>Longest possible sub-array of ugly number sis {9, 120, 810}</p><h2>Algorithm</h2><ul class="list"><li>Take a unordered_set, and insert all the ugly numbers which are less than 1000 in the set</li><li>Traverse the array with two variables named current_max and max_so_far.</li><li>Check for each element if it is present in the set</li><li>If an ugly number is found, then increment current_max and compare it with max_so_far</li><li>If current_max &gt; max_so_far then max_so_far = current_max.</li><li>Every time a non-ugly element is found, reset current_max = 0.</li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/QZ6Ztz5L" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; unsigned getUglyNumbers(int n) { &nbsp; &nbsp;int ugly[n]; &nbsp; &nbsp;int i2 = 0, i3 = 0, i5 = 0; &nbsp; &nbsp;int next_multiple_of_2 = 2; &nbsp; &nbsp;int next_multiple_of_3 = 3; &nbsp; &nbsp;int next_multiple_of_5 = 5; &nbsp; &nbsp;int next_ugly_no = 1; &nbsp; &nbsp;ugly[0] = 1; &nbsp; &nbsp;for (int i = 1; i &lt; n; i++) { &nbsp; &nbsp; &nbsp; next_ugly_no = min(next_multiple_of_2, min(next_multiple_of_3, next_multiple_of_5)); &nbsp; &nbsp; &nbsp; ugly[i] = next_ugly_no; &nbsp; &nbsp; &nbsp; if (next_ugly_no == next_multiple_of_2) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i2 = i2 + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next_multiple_of_2 = ugly[i2] * 2; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if (next_ugly_no == next_multiple_of_3) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i3 = i3 + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next_multiple_of_3 = ugly[i3] * 3; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if (next_ugly_no == next_multiple_of_5) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i5 = i5 + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next_multiple_of_5 = ugly[i5] * 5; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return next_ugly_no; } int maxUglySubarray(int arr[], int n) { &nbsp; &nbsp;unordered_set&lt;int&gt; s; &nbsp; &nbsp;int i = 1; &nbsp; &nbsp;while (1) { &nbsp; &nbsp; &nbsp; int next_ugly_number = getUglyNumbers(i); &nbsp; &nbsp; &nbsp; if (next_ugly_number &gt; 1000) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; s.insert(next_ugly_number); &nbsp; &nbsp; &nbsp; i++; &nbsp; &nbsp;} &nbsp; &nbsp;int current_max = 0, max_so_far = 0; &nbsp; &nbsp;for (int i = 0; i &lt; n; i++) { &nbsp; &nbsp; &nbsp; if (s.find(arr[i]) == s.end()) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;current_max = 0; &nbsp; &nbsp; &nbsp; else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;current_max++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;max_so_far = max(current_max, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;max_so_far); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return max_so_far; } int main() { &nbsp; &nbsp;int arr[] = {1, 2, 7, 9, 120, 810, 374}; &nbsp; &nbsp;int n = sizeof(arr) / sizeof(arr[0]); &nbsp; &nbsp;cout &lt;&lt; &quot;Maximum sub-array size of consecutive ugly numbers = &quot; &lt;&lt; maxUglySubarray(arr, n) &lt;&lt; endl; &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>When you compile and execute above program. It generates following output &minus;</p><pre class="result notranslate">Maximum sub-array size of consecutive ugly numbers = 3</pre><p><br></p>
raja
Updated on 10-Jan-2020 07:31:31

Advertisements