Skip to main content

Command Palette

Search for a command to run...

Product of Array Except Self

Published
1 min read
Product of Array Except Self
N

I am Nirbhay Singh , I am starting this blog to document my coding journey of becoming a software developer and to get my first $ 100k offer .

Product of Array Except Self

https://leetcode.com/problems/product-of-array-except-self/description/

Hint:

Code:

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {

        int n = nums.size();
        vector<int> ans(n,1);

        int left = nums[0];
        int right = nums[n-1];

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

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

        return ans;
    }
};

DSA prep

Part 9 of 22

This series is specifically to document and share my learning of Data Structure and Algorithms and building the programmers intuition to solve a problem through coding and getting my first job as SDE.

Up next

2D Arrays - Create A Matrix With Alternating X And 0

Create A Matrix With Alternating X And 0 Problem Link: https://www.codingninjas.com/studio/problems/create-a-matrix-with-alternating-x-and-0_981321?leftPanelTabValue=PROBLEM Hint: Create an array ['X', '0']; Declare a variable 'ind' and initialize ...

More from this blog

Daily Code by Nirbhay

74 posts

Hey, this is Nirbhay. I started this blog to document my journey of learning to code and get my first $100k offer. I'll be sharing the things related to DSA, backend development, devops and many more.