/**
* Problem: LeetCode 1752 - Check if Array Is Sorted and Rotated
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public boolean check(int[] nums) {
int count = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
// Check if there is an inversion between adjacent elements cyclically
if (nums[i] > nums[(i + 1) % n]) {
count++;
}
}
// A valid sorted and rotated array has at most 1 drop point
return count <= 1;
}
}
"""
Problem: LeetCode 1752 - Check if Array Is Sorted and Rotated
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def check(self, nums: List[int]) -> bool:
count = 0
n = len(nums)
for i in range(n):
if nums[i] > nums[(i + 1) % n]:
count += 1
return count <= 1
/**
* Problem: LeetCode 1752 - Check if Array Is Sorted and Rotated
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
class Solution {
public:
bool check(vector<int>& nums) {
int count = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] > nums[(i + 1) % n]) {
count++;
}
}
return count <= 1;
}
};
/**
* Problem: LeetCode 1752 - Check if Array Is Sorted and Rotated
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
var check = function(nums) {
let count = 0;
const n = nums.length;
for (let i = 0; i < n; i++) {
if (nums[i] > nums[(i + 1) % n]) {
count++;
}
}
return count <= 1;
};