“”" int t;
cin>>t;
vectorv1,v2;
while(t–)
{
int a,b;
cin>>a>>b;
if(a>b)
v1.push_back(a-b);
else
v2.push_back(b-a);
}
int m1=*max_element(v1.begin(),v1.end());
int m2=*max_element(v2.begin(),v2.end());
if(m1>m2)
cout<<“1”<<" “<<m1;
else
cout<<“2”<<” “<<m2;
}
“””
Why am I getting SIGTSTP . can anyone help me?
The reason is very clear. Let’s say your test case look like
1
5
6
…In this case your v1 vector is empty and you are trying to fetch maximum element from your empty vector…That’s why you get SEGMENTATION FAULT.
Just try with this test case, you will get your answer
2
5
6
8
5
Thanks a lot…
int main() {
// your code goes here
int t;
cin>>t;
vectorv1,v2;
v1.push_back(0);
v2.push_back(0);
while(t–)
{
int a,b;
cin>>a>>b;
if(a>b)
v1.push_back(a-b);
else
v2.push_back(b-a);
}
int m1,m2;
if(v1.size()>1)
m1=*max_element(v1.begin(),v1.end());
else
m1=0;
if(v2.size()>1)
m2=*max_element(v2.begin(),v2.end());
else
m2=0;
if(m1>m2)
cout<<"1"<<" "<<m1;
else
cout<<"2"<<" "<<m2;
}
I have solved this issue. But I am getting this error again. this code is also working for custom inputs but not working without custom input and giving error.
I am new in programming so please tell me where to learn code better. Suggest me any YouTube channel or website.
Can you share question link
Bro, why are you running without custom input ? Never run a code without input in cc ide.
when i am submitting it is giving me wrong answer. In case of custom input it is giving proper output.
Then you are missing some corner cases and change the heading from SIGSTP to WA
Share your submission link or format your code. Use ``` symbol at the start and the end of code
thanks bro.