Count subarrays having total distinct elements same as original array in C++

C++Server Side ProgrammingProgramming

<p>We are given an array arr[] containing integers. The goal is to count all subarrays of arr[] such that the number of distinct elements in each is the same as the number of distinct elements in the original array. If the original array is [1,1,2,3] then subarrays will be [1,2,3] and [1,1,2,3].</p><p>Total distinct elements in the original array is 3. Total distinct elements in both subarrays is also 3</p><p>Let us understand with examples.</p><p><strong>Input</strong> &minus; arr[] = {1,2,1,2,3,4,2 };</p><p><strong>Output</strong> &minus; Count of subarrays having total distinct elements same as an original array is &minus; 6</p><p><strong>Explanation</strong> &minus; Distinct elements in arr[] are 4 (1,2,3,4). Subarrays with the same number of distinct elements are: ( count distinct from left to right )</p><pre class="result notranslate">[1,2,1,2,3,4], [2,1,2,3,4], [1,2,3,4], [1,2,3,4,2], [2,1,2,3,4,2], [1,2,1,2,3,4,2 ]</pre><p><strong>Input</strong> &minus; arr[] = {8,7,5,6,10 };</p><p><strong>Output</strong> &minus; Count of subarrays having total distinct elements same as an original array is &minus; 1</p><p><strong>Explanation</strong> &minus; Distinct elements in arr[] are 5 (5,6,7,8,10). Subarrays with same number of distinct elements are: ( count distinct from left to right ) [8,7,6,5,10]. 1 only</p><h2>The approach used in the below program is as follows</h2><ul class="list"><li><p>Take an array arr[] of integer numbers and calculate the size of an array.</p></li><li><p>Function sub_ele_diff_one(int arr[], int size) takes the array and returns a count of subarrays with consecutive elements differing by 1.</p></li><li><p>Take a temporary variable count and variables right and left.</p></li><li><p>Take a variable of type unordered_map for creating the random pairs.</p></li><li><p>Start loop FOR from 0 till the size of an array and inside it set the value of arr[i] inside the unordered_map.</p></li><li><p>Now, calculate the size of an unordered_map and clear the unordered_map.</p></li><li><p>Start loop FOR from 0 till the size of an array.</p></li><li><p>Inside the loop, start WHILE till right &lt; size and left &lt; size of an unordered_map</p></li><li><p>Pre-Increment the value of um[arr[right]]</p></li><li><p>Now, check if um[arr[right]] = 1 then pre increment the value of left by 1.</p></li><li><p>Outside the WHILE, pre-increment the value of right by 1.</p></li><li><p>Check IF left = size of an unordered_map then set count to count + size - right + 1</p></li><li><p>Decrement the um[arr[i]] by 1</p></li><li><p>Check IF um[arr[i]] = 0 then decrement the left by 1</p></li><li><p>Return the count</p></li><li><p>Print the result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/4yCBoBgw" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; int sub_distinct(int arr[], int size){ &nbsp; &nbsp;int count = 0, right = 0, left = 0; &nbsp; &nbsp;unordered_map&lt;int, int&gt; um; &nbsp; &nbsp;for (int i = 0; i &lt; size; ++i){ &nbsp; &nbsp; &nbsp; um[arr[i]] = 1; &nbsp; &nbsp;} &nbsp; &nbsp;int um_size = um.size(); &nbsp; &nbsp;um.clear(); &nbsp; &nbsp;for(int i = 0; i &lt; size; ++i){ &nbsp; &nbsp; &nbsp; while (right &lt; size &amp;&amp; left &lt; um_size){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;++um[arr[right]]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (um[arr[right]] == 1){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++left; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;++right; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if (left == um_size){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count = count + (size - right + 1); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; --um[arr[i]]; &nbsp; &nbsp; &nbsp; if (um[arr[i]] == 0){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--left; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main(){ &nbsp; &nbsp;int arr[] = {4, 3, 2, 5}; &nbsp; &nbsp;int size = sizeof(arr) / sizeof(arr[0]); &nbsp; &nbsp;cout&lt;&lt;&quot;Count of subarrays having total distinct elements same as original array are: &quot;&lt;&lt;sub_distinct(arr, size); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Count of subarrays having total distinct elements same as original array are: 1</pre>
raja
Updated on 01-Dec-2020 12:37:35

Advertisements