Help me in solving ARRSORT problem

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

@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;
    }
}
1 Like

but if array is already sorted then i can choose k to n-1

@jaggadaku005
its given in the question :-

  • It is guaranteed that the given permutation is not sorted.
1 Like

ok thank you!