Interview Question | Product of Array Except Self | C++

Kartikeya Mishra
7 min readNov 1, 2023

--

Interview Question: Product of Array Except Self: C++
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> output(n);
output[0] = 1;
for(int i=1; i<n; i++){
output[i] = output[i-1] * nums[i-1];
}
int right = 1;
for(int i=n-1; i>=0; i--){
output[i] *= right;
right *= nums[i];
}
return output;
}

English :

This C++ code defines a function called `productExceptSelf` that takes a vector of integers `nums` as input and returns another vector of integers. The output vector contains the product of all elements in the `nums` vector except for the element at the same index. In other words, it computes the product of all elements to the left of an element and all elements to the right of that element.

Here’s a step-by-step explanation of how this code works:

1. `int n = nums.size();` — This line calculates the size of the input vector `nums` and stores it in the variable `n`, which represents the number of elements in the input vector.

2. `vector<int> output(n);` — This line initializes an output vector of integers called `output` with the same size as the input vector `nums`. This output vector will store the final results.

3. `output[0] = 1;` — This sets the first element of the `output` vector to 1. This is done because there are no elements to the left of the first element, so the product to the left is considered 1.

4. The first `for` loop:

for(int i = 1; i < n; i++) {
output[i] = output[i-1] * nums[i-1];
}

In this loop, it iterates through the input vector `nums` starting from the second element (index 1). For each element at index `i`, it calculates the product of all elements to the left of it and stores it in the `output` vector at the same index `i`.

5. `int right = 1;` — This initializes a variable `right` to 1. This variable is used to keep track of the product of all elements to the right of the current element.

6. The second `for` loop:

for(int i = n - 1; i >= 0; i - ) {
output[i] *= right;
right *= nums[i];
}

In this loop, it iterates through the input vector `nums` in reverse order, starting from the last element. For each element at index `i`, it multiplies the value in the `output` vector at index `i` with the value of `right`, which represents the product of all elements to the right of the current element. Then, it updates `right` by multiplying it with the value of the current element in `nums`. This process effectively computes the product of all elements to the right of each element.

7. Finally, the `output` vector contains the desired result, where each element at index `i` is the product of all elements in the original `nums` vector except for the element at index `i`. The function returns this `output` vector.

So, the code efficiently calculates the product of all elements except the one at the current index for each element in the input vector.

Hindi :

यह C++ कोड एक फ़ंक्शन को परिभाषित करता है जिसे `productExceptSelf` कहा जाता है, जो एक पूर्णांकों के एक वेक्टर `nums` को इनपुट के रूप में लेता है और एक और पूर्णांकों के वेक्टर को आउटपुट के रूप में लौटाता है। आउटपुट वेक्टर में इसका परिणाम सभी तत्वों का गुणफल शामिल है बिना उस तत्व के बिना जो समान सूची में है। दूसरे शब्दों में, यह किसी तत्व के बाएं ओर के सभी तत्वों का गुण और उस तत्व के दाएं ओर के सभी तत्वों का गुणांक निकालता है।

यहाँ इस कोड काम करने की कदम-से-कदम व्याख्या दी गई है:

1. `int n = nums.size();` — इस पंक्ति में इनपुट वेक्टर `nums` का आकार गणना किया जाता है और इसे मुद्रित की गई संख्या को प्रतिनिधित करने वाले चर `n` में संग्रहित किया जाता है, जो इनपुट वेक्टर में तत्वों की संख्या को प्रस्तुत करता है।

2. `vector<int> output(n);` — इस पंक्ति में एक आउटपुट वेक्टर को पूर्णांकों के रूप में मुद्रित किया जाता है, जिसे `output` कहा जाता है, और इसका आकार इनपुट वेक्टर `nums` के आकार के बराबर होता है। यह आउटपुट वेक्टर अंतिम परिणामों को संग्रहित करेगा।

3. `output[0] = 1;` — यह पहले तत्व को `output` वेक्टर का 1 पर सेट करता है। इसे इसलिए किया जाता है क्योंकि पहले तत्व के बाएं ओर कोई तत्व नहीं है, इसलिए बाएं ओर का गुण एक माना जाता है।

4. पहले `for` लूप:

for(int i = 1; i < n; i++) {
output[i] = output[i-1] * nums[i-1];
}

इस लूप में, यह इनपुट वेक्टर `nums` में दूसरे तत्व (सूची 1) से शुरू होकर चलता है। प्रत्येक तत्व पर जो सूची में है, वह उसके बाएं ओर के सभी तत्वों का गुण निकालता है और उसे उसी सूची के निकटस्थान (इंडेक्स) पर `output` वेक्टर में संग्रहित करता है।

