Skip to content

Commit

Permalink
Create ProductArray
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Jun 7, 2024
1 parent 58af0db commit ed87f38
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Leetcode/ProductArray
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//**


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.



Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]


**//



class ProductArray {
public int[] productExceptSelf(int[] nums) {

int[] arr = new int[nums.length];
Arrays.fill(arr,1);
int curr =1;
for(int j=0;j<nums.length;j++){
arr[j]*=curr;
curr*=nums[j];
}

curr=1;
for(int i=nums.length-1;i>-1;i--){

arr[i]*=curr;
curr*=nums[i];
}
return arr;

}
}

0 comments on commit ed87f38

Please sign in to comment.