← Deloitte bank

Deloitte · Question 25

Maximum Product of Two Elements

Javacode⏱ O(n)💾 O(1)
Service-based

Problem

Find the max product (nums[i]-1)*(nums[j]-1).

Approach

Sort the array or find the top two max values.

Time O(n)Space O(1)

How it works

  1. 1Track the two largest values in one pass.

Sample input / output

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

Constraints

2 <= nums.length <= 500; 1 <= nums[i] <= 10^3.

Solution — read it, then walk it

Time O(n)Space O(1)Loops 1Branches 2Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
yesno↻ repeatSTARTimport java.util.*DEFINE class MainDEFINE static intmaxProduct(int[] nums)DEFINE first = 0, second =0for (int n : nums)if (n > first)second = firstfirst = nelse if (n > second)second = nRETURN (first - 1) *(second - 1)DEFINE static voidmain(String[] args)System.out.println(maxProduct(newint[]{3, 4, 5, 2}))END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/13

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits