# Maximize M such that swapping arr[i] with arr[i+M] makes Array sorted

• Last Updated : 10 Jan, 2023

Given a permutation arr[] of size N which is unsorted and contains all integers from 1 to N exactly once, the task is to find the maximum value of M such that performing a swap of arr[i] with arr[i+M] for any i in the range [1, N-M] makes the array sorted.

Examples:

Input:  arr[] = {4, 2, 3, 1}
Output: 3
Explanation: Permutation can be sorted by swapping
arr[1] and arr[4], where M = 3.
So, the maximum possible value of M is 3.
It can be shown that we cannot sort the permutation for any M > 3.

Input: arr[] = {3, 4, 2, 1}
â€¨Output: 1

Approach: To solve the problem follow the below idea:

• Here arr[i] and arr[i+M]  are swapped for sorting.
• First check the element at index i and then take absolute difference of element at index i with (i+1), i.e. arr[i] and (i+1) and repeat this for all index of P array. [This denotes how much shifting we need to perform for this element]
• Then find the greatest common divisor(GCD) of absolute differences of all elements in P. The obtained value of gcd will be the maximum value of M. [Otherwise, some elements will get misplaced]

Follow the given steps to solve the problem:

• Find the absolute difference between arr[i] and (i+1), for every i in the range [0, N).
• Then find the greatest common divisor(GCD) of all the values found in the above step.
• The final value of GCD is the maximum possible value of M, that we can obtain.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach` `#include ``using` `namespace` `std;` `// Recursive function to return gcd of a and b``int` `gcd(``int` `a, ``int` `b)``{``    ``if` `(b == 0)``        ``return` `a;``    ``return` `gcd(b, a % b);``}` `// Function to find the maximum value of m``int` `maxValue(``int` `P[], ``int` `N)``{``    ``int` `res = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {``        ``res = gcd(res, ``abs``(P[i] - (i + 1)));``    ``}``    ``return` `res;``}` `// Driver code``int` `main()``{``    ``int` `P[] = { 4, 2, 3, 1 };``    ``int` `N = ``sizeof``(P) / ``sizeof``(P[0]);` `    ``// Function call``    ``cout << maxValue(P, N);``    ``return` `0;``}`

## Java

 `// Java code to implement the approach` `import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``// Recursive function to return gcd of a and b``    ``static` `int` `gcd(``int` `a, ``int` `b)``    ``{``        ``if` `(b == ``0``)``            ``return` `a;``        ``else``            ``return` `gcd(b, a % b);``    ``}` `    ``// Function to find the maximum value of M``    ``public` `static` `int` `maxVal(``int` `P[], ``int` `N)``    ``{``        ``int` `res = ``0``;``        ``for` `(``int` `i = ``0``; i < N; i++) {``            ``int` `diff = P[i] - (i + ``1``);``            ``int` `x = Math.abs(diff);``            ``res = gcd(res, x);``        ``}``        ``return` `res;``    ``}``    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `P[] = { ``4``, ``2``, ``3``, ``1` `};``        ``int` `N = P.length;` `        ``// Function call``        ``System.out.println(maxVal(P, N));``    ``}``}`

## Python3

 `# Python3 code to implement the approach` `# Recursive function to return gcd of a and b``def` `gcd(a, b):``    ``if``(b ``=``=` `0``):``        ``return` `abs``(a)``    ``else``:``        ``return` `gcd(b, a ``%` `b)``      ` `# Function to find the maximum value of M      ``def` `maxValue(P,N):``  ``res ``=` `[]``  ``for` `i ``in` `range``(N):``     ``if` `abs``((P[i]``-``1``)``-``i) > ``0``:``         ``res.append(``abs``((P[i]``-``1``)``-``i))``  ``answer ``=`  `res[``0``]``  ``for` `i ``in` `range``(``len``(res)``-``1``):``      ``answer ``=` `gcd(answer,res[i``+``1``])``  ``return` `answer` `# Driver code``P ``=` `[ ``4``, ``2``, ``3``, ``1` `]``N``=``len``(P)``print``(maxValue(P,N))` `# This code is contributed by aarohirai2616.`

## C#

 `// C# code to implement the approach` `using` `System;` `public` `class` `GFG {` `    ``// Recursive function to return gcd of a and b``    ``static` `int` `gcd(``int` `a, ``int` `b)``    ``{``        ``if` `(b == 0)``            ``return` `a;``        ``else``            ``return` `gcd(b, a % b);``    ``}` `    ``// Function to find the maximum value of M``    ``public` `static` `int` `maxVal(``int` `[]P, ``int` `N)``    ``{``        ``int` `res = 0;``        ``for` `(``int` `i = 0; i < N; i++) {``            ``int` `diff = P[i] - (i + 1);``            ``int` `x = Math.Abs(diff);``            ``res = gcd(res, x);``        ``}``        ``return` `res;``    ``}``    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `[]P = { 4, 2, 3, 1 };``        ``int` `N = P.Length;` `        ``// Function call``        ``Console.WriteLine(maxVal(P, N));``    ``}``}` `// This code is contributed by AnkThon`

## Javascript

 ``

Output

`3`

Time Complexity: O(N * log(N))
Auxiliary Space: O(log (min(a,b)))

My Personal Notes arrow_drop_up