Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Product of Array Except Self problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Product of Array Except Self problem solution, we have given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.

Leetcode Product of Array Except Self problem solution

Leetcode Product of Array Except Self problem solution in Python.

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        left = [1] * len(nums)
        right = [1] * len(nums)
        res = []
        for i in range (1, len(nums)):
            left[i] = left[i-1] * nums[i-1]
        for i in range (len(nums)-2, -1, -1):
            right[i] = right[i+1] * nums[i+1]
        for i in range (0, len(nums)):
            res.append(left[i] * right[i])
        return res

Product of Array Except Self problem solution in Java.

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int tmp = 1;
        int n  =nums.length;
        int[] left = new int[nums.length];
        int[] right = new int[n];
        left[0] = 1;
        for (int i = 1; i < n; i++){
            tmp *= nums[i-1];
            left[i] = tmp;
        }
        right[n-1] = 1;
        tmp = 1;
        for (int i = n - 2; i >= 0; i--){
            tmp *= nums[i+1];
            right[i] = tmp;
        }
        for (int i = 0; i < n; i++){
            nums[i] = left[i] * right[i];
        }
        return nums;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
            int cnt=0;
            int n=nums.size();
            vector<int>pref(n),suf(n);
            for(int i=0;i<n;i++){
               pref[i]=nums[i];
               if(i) pref[i]*=pref[i-1];
            }
            for(int i=n-1;~i;i--){
                suf[i]=nums[i];
                if(i<n-1) suf[i]*=suf[i+1];
            }
            for(int i=0;i<n;i++){
                if(i==0){
                    nums[i]=suf[i+1];
                }else if(i==n-1){
                    nums[i]=pref[i-1];
                }else{
                    nums[i]=(pref[i-1]*suf[i+1]);
                }
            }
        
            return nums;
        
    }
};

Problem solution in C.

int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
    int sum = 1;
    int flag = 0;
    for (int i = 0; i < numsSize && flag < 2; i++){
        if (!nums[i]) {flag++; continue;}
        sum = sum * nums[i];
    }
    int* result = (int*)malloc(sizeof(int) * numsSize);
    memset(result, 0, sizeof(int)*numsSize);
    *returnSize = numsSize;
    if (flag>1) return result;
    for (int i = 0; i < numsSize; i++){
        if (!nums[i]) {result[i] = sum; continue;}
        if (flag > 0) {result[i] = 0; continue;}
        result[i] = sum / nums[i];
    }
    return result;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes