
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if it is possible to sort the array after rotating it in Python
Suppose we have a list of numbers called nums, we have to check whether we can sort nums or not by using rotation. By rotation we can shift some contiguous elements from the end of nums and place it in the front of the array.
So, if the input is like nums = [4,5,6,1,2,3], then the output will be True as we can sort by rotating last three elements and send them back to first.
To solve this, we will follow these steps −
- n := size of nums
- if nums is sorted, then
- return True
- otherwise,
- status := True
- for i in range 0 to n - 2, do
- if nums[i] > nums[i + 1], then
- come out from loop
- if nums[i] > nums[i + 1], then
- i := i + 1
- for k in range i to n - 2, do
- if nums[k] > nums[k + 1], then
- status := False
- come out from loop
- if nums[k] > nums[k + 1], then
- if status is false, then
- return False
- otherwise,
- if nums[n - 1] <= nums[0], then
- return True
- return False
- if nums[n - 1] <= nums[0], then
Example
Let us see the following implementation to get better understanding −
def solve(nums): n = len(nums) if all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)): return True else : status = True for i in range(n - 1) : if nums[i] > nums[i + 1] : break i += 1 for k in range(i, n - 1) : if nums[k] > nums[k + 1]: status = False break if not status: return False else : if nums[n - 1] <= nums[0]: return True return False nums = [4,5,6,1,2,3] print(solve(nums))
Input
[4,5,6,1,2,3]
Output
True
- Related Articles
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Check if it is possible to reach vector B by rotating vector A and adding vector C to its in Python
- Check if it is possible to survive on Island in Python
- Check if it is possible to transform one string to another in Python
- Check if it is possible to return to the starting position after moving in the given directions in C++
- Check if it is possible to serve customer queue with different notes in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Check if it is possible to form string B from A under the given constraint in Python
- Check if it possible to partition in k subarrays with equal sum in Python
- Check if it is possible to convert one string into another with given constraints in Python
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python

Advertisements