c++ Vector memory problems
<p>The memory of my program is supposed to be &lt; 4-16 MB. How can I optimize it? I will explain what my program does in short :</p>

<p>I input n, k and n numbers in the vector a. Then I store in the vector b the counts of all equal numbers found in the vector a. So, for example, if the vector a contained 1,2,2,3, the vector B would store the counts like this:</p>

<pre><code>1 -&gt; 1
2 -&gt; 2
3 -&gt; 1 
</code></pre>

<p>Then I check if these counts are divisible by k and if not, I output that number and break the loop. I know there are some things that can be optimized but how do I deal with the memory problem? This is the code below : </p>

<pre><code>#include&lt;iostream&gt;
#include&lt;vector&gt;
using namespace std;
vector&lt;int&gt; a,b[1000001];
int main()
{
    int n,k;
    cin&gt;&gt;n&gt;&gt;k;
    for(int i=0; i&lt;n; i++)
    {
        int k;
        cin&gt;&gt;k;
        a.push_back(k);
        b[a[i]].push_back(1);

    }
    for(int i=n-1; i&gt;=0; i--)
    {
        if(b[a[i]].size() % k != 0)
        {

            cout&lt;&lt;a[i]&lt;&lt;"\n";

            break;

        }
    }
    return 0;
} 
</code></pre>