Product array puzzle | Practice | GeeksforGeeks

Given an array nums[] of size n, construct a Product Array P (of same size n) such that P[i] is equal to the product of all the elements of nums except nums[i].
 
Example 1:
Input:
n = 5
nums[] = {10, 3, 5, 6, 2}
Output:
180 600 360 300 900
Explanation: 
For i=0, P[i] = 3*5*6*2 = 180.
For i=1, P[i] = 10*5*6*2 = 600.
For i=2, P[i] = 10*3*6*2 = 360.
For i=3, P[i] = 10*3*5*2 = 300.
For i=4, P[i] = 10*3*5*6 = 900.

Example 2:
Input:
n = 2
nums[] = {12,0}
Output:
0 12


Your Task:
You do not have to read input. Your task is to complete the function productExceptSelf() that takes array nums[] and n as input parameters and returns a list of n integers denoting the product array P. If the array has only one element the returned list should should contains one value i.e {1}
Note: Try to solve this problem without using the division operation.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
 
Constraints:
1 <= n <= 1000
0 <= numsi <= 200
Array may contain duplicates.
+1
yuvrajranabtcse203 days ago
c++ soln without using division operation
Time Complexity: O(n)Auxiliary Space: O(n)
//for n==1
if(n==1)return {1};
//for n==2
if(n==2)return {nums[1],nums[0]};      //mesure left value and right value      long long int lefft=nums[0];      long long int rigght=nums[n-1];      vector<long long int> left;      vector<long long int> right;      for(int i=1;i<=n-1;i++){          left.push_back(lefft);          lefft*=nums[i];      }      for(int i=n-2;i>=0;i--){          right.push_back(rigght);          rigght*=nums[i];      }      vector<long long int> ans;      ans.push_back(right[n-2]);      for(int i=0;i<n-2;i++){          ans.push_back(left[i]*right[n-3-i]);      }      ans.push_back(left[n-2]);      return ans;   }
0
abhigyanpatek4 days ago
Simple C++ Solution:
I used only 1 array for right product, left product can be calculated during same traversal when ans is being stored.
 
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {       vector<long long> right(n);       right[n-1] = 1;       for(int i = n-2; i >= 0; i--){           right[i] = right[i+1]*nums[i+1];       }       vector<long long> res;       long long left = 1;       for(int i = 0; i<n; i++){           res.push_back(left * right[i]);           left *= nums[i];       }       return res;   }
0
itsmemritu4 days ago
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {             //code here         vector<long long int>v;       long long int sum=1;       long long int sum1=1;       int c=0;       for(int i=0 ;i<n ;i++){           sum*=nums[i];           if(nums[i]!=0){               sum1*=nums[i];           }           if(nums[i]==0)           c++;       }       for(int i=0 ;i<n ;i++){           if(nums[i]==0){               if(c>=2){                  v.push_back(0);                }               else               v.push_back(sum1);           }           else           v.push_back(sum/nums[i]);       }       return v;   }
0
anmolbansal25 days ago
 public static long[] productExceptSelf(int nums[], int n) {        // code here       if(n<=1)           return new long[]{1L};                  long[] p=new long[n];       for(int i=0;i<n;i++){           long mul=1;           for(int j=0;j<n;j++){                      if(i!=j){               mul=mul*nums[j];                                             }           }           p[i]=mul;                  }       return p;} 
0
rahulgupta067896 days ago
What is the issue with this approach can any body detect....
 
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {             //code here           vector<long long int>product;       long long int p=accumulate(nums.begin(),nums.end(),1,multiplies<int>());       for(int i=0;i<n;i++){           long long int a=p/nums[i];           product.push_back(a);       }       return product;   }
+1
badgujarsachin836 days ago
 vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {
       
        vector<long long int> v;
        long long int pro=1,zero=0;
        for(int i=0;i<n;i++){
            if(nums[i]==0){
                zero++;
            }else{
                pro*=nums[i];
            }
        }
        for(int i=0;i<n;i++){
            if(zero==0){
                v.push_back(pro/nums[i]);
            }else if(zero==1){
                if(nums[i]==0){
                    v.push_back(pro);
                }else{
                    v.push_back(0);
                }
            }else{
                v.push_back(0);
            }
        }
        return v;
        
    }
-1
amarrajsmart1976 days ago
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {             //code here       vector<long long int> v;       int count0=0;       long long int pro=1;       for(int i=0;i<n;i++)       {           if(nums[i]==0)           {               count0++;                          }           else           {               pro=pro*nums[i];           }       }       for(int i=0;i<n;i++){       if(count0==0)       {           v.push_back(pro/nums[i]);       }       else if(count0==1)       {           if(nums[i]==0)           v.push_back(pro);           else           {               v.push_back(0);           }       }       else       {           v.push_back(0);       }   }   return v;   }
0
debanshuswag1 week ago
vector<long long int> v;       for(int i=0;i<n;i++)       {           long long int a=1;           for(int j=0;j<n;j++)           {               if(i!=j)               a=a*nums[j];           }           v.push_back(a);       }       return v;
0
anubhavjoria292 weeks ago
class Solution 
{ 
	public static long[] productExceptSelf(int nums[], int n) 
	{ 
        // code here
        long[] p=new long[n];
        for(int i=0;i<n;i++){
            long mul=1;
            for(int j=0;j<n;j++){
            
            if(i!=j){
                mul=mul*nums[j];
                
                
                }
            }
            p[i]=mul;
            
        }
        return p;
	} 
} 

0
geeky20092 weeks ago
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {             //code here               vector<long long int>v;              for(int i=0;i<n;i++){       long long c=1;               for(int j=0;j<n;j++){               if(j!=i){                   c=c*nums[j];               }           }           v.push_back(c);       }       return v;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.