<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhinav Yadav on Medium]]></title>
        <description><![CDATA[Stories by Abhinav Yadav on Medium]]></description>
        <link>https://medium.com/@abhinavay2003?source=rss-bf17459b8ccc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MeeNEmKl0ujIN6uiV25ufg.jpeg</url>
            <title>Stories by Abhinav Yadav on Medium</title>
            <link>https://medium.com/@abhinavay2003?source=rss-bf17459b8ccc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 18:22:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhinavay2003/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Explaination : Check if Array Is Sorted and Rotated]]></title>
            <link>https://medium.com/@abhinavay2003/explaination-check-if-array-is-sorted-and-rotated-d112830287cb?source=rss-bf17459b8ccc------2</link>
            <guid isPermaLink="false">https://medium.com/p/d112830287cb</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[easy-explanation]]></category>
            <category><![CDATA[leetcode-solution]]></category>
            <dc:creator><![CDATA[Abhinav Yadav]]></dc:creator>
            <pubDate>Fri, 08 Sep 2023 16:15:42 GMT</pubDate>
            <atom:updated>2023-09-08T16:18:21.375Z</atom:updated>
            <content:encoded><![CDATA[<h3>Explaination : Check if Array Is Sorted and Rotated</h3><h3><strong>Solving the “Check if Array Is Sorted and Rotated” LeetCode Easy Problem</strong></h3><p>In this blog post, we’ll explore a common coding problem: <a href="https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/description/">checking whether an array was originally sorted in non-decreasing order and then rotated some number of positions.</a> <em>We will discuss the problem statement, provide example test cases, and outline an approach to solve it.</em></p><h4><strong>Example Test Cases:</strong></h4><p>Let’s consider a few example test cases to better understand the problem:</p><blockquote><em>Input: </em><em>nums = [3,4,5,1,2] Output: </em><em>true</em></blockquote><p><em>Explanation: The original sorted array is </em><em>[1,2,3,4,5], and it was rotated by 3 positions to obtain </em><em>[3,4,5,1,2].</em></p><blockquote><em>Input: </em><em>nums = [2,1,3,4] Output: </em><em>false</em></blockquote><p><em>Explanation: No sorted array exists once rotated that can result in the given </em><em>nums.</em></p><blockquote><em>Input: </em><em>nums = [1,2,3] Output: </em><em>true</em></blockquote><p><em>Explanation: The array is perfectly sorted, with no rotation needed.</em></p><h4>Approach to Solve:</h4><p>To solve this problem, we can use a simple observation: in a sorted and rotated array, there will be <strong>at most one pair </strong>of adjacent elements where the first element is greater than the second. This pair represents the point of rotation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n07ki6TpMHRf4_Y5i8q1TQ.png" /></figure><h4>Here’s a step-by-step approach to implement this:</h4><ol><li>Initialize a variable rotationCount to 0, which will represent the number of rotations needed.</li><li>Iterate through the nums array from left to right.</li><li>For each pair of adjacent elements nums[i] and nums[i+1], if nums[i] is greater than nums[i+1], increment rotationCount by 1.</li><li>After iterating through the entire array, check the value of rotationCount.</li><li>If rotationCount is 0, it means the array was not rotated at all, so return true as it&#39;s already sorted.</li><li>If rotationCount is 1, it means the array was rotated, so return true.</li><li>If rotationCount is greater than 1, it means there are multiple pairs where the first element is greater than the second, indicating that the array cannot be sorted and rotated to match the original sorted array. In this case, return false.</li></ol><h4>Java Code :</h4><pre>public class Solution {<br>    public boolean check(int[] nums) {<br>        int n = nums.length;<br>        int rotationCount = 0; // Count of elements out of order<br>        <br>        for (int i = 0; i &lt; n; i++) {<br>            if (nums[i] &gt; nums[(i + 1) % n]) {<br>                rotationCount++;<br>            }<br>            <br>            if (rotationCount &gt; 1) {<br>                return false; // More than one element out of order, can&#39;t be rotated sorted array<br>            }<br>        }<br>        <br>        return true; // At most one element out of order, can be rotated sorted array<br>    }<br>}</pre><blockquote>nums[i] &gt; nums[(i + 1) % n]</blockquote><ul><li>i + 1 calculates the index of the element that follows the current element.</li><li>% n takes the modulo (remainder) of the result by the length of the array n. This ensures that the index remains within the bounds of the array even if it exceeds the array length. For example, if i is the last element in the array (i.e., i == n - 1), (i + 1) % n will wrap around to the first element (i.e., index 0).</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d112830287cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>