LeetCode 1752: Check if Array Is Sorted and Rotated – Java Solution Explained

Learn how to solve LeetCode 1752 Check if Array Is Sorted and Rotated using rotation logic and array traversal in Java. Includes brute force approach, optimized solution, intuition, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
LeetCode 1752: Check if Array Is Sorted and Rotated – Java Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 1752 – Check if Array Is Sorted and Rotated is a classic array observation problem that tests your understanding of:

  1. Sorted arrays
  2. Rotation logic
  3. Circular traversal
  4. Edge case handling
  5. Pattern recognition

At first, many developers overcomplicate this problem by trying to actually rotate arrays and compare them. However, the problem can be solved using a very elegant observation.

This problem is commonly asked in coding interviews because it evaluates:

  1. Logical thinking
  2. Array traversal skills
  3. Optimization ability
  4. Understanding of rotated arrays

Problem Link

🔗 https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/

Problem Statement

Given an array:

nums

Return:

true

if the array was originally sorted in non-decreasing order and then rotated some number of times.

Otherwise return:

false

Duplicates are allowed.

Understanding Rotation

Suppose the original sorted array is:

[1,2,3,4,5]

After rotation:

[3,4,5,1,2]

The array is still almost sorted except for one “breaking point”.

Key Observation

A sorted rotated array can have:

At most one decreasing pair

Example:

[3,4,5,1,2]

Breaking point:

5 > 1

Only once.

Invalid Example

[2,1,3,4]

Breaking points:

2 > 1

and circularly:

4 > 2

Two breaking points.

So answer is:

false

Brute Force Approach

Intuition

Try all possible rotations.

For every rotation:

  1. Rotate array
  2. Check if sorted
  3. If any rotation works → return true

Brute Force Algorithm

For every rotation count:

  1. Create rotated array
  2. Verify sorted order

If sorted:

return true

Else:

return false

Brute Force Complexity

Time Complexity

O(N²)

because each rotation requires traversal.

Space Complexity

O(N)

This solution:

  1. Finds rotation point
  2. Sorts array
  3. Rotates sorted array
  4. Compares with original

This is a valid simulation-based approach.

Java Solution

class Solution {

public boolean check(int[] nums) {

int[] arr = new int[nums.length];

int o = 0;

int mini = Integer.MIN_VALUE;

int temp = 0;

int maxnumind = 0;

for(int a : nums) {

arr[o] = a;

temp = mini;

mini = Math.max(mini, a);

if(mini != temp) {
maxnumind = o;
}

o++;
}

for(int i = 0; i < nums.length - 1; i++) {

if(nums[i] > nums[i + 1]) {
maxnumind = i;
}
}

int ro = nums.length - maxnumind - 1;

Arrays.sort(nums);

int[] rotarr = new int[nums.length];

for(int i = 0; i < nums.length; i++) {

rotarr[i] = nums[(i + ro) % nums.length];
}

for(int i = 0; i < arr.length; i++) {

if(rotarr[i] != arr[i]) {
return false;
}
}

return true;
}
}

Optimized Approach (Best Solution)

We do not need:

  1. Sorting
  2. Extra arrays
  3. Rotation simulation

We only count:

decreasing pairs

Optimized Intuition

For a valid rotated sorted array:

nums[i] > nums[i+1]

can happen only once.

Also check circular condition:

last element > first element

Optimized Java Solution


class Solution {

public boolean check(int[] nums) {

int count = 0;

for(int i = 0; i < nums.length; i++) {

if(nums[i] > nums[(i + 1) % nums.length]) {
count++;
}
}

return count <= 1;
}
}

Why This Works

If array is sorted and rotated:

  1. Sequence increases normally
  2. Only one position breaks order

If more than one break exists:

Not a rotated sorted array

Dry Run

Input

nums = [3,4,5,1,2]

Step 1

Compare adjacent elements:

3 < 4
4 < 5
5 > 1 ← breaking point
1 < 2
2 < 3 (circular)

Breaking points:

1

Valid.

Return:

true

Another Dry Run

Input

nums = [2,1,3,4]

Comparisons:

2 > 1 ← break
1 < 3
3 < 4
4 > 2 ← circular break

Breaking points:

2

Invalid.

Return:

false

Time Complexity Analysis

Time Complexity

O(N log N)

because of sorting.

Space Complexity

O(N)

extra arrays used.

Optimized Approach

Time Complexity

O(N)

single traversal.

Space Complexity

O(1)

Comparison of Approaches

ApproachTime ComplexitySpace Complexity
Rotation SimulationO(N log N)O(N)
Decreasing Pair CountO(N)O(1)

Interview Explanation

In interviews, explain:

A sorted rotated array can contain only one position where the order decreases. By counting such breaking points including circular comparison, we can determine validity in linear time.

This demonstrates:

  1. Pattern recognition
  2. Circular traversal understanding
  3. Optimization thinking

Common Mistakes

1. Forgetting Circular Check

Always compare:


nums[n-1] > nums[0]

using modulo.

2. Actually Rotating Arrays

Unnecessary and inefficient.

3. Using Strictly Increasing Logic

Duplicates are allowed.

So:

1,1,2,2

is valid.

FAQs

Q1. Why use modulo?

To compare:

last element with first element

circularly.

Q2. Why is only one break allowed?

Because rotation shifts sorted order only once.

Q3. Is sorting required?

No.

Observation-based traversal is enough.

Q4. Is this problem important for interviews?

Yes.

It tests:

  1. Array logic
  2. Rotations
  3. Optimization
  4. Observation skills

Related Problems

After mastering this problem, practice:

  1. Search in Rotated Sorted Array
  2. Find Minimum in Rotated Sorted Array
  3. Find Minimum in Rotated Sorted Array II

Conclusion

LeetCode 1752 is an excellent observation-based array problem.

It teaches:

  1. Rotated array logic
  2. Circular traversal
  3. Optimization techniques
  4. Pattern recognition

The key insight is:

A sorted rotated array can have at most one decreasing point.

Once you understand this observation, the optimized solution becomes extremely clean and efficient.

Ai Assistant Kas