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.

You are given an array of integers `nums`. Choose two different indices `i` and `j` and compute `(nums[i] - 1) * (nums[j] - 1)`. Implement `max_product(nums)` so that it returns the maximum value of this expression over all such pairs. The two indices must be different, but the values stored at them may be equal. Return the maximum product as a single integer. Example 1: 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. Example 2: 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. Constraints: - 2 <= nums.length <= 500 - 1 <= nums[i] <= 1000 - The answer lies between 0 and 998001 (999 * 999), so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Constraints

  • 2 <= nums.length <= 500
  • 1 <= nums[i] <= 1000
  • i and j must be different indices; the values at those indices may be equal.
  • The answer lies between 0 and 998001 (999 * 999), so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Examples

Input: ([3, 4, 5, 2],)

Expected Output: 12

Explanation: Source example 1: values 4 and 5 give 3 * 4 = 12, and every other pair gives 8 or less.

Input: ([1, 5, 4, 5],)

Expected Output: 16

Explanation: Source example 2: the maximum 5 sits at two different indices, so both copies count: 4 * 4 = 16.

Hints

  1. Every value is at least 1, so each factor nums[i] - 1 is zero or positive. Think about how a product of two non-negative numbers changes as either factor grows.
  2. The two indices must differ, yet the same value can appear at two indices and both copies may be used.
  3. Check your idea by hand on [3, 4, 5, 2] (answer 12) and [1, 5, 4, 5] (answer 16).

Best Pair Product After Subtracting One

easySamsungSoftware Engineer

You are given an array of integers nums. Choose two different indices i and j and compute (nums[i] - 1) * (nums[j] - 1). Implement max_product(nums) so that it returns the maximum value of this expression over all such pairs.

The two indices must be different, but the values stored at them may be equal. Return the maximum product as a single integer.

Example 1: 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.

Example 2: 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.

Constraints:

  • 2 <= nums.length <= 500
  • 1 <= nums[i] <= 1000
  • The answer lies between 0 and 998001 (999 * 999), so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Examples

Example 1
Input
([3, 4, 5, 2],)
Output
12
Notes
Source example 1: values 4 and 5 give 3 * 4 = 12, and every other pair gives 8 or less.
Example 2
Input
([1, 5, 4, 5],)
Output
16
Notes
Source example 2: the maximum 5 sits at two different indices, so both copies count: 4 * 4 = 16.

Constraints

  • •2 <= nums.length <= 500
  • •1 <= nums[i] <= 1000
  • •i and j must be different indices; the values at those indices may be equal.
  • •The answer lies between 0 and 998001 (999 * 999), so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_product(nums: list[int]) -> int:
Sample call
max_product([3, 4, 5, 2])
Language
Case 1
Input
([3, 4, 5, 2],)
Expected
12
Case 2
Input
([1, 5, 4, 5],)
Expected
16

Browse More Questions

Show the approach

Approach

Every nums[i] is at least 1, so each factor nums[i] - 1 is zero or positive. For two non-negative factors the product never decreases when either factor grows, so the best pair uses the two largest factors taken from two different indices.

The solution scans the array once and keeps the largest factor seen so far (first) and the second largest (second), both starting at 0. When a new factor is at least first, the old first moves down into second and the new factor becomes first. Otherwise the new factor replaces second if it is larger. Comparing with 'at least' means a repeated maximum at another index fills the second slot, so [1, 5, 4, 5] gives 4 * 4 = 16.

Invariant: after each step, first and second are the two largest values of the multiset made of the factors seen so far plus two placeholder zeros. Because every real factor is at least 0 and the array has at least two elements, the top two of that multiset equal the top two real factors at distinct indices once the scan ends, and first * second is the answer.

Edge cases: with two elements the pair is forced; a value of 1 contributes a factor of 0, so when only one value exceeds 1 the answer is 0; all equal values v give (v - 1) * (v - 1); the largest possible answer is 999 * 999 = 998001, which fits in a 32-bit integer.

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