LeetCode: Maximum Product of Two Elements in an Array
Question
Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).
Solution
def max_product(nums)
  length = nums.length
  sorted_nums = nums.sort
  n1, n2 = sorted_nums[length - 2] - 1, sorted_nums[length - 1] - 1
  n1 * n2
end
 
                     
            
Comments