Program to find size of sublist where product of minimum of A and size of A is maximized in Python

Given a list of numbers called nums and an index pos, we need to find a sublist that includes the element at index pos such that (minimum of sublist) × (size of sublist) is maximized.

For example, if nums = [-2, 2, 5, 4] and pos = 3, the best sublist is [5, 4] (indices 2-3). The minimum is 4, size is 2, so the product is 4 × 2 = 8.

Algorithm

We use a greedy approach to expand the sublist from the given position ?

  • Start with the element at pos as both answer and current minimum

  • Use two pointers i and j to track sublist boundaries

  • At each step, expand to the side with the larger adjacent element

  • Update the minimum and calculate the new product

  • Keep track of the maximum product found

Implementation

class Solution:
    def solve(self, nums, pos):
        NINF = float("-inf")
        ans = minimum = nums[pos]
        i = pos
        j = pos
        
        # Expand sublist one element at a time
        for _ in range(len(nums) - 1):
            left = nums[i - 1] if i - 1 >= 0 else NINF
            right = nums[j + 1] if j + 1 < len(nums) else NINF
            
            # Choose the larger adjacent element to expand to
            if left >= right:
                i -= 1
                minimum = min(minimum, nums[i])
            else:
                j += 1
                minimum = min(minimum, nums[j])
            
            # Update maximum product
            ans = max(ans, minimum * (j - i + 1))
        
        return ans

# Test the solution
solution = Solution()
nums = [-2, 2, 5, 4]
pos = 3
result = solution.solve(nums, pos)
print(f"Maximum product: {result}")
Maximum product: 8

How It Works

Let's trace through the example step by step ?

def solve_with_trace(nums, pos):
    NINF = float("-inf")
    ans = minimum = nums[pos]
    i = pos
    j = pos
    
    print(f"Start: sublist[{i}:{j+1}] = {nums[i:j+1]}, min = {minimum}, product = {ans}")
    
    for step in range(len(nums) - 1):
        left = nums[i - 1] if i - 1 >= 0 else NINF
        right = nums[j + 1] if j + 1 < len(nums) else NINF
        
        if left >= right:
            i -= 1
            minimum = min(minimum, nums[i])
            direction = "left"
        else:
            j += 1
            minimum = min(minimum, nums[j])
            direction = "right"
        
        product = minimum * (j - i + 1)
        ans = max(ans, product)
        
        print(f"Step {step + 1}: expand {direction}, sublist[{i}:{j+1}] = {nums[i:j+1]}")
        print(f"         min = {minimum}, size = {j-i+1}, product = {product}")
    
    return ans

nums = [-2, 2, 5, 4]
pos = 3
result = solve_with_trace(nums, pos)
print(f"\nFinal answer: {result}")
Start: sublist[3:4] = [4], min = 4, product = 4
Step 1: expand left, sublist[2:4] = [5, 4]
         min = 4, size = 2, product = 8
Step 2: expand left, sublist[1:4] = [2, 5, 4]
         min = 2, size = 3, product = 6
Step 3: expand left, sublist[0:4] = [-2, 2, 5, 4]
         min = -2, size = 4, product = -8

Final answer: 8

Key Points

  • The algorithm guarantees the sublist includes the element at pos

  • We always expand to the side with the larger adjacent element to maximize potential

  • Time complexity: O(n), Space complexity: O(1)

  • The greedy approach works because we want to include larger elements before smaller ones

Conclusion

This greedy algorithm efficiently finds the sublist that maximizes the product of minimum value and size. By expanding toward larger adjacent elements first, we ensure optimal results in linear time.

Updated on: 2026-03-25T13:53:07+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements