- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 number of global and local inversions are same or not in Python

Suppose we have a list of distinct numbers called nums. Here a global inversion is when there's indices i < j such that nums[i] > nums[j]. And local inversion is when there is an index i and i + 1 such that nums[i] > nums[i + 1]. We have to check whether the number of global inversions is equal to the number of local inversions or not.

So, if the input is like nums = [3, 2, 4], then the output will be True, as the indices 0 and 1 are both a global and local inversion.

To solve this, we will follow these steps −

- l := size of nums
- for i in range 0 to l - 3, do
- for j in range i + 2 to l-1, do
- if nums[i] > nums[j], then
- return False

- if nums[i] > nums[j], then

- for j in range i + 2 to l-1, do
- return True

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): l = len(nums) for i in range(l - 2): for j in range(i + 2, l): if nums[i] > nums[j]: return False return True ob = Solution() nums = [3, 2, 4] print(ob.solve(nums))

## Input

[3, 2, 4]

## Output

True

- Related Questions & Answers
- Global and Local Inversions in C++
- Program to check whether leaves sequences are same of two leaves or not in python
- Program to check all values in the tree are same or not in Python
- C Program to check if two strings are same or not
- Program to check whether all leaves are at same level or not in Python
- Global and Local Variables in Python?
- C# program to check whether two sequences are the same or not
- Program to check same value and frequency element is there or not in Python
- Program to check a number is ugly number or not in Python
- Program to check a number is power of two or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether given number is Narcissistic number or not in Python
- Program to check strings are rotation of each other or not in Python
- Python program to check if a number is Prime or not
- Python program to check a number n is weird or not

Advertisements