-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProductPuzzle.java
89 lines (74 loc) · 2.18 KB
/
ProductPuzzle.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*https://leetcode.com/problems/product-of-array-except-self/*/
/*Prateekshya*/
/*With extra space*/
class Solution
{
public static long[] productExceptSelf(int nums[], int n)
{
long product = 1;
long[] arr = new long[n];
int zeroCount = 0;
for (int i = 0; i < n; ++i)
{
//if element is not zero, multiply
if (nums[i] != 0)
product *= nums[i];
//if zero
else
{
//increase the count
++zeroCount;
//if mroe than one zeroes, return array containing zeroes
if (zeroCount > 1)
return arr;
}
}
for (int i = 0; i < n; ++i)
//if one zero present
if (zeroCount > 0)
//everything will be zero apart from the index containing zero
arr[i] = (nums[i] == 0 ? product : 0);
//otherwise, normal division
else
arr[i] = product/nums[i];
return arr;
}
}
/*Without extra space*/
class Solution {
public int[] productExceptSelf(int[] nums) {
int zeroCount = 0, zeroIndex = -1, totalProduct = 1;
//count zeros, update total product except zeros
for (int i = 0; i < nums.length; ++i)
{
if (nums[i] == 0)
{
++zeroCount;
zeroIndex = i;
}
else
totalProduct *= nums[i];
if (zeroCount > 1)
break;
}
//if more than one zeros, set everything to 0 and return
if (zeroCount > 1)
{
for (int i = 0; i < nums.length; ++i)
nums[i] = 0;
return nums;
}
//if a single zero, update everything to 0 apart from that index
else if (zeroCount == 1)
{
for (int i = 0; i < nums.length; ++i)
nums[i] = 0;
nums[zeroIndex] = totalProduct;
return nums;
}
//general case
for (int i = 0; i < nums.length; ++i)
nums[i] = totalProduct/nums[i];
return nums;
}
}