I was solving hackerrank problem https://www.hackerrank.com/challenges/icecream-parlor/problem. If I declare freq[]
first, all test cases passed, but if I declare arr[]
first, two test cases failed. Can anyone explain why?
#include <bits/stdc++.h>
using namespace std;
int freq[10004]; //This passes
int arr[10004]; //for all test cases
//int arr[10004]; but this fails
//int freq[10004]; for two test cases...why??
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
memset(freq, 0, sizeof(freq));
int m, n, ansi, val;
cin >> m >> n;
for (int j = 0; j < n; j++)
{
cin >> arr[j];
freq[arr[j]]++;
}
for (int j = 0; j < n; j++)
{
freq[arr[j]]--;
if (freq[m - arr[j]] > 0)
{
ansi = j;
val = m - arr[j];
cout << j + 1;
break;
}
freq[arr[j]]++;
}
for (int j = ansi + 1; j < n; j++)
{
if (val == arr[j])
{
cout << " " << j + 1 << endl;
break;
}
}
}
return 0;
}