classSolution {
publicint[]productExceptSelf(int[] nums) {
int n = nums.length;
int ans[]=newint[n];
for(int i = 0; i < n; i++) {
int prod = 1;
for(int j = 0; j < n; j++) {
if(i == j) {
continue;
}
prod *= nums[j];
}
ans[i]= prod;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defproductExceptSelf(self, nums):
n = len(nums)
ans = [0] * n # Create a result array filled with 0s# Iterate through the arrayfor i in range(n):
prod =1# Initialise product for current indexfor j in range(n):
if i != j: # Compute product of all numbers except at the current index prod *= nums[j]
ans[i] = prod # Store the product in the result arrayreturn ans # Return the computed array
We can calculate product of all elements. Now, for each element, we can just divide the element and store it in new array.
Using a division-based approach is a straightforward way to solve the problem. However, there are a few important considerations, such as handling edge cases like arrays containing zero or avoiding integer division errors.
Here are the edge case for this approach:
If there’s one zero in the array, the product will only be non-zero for the position of the zero.
If there are multiple zeros, all elements in the result array will be zeros.
classSolution {
publicint[]productExceptSelf(int[] nums) {
int n = nums.length;
int[] ans =newint[n]; // Standard array declaration styleint totalProduct = 1; // Variable to store the product of all elementsint zeroCount = 0; // To count zeros in the arrayint zeroIdx =-1;
// Step 1: Calculate total product and count zerosfor (int i = 0; i < n; i++) {
if (nums[i]== 0) {
zeroCount++;
zeroIdx = i;
// If more than 1 zero, all products will be 0if (zeroCount > 1) {
return ans;
};
} else {
totalProduct *= num;
}
}
if (zeroCount == 1) {
ans[zeroIdx]= totalProduct;
return ans;
}
// Step 2: Populate the result arrayfor (int i = 0; i < n; i++) {
ans[i]= totalProduct / nums[i]; // Regular case with no zeros }
return ans;
}
}
classSolution:
defproductExceptSelf(self, nums):
n = len(nums)
ans = [0] * n # Initialise the result array with zeros totalProduct =1# Variable to store the product of non-zero elements zeroCount =0# To count the number of zeros in the array# Step 1: Calculate total product and count zerosfor i in range(n):
if nums[i] ==0:
zeroCount +=1 zeroIdx = i
if zeroCount >1: # If more than 1 zero, all products will be 0return ans
else:
totalProduct *= nums[i]
if zeroCount ==1:
ans[zeroIdx] = totalProduct
return ans
# Step 2: Populate the result arrayfor i in range(n):
ans[i] = totalProduct // nums[i] # Regular case with no zerosreturn ans
classSolution {
publicint[]productExceptSelf(int[] nums) {
int n = nums.length;
int[] prefix =newint[n];
int[] suffix =newint[n];
// Initialise first prefix and last suffix prefix[0]= 1;
suffix[n - 1]= 1;
// Scan from left to right to fill prefix arrayfor (int i = 1; i < n; i++) {
prefix[i]= nums[i - 1]* prefix[i - 1];
}
// Scan from right to left to fill suffix arrayfor (int i = n - 2; i >= 0; i--) {
suffix[i]= nums[i + 1]* suffix[i + 1];
}
// Multiply prefix and suffix arrays to get the resultint[] ans =newint[n];
for (int i = 0; i < n; i++) {
ans[i]= prefix[i]* suffix[i];
}
return ans;
}
}
classSolution:
defproductExceptSelf(self, nums):
n = len(nums)
prefix = [1] * n # Initialise prefix array suffix = [1] * n # Initialise suffix array# Fill prefix arrayfor i in range(1, n):
prefix[i] = nums[i -1] * prefix[i -1]
# Fill suffix arrayfor i in range(n -2, -1, -1):
suffix[i] = nums[i +1] * suffix[i +1]
# Multiply prefix and suffix to get the result ans = [0] * n
for i in range(n):
ans[i] = prefix[i] * suffix[i]
return ans
classSolution {
publicint[]productExceptSelf(int[] nums) {
int n = nums.length;
int[] ans =newint[n];
// Calculate prefix product and store in ansint prefix = 1;
for (int i = 0; i < n; i++) {
ans[i]= prefix; // Ans holds prefix product for index i prefix *= nums[i];
}
// Calculate suffix product and update ansint suffix = 1;
for (int i = n - 1; i >= 0; i--) {
ans[i]*= suffix; // Combine suffix product with prefix already in ans suffix *= nums[i];
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defproductExceptSelf(self, nums):
n = len(nums)
ans = [1] * n # Initialise result array with 1s# Calculate prefix product and store in ans prefix =1for i in range(n):
ans[i] = prefix # Store prefix product at index i prefix *= nums[i]
# Calculate suffix product and update ans suffix =1for i in range(n -1, -1, -1):
ans[i] *= suffix # Multiply suffix with the value in ans suffix *= nums[i]
return ans