Codechef lunchtime april 2020 div 2 problem another bithday present

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…

@S19LPPP0202,
Please explain your approach, and also don’t just copy code like text here, you can see how untidy it looks, Use Coding Blocks IDE for sharing codes.

Next time I will.post it using ide …this time you plz clarify the doubt
I used the approach using 2 arrays where I sort the given array and if at that index element is not same in both the arrays then i store that index in a vector and when I have the vector then I check if the diff of consecutive elements of the vector is divisible by k…or not if it is not then. I break out of the loop and print no…but it is giving wrong ans…can u plz tell me why ??

@S19LPPP0202,
Please provide the link of the problem, and your submission, or save your working code on Coding Blocks IDE (make sure it compiles before sharing here) and share here.


link of ques :https://www.codechef.com/LTIME83B/problems/SHUFFLE
now
I used the approach using 2 arrays where I sort the given array and if at that index element is not same in both the arrays then i store that index in a vector and when I have the vector then I check if the diff of consecutive elements of the vector is divisible by k…or not if it is not then. I break out of the loop and print no…but it is giving wrong ans…tell me why ??

@S19LPPP0202,
Check your code on this,

Input:
1
4 2
3 4 1 2

Correct Output:
yes

Wrong output is coming in my code … correct my code and let me know where my approach was wrong

@S19LPPP0202,
Dry run the code with a pen and paper, we won’t spoon-feed you. I have told you the test case where your code fails, just dry run and realise what is wrong in your code. Me correcting your code will not help anyone. And I did told you where your approach is wrong, ITS THIS TESTCASE!!!.

if(divisor(abs(v2[i]-x),k)==0) line does not make sense, two adjacent indexes can have a difference not divisible by k, and still give a sorted array, like in this very test case. You would have known, if you would have tried to dry run the code.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.