- 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 find size of sublist where product of minimum of A and size of A is maximized in Python
Suppose we have a list of numbers called nums and another value pos. We have to find a sublist A of nums that includes the index pos such that (minimum of A) * (size of A) is maximized then return the value.
So, if the input is like nums = [-2, 2, 5, 4] pos = 3, then the output will be 8, as the best sublist is [5, 4], because (5, 4) = 4 and its size is 2 we have 4 * 2 = 8.
To solve this, we will follow these steps −
ans := A[pos], m := A[pos]
i := pos, j := pos
do the following for i in range 0 to size of A - 1, do
left := A[i - 1] if i - 1 >= 0 otherwise -inf
right := A[j + 1] if j + 1 < size of A otherwise -inf
if left >= right, then
i := i - 1
m := minimum of m and A[i]
otherwise,
j := j + 1
m := minimum of m and A[j]
ans := maximum of ans and (m *(j - i + 1))
return ans
Example
Let us see the following implementation to get better understanding −
class Solution: def solve(self, A, pos): NINF = float("-inf") ans = m = A[pos] i = pos j = pos for _ in range(len(A) - 1): left = A[i - 1] if i - 1 >= 0 else NINF right = A[j + 1] if j + 1 < len(A) else NINF if left >= right: i -= 1 m = min(m, A[i]) else: j += 1 m = min(m, A[j]) ans = max(ans, m * (j - i + 1)) return ans ob = Solution() nums = [-2, 2, 5, 4] pos = 3 print(ob.solve(nums, pos))
Input
[-2, 2, 5, 4], 3
Output
8
- Related Articles
- Program to find the size of the longest sublist where car speed is constant in python
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find largest average of sublist whose size at least k in Python
- Program to find out the minimum size of the largest clique in a graph (Python)
- Python program to Find the size of a Tuple
- Program to find maximum size of any sequence of given array where every pair is nice in Python
- Find size of a list in Python
- C Program to find size of a File
- Program to find size of each partition of a list where each letter appears at most one piece in Python
- C++ program to find minimum vertex cover size of a graph using binary search
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Maximum product of a triplet (subsequence of size 3) in array in C++ Program.
- Find maximum (or minimum) sum of a subarray of size k in C++
- Program to find number of increasing subsequences of size k in Python
- Program to find max values of sublists of size k in Python
