Загрузка...

LeetCode 238 – Product of Array Except Self | c++

Idea / How to Solve:
For each number in the array, we want the product of all other numbers.

Instead of multiplying every time:

1️⃣ Multiply all numbers before current index → call it left
2️⃣ Multiply all numbers after current index → call it right
3️⃣ Product for current index = left × right

We do this in two passes:

First pass → left products (left to right)

Second pass → right products (right to left, using a variable)

✅ This way, we get the answer for all indices without using division and in O(n) time.

Example:
nums = [1, 2, 3, 4]

Left products → [1, 1, 2, 6]

Multiply with right products → final = [24, 12, 8, 6]

#LeetCode #Coding #DSA #ProductOfArrayExceptSelf #Programming

Видео LeetCode 238 – Product of Array Except Self | c++ канала izumicode
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять