惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
C
Cisco Blogs
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
S
Security Affairs
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
W
WeLiveSecurity
T
Tenable Blog
L
LINUX DO - 最新话题
T
Tor Project blog
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
爱范儿
爱范儿
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Y
Y Combinator Blog
I
Intezer
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
S
Securelist
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
量子位
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Scott Helme
Scott Helme
H
Help Net Security
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
I
InfoQ
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
N
News | PayPal Newsroom
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
LeetCode Solution: 1752. Check if Array Is Sorted and Rotated
Vansh Aggarw · 2026-05-24 · via DEV Community

🔄 LeetCode 1752: Can You Un-Rotate This Array? (A Beginner's Guide)

Hey there, fellow coders! 👋 Vansh2710 here, ready to demystify another exciting LeetCode challenge. Today, we're tackling problem 1752: "Check if Array Is Sorted and Rotated." Sounds a bit like a puzzle, right? Don't worry, we'll break it down piece by piece and uncover a super elegant solution!

This problem is a fantastic way to sharpen your array manipulation and logical thinking skills. It might seem tricky at first, but with a simple trick, it becomes incredibly straightforward. Let's dive in!


What's the Problem All About? 🧐

Imagine you have a perfectly sorted list of numbers, like [1, 2, 3, 4, 5].
Now, imagine you rotate it. This means you take some elements from the beginning and move them to the end, keeping their relative order.

For example, if you rotate [1, 2, 3, 4, 5] by 2 positions:

  1. [1, 2, 3, 4, 5]
  2. Take 1, 2 and move them to the end: [3, 4, 5, 1, 2]

The problem asks: Given an array nums, can we tell if it could have been originally sorted (non-decreasingly) and then rotated some number of times (even zero rotations)?

Key things to remember:

  • Non-decreasing order: [1, 2, 2, 3] is sorted. [3, 2, 1] is not.
  • Rotation: Can be any number of positions, including 0 (no rotation).
  • Duplicates are allowed: This is an important detail! [3, 4, 5, 5, 1, 2] could be [1, 2, 3, 4, 5, 5] rotated.

Let's look at examples:

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

    • Is [1, 2, 3, 4, 5] sorted? Yes.
    • Can [1, 2, 3, 4, 5] be rotated to get [3, 4, 5, 1, 2]? Yes, rotate by 2 positions.
    • Output: true
  • nums = [2, 1, 3, 4]

    • If sorted, it would be [1, 2, 3, 4].
    • Can [1, 2, 3, 4] be rotated to [2, 1, 3, 4]? No. The 2, 1 part breaks the sorted order that a single rotation would maintain.
    • Output: false
  • nums = [1, 2, 3]

    • Is it sorted? Yes.
    • Can it be rotated to get [1, 2, 3]? Yes, 0 rotations.
    • Output: true

The "Aha!" Moment - Our Intuition ✨

Think about a truly sorted array like [1, 2, 3, 4, 5]. If you go from left to right, nums[i-1] is always less than or equal to nums[i]. There are no "drops" or "descents" in value.

Now, consider a rotated sorted array: [3, 4, 5, 1, 2].
If you scan this, you'll see:

  • 3 <= 4 (OK)
  • 4 <= 5 (OK)
  • 5 > 1 (AHA! A descent!)
  • 1 <= 2 (OK)

Notice something? There's only one place where the non-decreasing order is broken: 5 followed by 1. This "break" is exactly where the rotation happened! The elements [1, 2] were moved from the beginning to the end, creating this single drop in value.

What if there are two descents? For example, [2, 1, 3, 4].

  • 2 > 1 (Descent 1)
  • 1 <= 3 (OK)
  • 3 <= 4 (OK) Here, we have one descent. What about the wrap-around? 4 > 2? No. Actually, if the original array was [1,2,3,4], and we rotated it, we would never get [2,1,3,4]. A single rotation implies that all elements after the rotation point are smaller than all elements before it. The only "break" can be at the rotation point.

So, the core intuition is: A sorted and rotated array will have at most ONE "descent" (where nums[i-1] > nums[i]) when you iterate through it.

Wait, what about the wrap-around?
Consider [4, 5, 1, 2, 3].
4 <= 5 (OK)
5 > 1 (Descent!)
1 <= 2 (OK)
2 <= 3 (OK)
Here, we have one descent. But also, nums[n-1] (which is 3) is less than nums[0] (which is 4). This comparison also forms part of the "break" in sorted order, conceptually wrapping around the array. Our descent counter needs to account for this.

Revised Intuition: Count the number of times nums[i-1] > nums[i]. This count should be at most 1, including the wrap-around comparison between the last and first elements.


Step-by-Step Approach 🚶‍♂️

Let's formalize our intuition into a clear algorithm:

  1. Initialize a counter: We'll need a variable, let's call it descentCount, and set it to 0. This counter will track how many times we find nums[i-1] > nums[i].

  2. Iterate through the array: Loop from the second element (i = 1) up to the end of the array (nums.size() - 1).

    • In each iteration, compare the current element (nums[i]) with the previous one (nums[i-1]).
    • If nums[i-1] > nums[i], it means we've found a "descent" or a break in the non-decreasing order. Increment descentCount.
  3. Check the wrap-around case: After the loop finishes, we need to consider the connection between the last element and the first element. In a rotated sorted array, if there was a rotation, the last element might be greater than the first element (e.g., in [3, 4, 5, 1, 2], nums[4] (which is 2) is less than nums[0] (which is 3). This doesn't add a descent.
    However, in [1,2,3] (0 rotations), nums[2] (3) is greater than nums[0] (1).
    Let's re-evaluate: The descent is where the sequence decreases.

    • [3, 4, 5, 1, 2] -> 5 > 1 (1 descent)
      • nums[n-1] (2) is not greater than nums[0] (3).
    • [1, 2, 3] -> No descents.
      • nums[n-1] (3) is not greater than nums[0] (1).
    • [2, 1, 3, 4] -> 2 > 1 (1 descent)
      • nums[n-1] (4) is not greater than nums[0] (2).

    My logic for the wrap-around check in the solution code is if (nums[n-1] > nums[0]). Let's trace it with examples:

    • [3,4,5,1,2] (n=5)
      • Loop: (5 > 1) -> descentCount = 1
      • Wrap-around: nums[4] (2) > nums[0] (3) is false.
      • Total descentCount = 1. Return true. Correct.
*   `[2,1,3,4]` (n=4)
    *   Loop: `(2 > 1)` -> `descentCount = 1`
    *   Wrap-around: `nums[3]` (4) `>` `nums[0]` (2) is `true`. -> `descentCount = 2`.
    *   Total `descentCount = 2`. Return `false`. Correct. (Because original `[1,2,3,4]` cannot be rotated to `[2,1,3,4]`. It has two "breaks" if you consider it circular: `2->1` and `4->2` (conceptual circular link)).

*   `[1,2,3]` (n=3)
    *   Loop: No descents. `descentCount = 0`.
    *   Wrap-around: `nums[2]` (3) `>` `nums[0]` (1) is `true`. -> `descentCount = 1`.
    *   Total `descentCount = 1`. Return `true`. Correct. (A sorted array is considered a 0-rotated sorted array).

This wrap-around check for `nums[n-1] > nums[0]` cleverly handles the case where the "rotation point" effectively exists between the last and first element *if the array was originally sorted*. If `[1,2,3]` is seen as `1,2,3,1` (circular), then `3 > 1` is a descent. This means a perfectly sorted array will register 1 descent with this method.

Enter fullscreen mode Exit fullscreen mode

  1. Final Check: After iterating through the array and checking the wrap-around, if descentCount is less than or equal to 1, it means the array could have been sorted and rotated. Return true. Otherwise, if descentCount is greater than 1, it means there are too many "breaks" in the sorted order for it to be a single rotation of a sorted array. Return false.

Show Me the Code! 💻

Here's the C++ implementation of this logic:

#include <vector> // Don't forget to include necessary headers!
#include <iostream> // For potential testing, though not strictly part of the solution

class Solution {
public:
    bool check(std::vector<int>& nums) {
        int count = 0; // Initialize descent counter
        int n = nums.size(); // Get the size of the array

        // Iterate from the second element to compare with the previous
        for (int i = 1; i < n; i++) {
            // If the previous element is greater than the current, it's a descent
            if (nums[i-1] > nums[i]) {
                count++;
            }
        }

        // Handle the wrap-around case: compare the last element with the first
        // If nums[n-1] is greater than nums[0], it means a "descent" occurs
        // conceptually between the end and the beginning of the array.
        // E.g., for [1,2,3], nums[2](3) > nums[0](1) is true.
        // For [3,4,5,1,2], nums[4](2) > nums[0](3) is false.
        if (nums[n-1] > nums[0]) {
            count++;
        }

        // A sorted and rotated array can have at most one "descent" (break).
        // This includes the wrap-around check, meaning a perfectly sorted array
        // will result in count = 1 due to the wrap-around check.
        return count <= 1;
    }
};

Enter fullscreen mode Exit fullscreen mode


Complexity Analysis ⏱️🚀

Let N be the number of elements in the nums array.

  • Time Complexity: O(N)

    • We iterate through the array once in the for loop, which takes O(N) time.
    • All other operations (initialization, size() call, final comparison) take constant time, O(1).
    • Therefore, the total time complexity is dominated by the loop, making it O(N). This is highly efficient as we only need to pass through the array once.
  • Space Complexity: O(1)

    • We only use a few extra variables (count, n, i) to store our state, regardless of the input array size.
    • No auxiliary data structures (like new arrays or hash maps) are used that scale with the input size.
    • Thus, the space complexity is constant, O(1).

Key Takeaways ✨

  • Spotting the "Breaks": The most crucial insight is that a sorted and rotated array will have at most one point where the non-decreasing order is violated.
  • The Wrap-Around is Tricky: Don't forget the connection between the last element and the first! This is where many solutions go wrong. Our approach correctly includes this as another potential "descent."
  • Simple is Often Best: This problem could tempt you into complex sorting or array manipulation, but a single pass and a counter prove to be the most elegant and efficient solution.

This approach is clean, efficient, and demonstrates a good understanding of array properties! Keep practicing, and you'll master these patterns in no time. Happy coding!


Author Account: Vansh2710
Time Published: 2026-05-23 23:46:54