
- 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
Program to find length of longest sublist with value range condition in Python
Suppose we have a list of numbers called nums, we have to find the length of the longest sublist where 2 * (minimum of sublist) > (maximum of sublist).
So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, because the sublist [6, 6, 4, 4] is the longest sublist that meet the given criteria (2*4) > 6.
To solve this, we will follow these steps −
- ret := 0
- minq := an empty double ended queue
- maxq := an empty double ended queue
- l := 0
- r := 0
- while r < size of nums, do
- n := nums[r]
- while minq is not empty and n < nums[last element of minq], do
- delete last element from minq
- insert r at the end of minq
- while maxq is not empty and n > nums[last element of maxq], do
- delete last element from maxq
- insert r at the end of maxq
- r := r + 1
- while l < r and nums[minq[0]] * 2 <= nums[maxq[0]], do
- if minq[0] is same as l, then
- left left item from minq
- if maxq[0] is same as l, then
- delete last item of maxq
- l := l + 1
- if minq[0] is same as l, then
- ret := maximum of ret and (r - l)
- return ret
Example
Let us see the following implementation to get better understanding −
from collections import deque def solve(nums): ret = 0 minq, maxq = deque(), deque() l, r = 0, 0 while r < len(nums): n = nums[r] while minq and n < nums[minq[-1]]: minq.pop() minq.append(r) while maxq and n > nums[maxq[-1]]: maxq.pop() maxq.append(r) r += 1 while l < r and nums[minq[0]] * 2 <= nums[maxq[0]]: if minq[0] == l: minq.popleft() if maxq[0] == l: maxq.popleft() l += 1 ret = max(ret, r - l) return ret nums = [10, 2, 6, 6, 4, 4] print(solve(nums))
Input
[10, 2, 6, 6, 4, 4]
Output
4
- Related Articles
- Program to find length of longest sublist with given condition in Python
- Program to find length of longest distinct sublist in Python
- Program to find length of longest consecutive sublist with unique elements in Python
- Program to find length of longest alternating inequality elements sublist in Python
- Program to find length of longest contiguous sublist with same first letter words in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Program to find length of longest sublist containing repeated numbers by k operations in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find length of longest sublist where difference between min and max smaller than k in Python
- Program to find length of longest path with even sum in Python
- Program to find length of longest matrix path length in Python
- Program to find length of contiguous strictly increasing sublist in Python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python

Advertisements