this doubt is related to the codechef lunchtime april 2020 div 2 problem another bithday present…
i have used the following approach but it is showing wrong answer…
plz tell me what is wromg in my logic and dont forward the link of editorial of the problems given on codechef site…
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int divisor(int n,int k)
{
if(n==1&&k!=1)
{
return 0;
}
if(n%k==0)
{
return 1;
}
return 0;
}
int32_t main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int a[n];
vectorv,v2;
for(int i=0;i<n;i++)
{
cin>>a[i];
v.push_back(a[i]);
}
sort(v.begin(),v.end());
int z=0;
for(int i=0;i<n;i++)
{
if(v[i]!=a[i])
{
v2.push_back(i);
z=1;
}
}
if(z!=1)
{
cout<<"yes"<<"\n";
}
else
{
int x=v2[0];
for(int i=1;i<v2.size();i++)
{
if(divisor(abs(v2[i]-x),k)==0)
{
cout<<"no"<<"\n";
z=0;
break;
}
x=v2[i];
}
if(z!=0)
{
cout<<"yes"<<"\n";
}
}
}
}
plz tell what is wrong in this approach and also testcases where this approach fails…