logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
13. Roman to Integer14. Longest Common Prefix20. Valid Parentheses26. Remove Duplicates from Sorted Array35. Search Insert Position69. Sqrt(x)70. Climbing Stairs88. Merge Sorted Array (In-Place)104. Maximum Depth of Binary Tree118. Pascal's Triangle121. Best Time to Buy and Sell Stock205. Isomorphic Strings242. Valid Anagram283. Move Zeroes345. Reverse Vowels of a String605. Can Place Flowers704. Binary Search796. Rotate String1071. Greatest Common Divisor of Strings1299. Replace Elements with Greatest Element on Right Side1431. Kids With the Greatest Number of Candies1614. Maximum Nesting Depth of the Parentheses1752. Check if Array Is Sorted and Rotated1768. Merge Strings Alternately1903. Largest Odd Number in String2965. Find Missing and Repeated Values
Code Scratch/leetcode/easy

1752. Check if Array Is Sorted and Rotated

1 min read
On this pageIntuition
IntuitionComplexityCode

Check if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero).


# Intuition

If the array is sorted then it will have 0 inversion, if a sorted array is rotated either clockwise or anti-clockwise direction the amount of inversion will be 1.

[1,2,3,4,5,6,7,8,9] => rotate clockwise 3 time => [7,8,9,1,2,3,4,5,6]
[1,2,3,4,5,6,7,8,9] => rotate anti-clockwise 3 time => [4,5,6,7,8,9,1,2,3]

In a sorted or rotated-sorted array, there can be at most one "drop" (i.e., an instance where an element is greater than its immediate successor). By checking for these drops, we can conclude whether the array satisfies the condition.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(n)\text{O}(n)O(n)

# Code

public boolean check(int[] nums) {
    // if the array is sorted the next immediate will always be larger
    // except at the point of rotation    
    int countInversion = 0;
    for (int i = 0; i < nums.length; ++i) {
        if( nums[i] > nums[ (i+1)%nums.length ] ) countInversion++; 
    }    
    return countInversion <= 1 ;
}
←Previous1614. Maximum Nesting Depth of the ParenthesesNext→1768. Merge Strings Alternately

On this page

IntuitionComplexityCode