🚀 Welcome to another LeetCode DSA tutorial! In this video, we tackle LeetCode 1752 - Check if Array is Sorted and Rotated using an optimized approach that beats 100% of submissions! 🔥
💡 Problem Statement:
We are given an array nums, and we need to check if it was originally sorted in non-decreasing order and then rotated some number of positions. If yes, return true, else return false.
📌 Example Walkthrough:
Input: [3, 4, 5, 1, 2]
Output: true
Explanation: Originally sorted array [1, 2, 3, 4, 5] is rotated.
Input: [2, 1, 3, 4]
Output: false
Explanation: The array is neither sorted nor a rotated version of a sorted array.
📌 Approach & Explanation:
✔️ We traverse the array and count inversions.
✔️ If we find more than one inversion, the array is not sorted and rotated.
✔️ Since it's a circular array, we use (i+1) % n to compare the last and first elements.
✔️ Time Complexity: O(N), Space Complexity: O(1).
📌 Why This Works in Interviews?
👉 Optimized & Concise: Only one loop with O(N) complexity!
👉 Real-World Application: Used in circular buffer problems.
👉 Handles Edge Cases: Single elements, already sorted arrays, and multiple rotations!
LeetCode DSA Playlist: • Crack LeetCode Using Java
GeeksforGeeks DSA Playlist: • GeeksforGeeks DSA Solutions in Java | Comp...
Wings 1: • Wings 1
Let’s grow together! I’m open to collaborations and new ideas. Connect with me on:
🌐 LinkedIn: / rahul-vijayan-682a12194
🐦 Profile: https://rahulvijayan.in/
📸 Instagram: / rv_official_yt
🌟 GitHub: https://github.com/Rahulvijayan2291/
💡LeetCode: https://leetcode.com/u/rahulvijayan2291/
🚀 Don't Forget to LIKE 👍, SUBSCRIBE 🔔, and COMMENT 💬 Below!