From dee9ac73cd857ae7b0412b801d58502c9010379b Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Sat, 19 Nov 2022 00:02:59 +0400 Subject: [PATCH] feat: add Check if Array Is Sorted and Rotated (#1141) * add Check if Array Is Sorted and Rotated * Update 1752.c add new line * Rename README.md to DIRECTORY.md Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 1 + leetcode/src/1752.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 leetcode/src/1752.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 56051ab4..b043ca44 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -95,4 +95,5 @@ | 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c) | Easy | | 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy | | 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy | +| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/1752.c b/leetcode/src/1752.c new file mode 100644 index 00000000..492a82b4 --- /dev/null +++ b/leetcode/src/1752.c @@ -0,0 +1,18 @@ +bool check(int* nums, int numsSize){ + if (numsSize == 1) { + return true; + } + + bool wasShift = false; + for(int i = 1; i < numsSize; i++) { + if (nums[i - 1] > nums[i]) { + if (wasShift) { + return false; + } + + wasShift = true; + } + } + + return !wasShift || nums[0] >= nums[numsSize-1]; +}