What is the difference between the two codes in C++? : <p>I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:</p>

<pre><code>void whatFlavors(vector&lt;int&gt; cost, int money) {
    int ans1,ans2;
    vector&lt;int&gt; arr(100,0);          //notice this
    for(int i=0;i&lt;cost.size();i++){
        if(arr[money-cost[i]]!=0){
            ans1=i+1;ans2=arr[abs(money-cost[i])];
            if(ans1&gt;ans2){
                cout&lt;&lt;ans2&lt;&lt;" "&lt;&lt;ans1&lt;&lt;endl;
            }else{
                cout&lt;&lt;ans2&lt;&lt;" "&lt;&lt;ans1&lt;&lt;endl;
            }
            break;
        }
        else{
            arr[cost[i]]=i+1;
        }
    }
}
</code></pre>

<p>And output is:
<a href="https://i.stack.imgur.com/mHqK6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mHqK6.png" alt="enter image description here"></a></p>

<p>Case 2:
code:</p>

<pre><code>void whatFlavors(vector&lt;int&gt; cost, int money) {
    int arr[100]={0}; //notice this
    int ans1,ans2;
    for(int i=0;i&lt;cost.size();i++){
        if(arr[money-cost[i]]!=0){
            ans1=i+1;ans2=arr[abs(money-cost[i])];
            if(ans1&gt;ans2){
                cout&lt;&lt;ans2&lt;&lt;" "&lt;&lt;ans1&lt;&lt;endl;
            }else{
                cout&lt;&lt;ans2&lt;&lt;" "&lt;&lt;ans1&lt;&lt;endl;
            }
            break;
        }
        else{
            arr[cost[i]]=i+1;
        }
    }

}
</code></pre>

<p>output:
<a href="https://i.stack.imgur.com/2oeLx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2oeLx.png" alt="enter image description here"></a></p>
