+ 2
help in c++,i have mention the location of error
input are 5 2000000 1000000 2 3 1000000 4 error is ocurring in bool a[1000005] if i make bool a[2000005] it works #include <iostream> using namespace std; int printhi(int ar[], int k,int n) { bool a[1000005]={0};int temp;//if i make 2000005 it works for(int i=0;i<n;i++) { temp=k-ar[i]; if(temp>=0&&a[temp]==1) {cout<<"YES"; return 0; } a[ar[i]]=1; } cout<<"NO"; return 0; } int main() { int n,k; cin>>n>>k; int ar[n]; for(int i=0;i<n;i++)cin>>ar[i]; printhi(ar,k,n); }
2 odpowiedzi
+ 2
index out of range, here's why:
k = 2000000
temp= k(2000000) - ar[1](2) = 1999998
a[temp (1999998)] is out of index
this is causing the problem
+ 1
thanku Sreejith