Maximize M such that swapping arr[i] with arr[i+M] makes Array sorted
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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
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;
}
int main()
{
int P[] = { 4, 2, 3, 1 };
int N = sizeof (P) / sizeof (P[0]);
cout << maxValue(P, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
else
return gcd(b, a % b);
}
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;
}
public static void main(String[] args)
{
int P[] = { 4 , 2 , 3 , 1 };
int N = P.length;
System.out.println(maxVal(P, N));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return abs (a)
else :
return gcd(b, a % b)
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
P = [ 4 , 2 , 3 , 1 ]
N = len (P)
print (maxValue(P,N))
|
C#
using System;
public class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
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;
}
public static void Main( string [] args)
{
int []P = { 4, 2, 3, 1 };
int N = P.Length;
Console.WriteLine(maxVal(P, N));
}
}
|
Javascript
<script>
const gcd = (a, b) => {
if (b == 0)
return a;
return gcd(b, a % b);
}
const maxValue = (P, N) => {
let res = 0;
for (let i = 0; i < N; i++) {
res = gcd(res, Math.abs(P[i] - (i + 1)));
}
return res;
}
let P = [4, 2, 3, 1];
let N = P.length;
document.write(maxValue(P, N));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(log (min(a,b)))
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...