/**
* Problem: LeetCode 3942 - Minimum Operations to Sort a Permutation
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minOperations(int[] nums) {
int n = nums.length;
int best = Integer.MAX_VALUE;
int s = nums[0];
boolean asc = true;
for (int i = 0; i < n; i++) {
if (nums[i] != (i + s) % n) {
asc = false;
break;
}
}
if (asc) {
if (s == 0) return 0;
best = Math.min(best, Math.min(n - s, s + 2));
}
int c = nums[0];
boolean desc = true;
for (int i = 0; i < n; i++) {
if (nums[i] != ((c - i) % n + n) % n) {
desc = false;
break;
}
}
if (desc) {
int t = (c + 1) % n;
best = Math.min(best, Math.min(t, n - t) + 1);
}
return best == Integer.MAX_VALUE ? -1 : best;
}
}
"""
Problem: LeetCode 3942 - Minimum Operations to Sort a Permutation
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
best = float('inf')
# Check if array is cyclically ascending
s = nums[0]
asc = all(nums[i] == (i + s) % n for i in range(n))
if asc:
if s == 0:
return 0
best = min(best, n - s, s + 2)
# Check if array is cyclically descending
c = nums[0]
desc = all(nums[i] == ((c - i) % n + n) % n for i in range(n))
if desc:
t = (c + 1) % n
best = min(best, min(t, n - t) + 1)
return -1 if best == float('inf') else best
/**
* Problem: LeetCode 3942 - Minimum Operations to Sort a Permutation
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
/**
* Problem: LeetCode 3942 - Minimum Operations to Sort a Permutation
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int minOperations(vector<int>& nums) {
int n = nums.size();
int best = INT_MAX;
// Check if array is cyclically ascending
int s = nums[0];
bool asc = true;
for (int i = 0; i < n; i++) {
if (nums[i] != (i + s) % n) {
asc = false;
break;
}
}
if (asc) {
if (s == 0) return 0;
best = min(best, min(n - s, s + 2));
}
// Check if array is cyclically descending
int c = nums[0];
bool desc = true;
for (int i = 0; i < n; i++) {
if (nums[i] != ((c - i) % n + n) % n) {
desc = false;
break;
}
}
if (desc) {
int t = (c + 1) % n;
best = min(best, min(t, n - t) + 1);
}
return best == INT_MAX ? -1 : best;
}
};
/**
* Problem: LeetCode 3942 - Minimum Operations to Sort a Permutation
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var minOperations = function(nums) {
const n = nums.length;
let best = Infinity;
// Check if array is cyclically ascending
const s = nums[0];
let asc = true;
for (let i = 0; i < n; i++) {
if (nums[i] !== (i + s) % n) {
asc = false;
break;
}
}
if (asc) {
if (s === 0) return 0;
best = Math.min(best, n - s, s + 2);
}
// Check if array is cyclically descending
const c = nums[0];
let desc = true;
for (let i = 0; i < n; i++) {
if (nums[i] !== ((c - i) % n + n) % n) {
desc = false;
break;
}
}
if (desc) {
const t = (c + 1) % n;
best = Math.min(best, Math.min(t, n - t) + 1);
}
return best === Infinity ? -1 : best;
};