-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.java
More file actions
41 lines (37 loc) · 1000 Bytes
/
Copy pathA.java
File metadata and controls
41 lines (37 loc) · 1000 Bytes
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
public class A {
public static int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];
int ans=1;
boolean zero=false;
for(int i:nums){
if(i!=0){
ans*=i;
}
else{
zero=true;
}
}
System.out.println( ans);
for(int i=0;i<nums.length;i++){
if(zero==true && nums[i]!=0){
answer[i]=0;
}
else if(zero==true && nums[i]==0){
answer[i]=ans;
}
else{
double div=Math.pow(nums[i],-1);
System.out.println(div);
answer[i]=(int)(ans*div);
}
}
return answer;
}
public static void main(String[] args) {
int[] arr = { -1,1,0,-3,3};
int[] res = productExceptSelf(arr);
for (int i : res) {
System.out.print(i + " ");
}
}
}