# LeetCode: Check If Array Is Sorted And Rotated Solution

## Approach

Count the number of increasing subarray, call this parts

• if
parts > 1
: totally false
• if
parts === 0
: totally true
• if
parts = 1
: check if the last elem of second part lte the first elem of first part

## Implementation

`.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number[]} nums3 * @return {boolean}4 */5var check = function (nums) {6  const N = nums.length7  let parts = 08  for (let i = 0; i < N - 1; i++) {9    if (nums[i] > nums[i + 1]) {10      parts++11    }12  }13  if (parts > 1) {14    return false15  }16  return parts === 0 || nums[0] >= nums[N - 1]17}`

leetcode

array

## Next Post

LeetCode: Duplicate Zeros

Feb 8, 2021

## Previous Post

LeetCode: Remove Element

Feb 8, 2021

Splice

Search Posts