3
4 7 8
12 52 56 8
2 10 2
21 75
2 5 8
10 51
0
18
9
*** stack smashing detected ***: terminated
Aborted (core dumped)
Process returned 134 (0x86) execution time : 1.402 s
Press ENTER to continue.
I am having above error in this question code goes here:-
#include <bits/stdc++.h>
using namespace std;
/*
[1,2,3,4,5,6,7]
1 2 3 4 5 6 7 */
/*
void color(int arr[],int start,int endd){
for(int i = start-1;i<endd;++i)
{
arr[i]=1; // 1 means covered and not safe
}
}*/
int main()
{
long t;
cin>>t;
for(int k = 0;k<t;++k)
{ int A[100] = {0};
int M,x,y;
cin>>M>>x>>y;
int cpos=0;
for(int i=0;i<M;++i)
{
cin>>cpos;
int st = cpos-(x*y);
if(st<1){
st =1;
}
int en = cpos+(x*y);
//color(A,st,en);
for(int i = st-1;i<en;++i)
{
A[i]=1; // 1 means covered and not safe
}
}
cout<<count(A,A+100,0)<<"\n";
}
}
Explanation:- for possible ranges of cops made the corresponding locations = 1 and counted the positions where safe houses are present.
#include <bits/stdc++.h>
using namespace std;
int A[100];
int main()
{ cin.tie(NULL);
cout.sync_with_stdio(false);
long t;
long long M,x,y,st,en;
long long cpos=0;
cin>>t;
try{
for(int k = 0;k<t;++k)
{ for(int i =0;i<100;++i){A[i]=0;}
cin>>M>>x>>y;
cpos=0;
for(int i=0;i<M;++i)
{
cin>>cpos;
st = cpos-(x*y);
if(st<1){
st =1;
}
en = cpos+(x*y);
for(int i = st-1;i<en;++i)
{
A[i]=1; // 1 means covered and not safe
}
}
cout<<count(A,A+100,0)<<"\n";
}
}
catch(int x){
}
}
SIR is this work around correct. I initialized array outside and this time looped to make all zero