The following C++ code when compiled gives the error:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
const int N = 5005;
int n, k;
int a[N], prev[N];
int cache[N][N];
int dp(int idx, int take)
{
if(take < 0)
return -1e9;
if(idx == 0)
return 0;
int &ans = cache[idx][take];
if(ans != -1)
return ans;
ans = dp(idx - 1, take);
ans = max(ans, idx - prev[idx] + dp(prev[idx], take - 1));
return ans;
}
int32_t main()
{
IOS;
memset(cache, -1, sizeof(cache));
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1, a+n+1);
int l = 1;
for(int r=1;r<=n;r++)
{
while(a[r] - a[l] > 5)
l++;
prev[r] = l - 1;
}
int ans = dp(n, k);
cout<<ans;
return 0;
}
//AshishGup
Invocation failed [COMPILATION_ERROR] Can't compile file: program.cpp:
In function 'int dp(int, int)': program.cpp:23:23: error: reference to 'prev' is ambiguous
ans = max(ans, idx - prev[idx] + dp(prev[idx], take - 1));
but as soon as the name of the array is changed from prev to prv it works fine. What is happening?