- 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
Largest Gap in Python
Suppose we have a list of numbers called nums, we have to find the largest difference of two consecutive numbers in the sorted version of nums.
So, if the input is like [5, 2, 3, 9, 10, 11], then the output will be 4, as the largest gap between 5 and 9 is 4.
To solve this, we will follow these steps −
- n := sorted list nums
- ans := a new list
- for i in range 0 to size of n -2, do
- insert n[i+1]-n[i] at the end of ans
- return maximum of ans
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): n = sorted(nums) ans = [] for i in range(len(n)-1): ans.append(n[i+1]-n[i]) return max(ans) ob = Solution() nums = [5, 2, 3, 9, 10, 11] print(ob.solve(nums))
Input
[5, 2, 3, 9, 10, 11]
Output
4
- Related Articles
- Largest gap in an array in C++
- Binary Gap in Python
- Largest Number in Python
- Maximum Gap in C++
- Largest Unique Number in Python
- Largest Triangle Area in Python
- Largest Perimeter Triangle in Python
- Largest Rectangle in Histogram in Python
- Largest Values From Labels in Python
- What is air gap?
- Largest Number By Two Times in Python
- Largest product of contiguous digits in Python
- Largest Merge of Two Strings in Python
- Kth Largest Element in an Array in Python
- Kth Largest Element in a Stream in Python

Advertisements