LeetCode Entry

1464. Maximum Product of Two Elements in an Array

27.07.2026 easy 2026 kotlin rust

Max product in array

1464. Maximum Product of Two Elements in an Array easy substack youtube

https://dmitrysamoylenko.com/leetcode/

27.07.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1433

Problem TLDR

Max product in array

Intuition

Brute-force: iterate in a nested ‘for’ loops. Accepted.

Approach

  • we can sort and take two max values
  • we can scan and find two max values

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

    fun maxProduct(n: IntArray) =
    n.sorted().run{(last()-1)*(get(n.size-2)-1)}
    pub fn max_product(n: Vec<i32>) -> i32 {
        n.iter().map(|&x|x-1).k_largest(2).product::<i32>()
    }

Comments