Maximize (nums[i] - 1) * (nums[j] - 1) over two different indices of an integer array

Quick Overview

Given an array of positive integers, choose two different indices and return the largest possible value of (nums[i] - 1) * (nums[j] - 1). The problem tests reasoning about how a product grows with its factors, handling repeated values at different positions, and writing a single linear scan.

Maximize (nums[i] - 1) * (nums[j] - 1) over two different indices of an integer array

Company: Samsung

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums`, choose two different indices `i` and `j` of that array. Return the maximum value of `(nums[i] - 1) * (nums[j] - 1)`. ### Function Signature ```python def max_product(nums: list[int]) -> int: ``` ### Rules - `i` and `j` must be different indices. The values stored at those indices may be equal. - Return the maximum product as a single integer. ### Constraints - `2 <= len(nums) <= 500` - `1 <= nums[i] <= 1000` ### Examples ```text Input: nums = [3,4,5,2] Output: 12 ``` Choosing indices 1 and 2 (values 4 and 5) gives (4 - 1) * (5 - 1) = 3 * 4 = 12. Every other pair gives 8 or less. ```text Input: nums = [1,5,4,5] Output: 16 ``` The two 5s sit at different indices, so both can be used: (5 - 1) * (5 - 1) = 16. ```text Input: nums = [1,9] Output: 0 ``` With only two elements the pair is forced, and (1 - 1) * (9 - 1) = 0.

Overview: Given an array of positive integers, choose two different indices and return the largest possible value of (nums[i] - 1) * (nums[j] - 1). The problem tests reasoning about how a product grows with its factors, handling repeated values at different positions, and writing a single linear scan.

Samsung logo
Samsung
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums, choose two different indices i and j of that array. Return the maximum value of (nums[i] - 1) * (nums[j] - 1).

Function Signature

def max_product(nums: list[int]) -> int:

Rules

  • i and j must be different indices. The values stored at those indices may be equal.
  • Return the maximum product as a single integer.

Constraints

  • 2 <= len(nums) <= 500
  • 1 <= nums[i] <= 1000

Examples

Input: nums = [3,4,5,2]
Output: 12

Choosing indices 1 and 2 (values 4 and 5) gives (4 - 1) * (5 - 1) = 3 * 4 = 12. Every other pair gives 8 or less.

Input: nums = [1,5,4,5]
Output: 16

The two 5s sit at different indices, so both can be used: (5 - 1) * (5 - 1) = 16.

Input: nums = [1,9]
Output: 0

With only two elements the pair is forced, and (1 - 1) * (9 - 1) = 0.