codility: EquiLeader Segmentation Fault problem in my code : <p>I have a problem with this simple code. Propably the problem is with using the vector class:</p>

<pre><code>int solution(vector&lt;int&gt; &amp;A) {
    // write your code in C++14 (g++ 6.2.0)
    vector&lt;int&gt; left, right;
    int cnt=0,k,j;
    if (A.size()==1) return 0;
    if (A.size()==2 &amp;&amp; A[0]==A[1]) return 1;
    if (A.size()==2 &amp;&amp; A[0]!=A[1]) return 0;
    if (A.size()==3 &amp;&amp; A[0]==A[1] &amp;&amp; A[1]==A[2]) return 2;
    if (A.size()==3 &amp;&amp; A[0]==A[1] &amp;&amp; A[1]!=A[2]) return 0;
    for (size_t i=0; i&lt;(A.size()-1); ++i)
    {
        left.assign( A.begin(), A.begin() + i );
        right.assign( A.begin() + i + 1, A.end());
        sort(left.begin(), right.end());
        sort(right.begin(), right.end());
        j=left.size()/2;
        k=right.size()/2;
        if (left[j]==right[k])
        {
            cnt++;
        }
    }
    return cnt;
}
</code></pre>

<p>after execution it return :</p>

<pre><code>stderr:
Segmentation Fault
</code></pre>
