- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python
Suppose we have a list of numbers called nums, where n elements are present. We have to chesk whether we can make a list with first n natural numbers either in increasing or decreasing fashion, like [1, 2, ..., n] or [n, n - 1, ..., 1] by shifting nums to the right any number of times or not.
So, if the input is like nums = [5,6,1,2,3,4], then the output will be True, because we can shift them four times to make the array [1,2,3,4,5,6]
To solve this, we will follow these steps −
- n := size of nums
- for i in range 1 to n - 1, do
- if |nums[i - 1] - nums[i]| is not 1 and |nums[i - 1] - nums[i]| is not n-1, then
- return False
- if |nums[i - 1] - nums[i]| is not 1 and |nums[i - 1] - nums[i]| is not n-1, then
- return True
Example
Let us see the following implementation to get better understanding −
def solve(nums): n = len(nums) for i in range(1, n): if abs(nums[i - 1] - nums[i]) != 1 and abs(nums[i - 1] - nums[i]) != n - 1: return False return True nums = [5,6,1,2,3,4] print(solve(nums))
Input
[5,6,1,2,3,4]
Output
True
- Related Articles
- Program to check whether list is strictly increasing or strictly decreasing in Python
- Strictly increasing or decreasing array - JavaScript
- Program to check strings are rotation of each other or not in Python
- Program to check some elements in matrix forms a cycle or not in python
- Program to check whether every rotation of a number is prime or not in Python
- Python program to check a number n is weird or not
- Building a lexicographically increasing sequence of first n natural numbers in JavaScript
- Python Program for cube sum of first n natural numbers
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- How to check if two numbers (m,n) are amicable or not using Python?
- C++ Program to check cats words are right or not with colored hats
- Program to check whether we can get N queens solution or not in Python
- Python Program for Sum of squares of first n natural numbers
- Sum of first N natural numbers which are divisible by X or Y
- Program to check we can form array from pieces or not in Python

Advertisements