-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-1.java
54 lines (37 loc) · 1012 Bytes
/
Problem-1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Product array puzzle
Problem Link :- https://practice.geeksforgeeks.org/problems/product-array-puzzle4525/1
Reference Link:- https://www.youtube.com/watch?v=5IUyc7cPD9E
*/
class Solution
{
public static long[] productExceptSelf(int nums[], int n)
{
// code here
long arr[]=new long[n];
long leftprod=1;
arr[0]=1;
for(int i=1;i<n;i++)
{
leftprod*=nums[i-1];
arr[i]=leftprod;
}
long rightarr[]=new long[n];
long rightprod=1;
rightarr[n-1]=1;
for(int i=n-2;i>=0;i--)
{
rightprod*=nums[i+1];
rightarr[i]=rightprod;
}
long result[]=new long[n];
for(int i=0;i<n;i++)
{
result[i]=arr[i]*rightarr[i];
}
return result;
}
}