5. `int right = 1;` — इस पंक्ति में एक चर `right` को 1 से प्रारंभ किया जाता है। इस चर का उपयोग मौजूदे तत्व के दाएं ओर के सभी तत्वों का गुणफल को ट्रैक करने के लिए किया जाता है।

6. दूसरे `for` लूप:

for(int i = n - 1; i >= 0; i - ) {
output[i] *= right;
right *= nums[i];
}

यह इनपुट वेक्टर `nums` को उल्टा क्रम में, अंतिम तत्व से शुरू करके चलता है। प्रत्येक तत्व पर जो सूची में है, वह `output` वेक्टर में संग्रहित किए गए मौजूदे का गुण करता है, जिसे उसी तत्व के निकटस्थान पर `output` वेक्टर में संग्रहित किया गया है। फिर, यह `right` को मौजूदे तत्व में के मूल्य से गुणित करके उसे अद्यतन करता है। इस प्रक्रिया के द्वारा वास्तविक रूप से प्रत्येक तत्व के दाएं ओर के सभी तत्वों का गुणफल निकालता है।

7. अंत में, `output` वेक्टर में इच्छित परिणाम होता है, जहां प्रत्येक तत्व का सूची में विरासत में उपस्थित सभी तत्वों का गुणफल होता है केवल उस तत्व को छोड़कर जिसका निकटस्थान `i` है। इस फ़ंक्शन द्वारा यह `output` वेक्टर लौटाया जाता है।

इस प्रकार, यह कोड इनपुट वेक्टर में हर तत्व के बाएं ओर के तत्व के बिना सभी तत्वों का गुणफल प्राप्त करने के लिए प्रभावी रूप से काम करता है।

Hinglish :

Yeh C++ ka code ek function define karta hai jiska naam hai `productExceptSelf`. Is function ko ek integer vector `nums` ke roop me input diya jata hai aur ye doosra ek integer vector return karta hai. Output vector me `nums` vector ke sabhi elements ka product hota hai, lekin us element ko chhod kar jo same index par hai. Dusre shabdon me, ye har ek element ke baaye aur daaye wale sabhi elements ka guna ka product nikalta hai.

Is code ka kaam kaise karta hai, uska step-by-step vistar hai:

1. `int n = nums.size();` — Is line me input vector `nums` ki size calculate ki jati hai aur usko variable `n` me store kiya jata hai, jo input vector me maujood elements ki sankhya ko darust karti hai.

2. `vector<int> output(n);` — Is line me ek output vector banaya jata hai, jisme integers ke elements hote hain, aur iska size input vector `nums` ke size ke samaan rakha jata hai. Yeh output vector antim parinaam ko store karega.

3. `output[0] = 1;` — Isse output vector ke pehle element ko 1 se set kiya jata hai. Iska matlab hai ki pehle element ke baaye koi element nahi hai, isliye uske baaye ke guna ko 1 mana jata hai.

4. Pehla `for` loop:

for(int i = 1; i < n; i++) {
output[i] = output[i-1] * nums[i-1];
}

Is loop me, input vector `nums` ke second element (index 1) se shuruaat karte huye har ek element ke liye, uske baaye wale sabhi elements ka guna calculate kiya jata hai aur usko output vector ke usi index `i` me store kiya jata hai.

5. `int right = 1;` — Isse ek variable `right` ko 1 se initialize kiya jata hai. Is variable ka upayog current element ke daaye wale sabhi elements ka guna track karne ke liye hota hai.

6. Dusra `for` loop:

for(int i = n - 1; i >= 0; i - ) {
output[i] *= right;
right *= nums[i];
}
Is loop me, input vector `nums` ko ulta order me traverse kiya jata hai, aakhri element se shuruaat karte huye. Har ek element ke liye, index `i` par, output vector ke usi index `i` me value ko `right` ke value se multiply kiya jata hai, jo current element ke daaye wale sabhi elements ka guna represent karta hai. Fir `right` ko current element ke value se multiply karke update kiya jata hai. Is tarah se har ek element ke daaye wale sabhi elements ka guna calculate hota hai.

7. Ant mein, `output` vector me maangaa gaya parinaam hota hai, jahan har ek element index `i` par, original `nums` vector ke sabhi elements ka product hota hai, lekin us element ko chhod kar jo index `i` par hai. Is function dwara yeh `output` vector return kiya jata hai.

Toh is code se, input vector ke har ek element ke liye, us element ke daaye wale sabhi elements ka guna nikala jata hai, aur yeh kaam bahut hi prabhavi tarike se kiya jata hai.

--

--

Kartikeya Mishra

Computer science engineer nomad exploring digital world. Learning and teaching while having fun.