My issue
help me to solve this problem
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
Problem Link: Array sorting Practice Coding Problem - CodeChef
help me to solve this problem
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
Problem Link: Array sorting Practice Coding Problem - CodeChef
@jaggadaku005
here plzz refer my c++ code for better understanding of the logic .
I have used simpler gcd function .
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
int ans=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(i+1!=a[i])
{
int d=abs(i+1-a[i]);
if(ans!=0)
ans=__gcd(ans,d);
else
ans=d;
}
}
cout<<ans<<endl;
}
}
but if array is already sorted then i can choose k to n-1
@jaggadaku005
its given in the question :-
ok thank you!