-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_prod.java
More file actions
53 lines (51 loc) · 1.34 KB
/
Array_prod.java
File metadata and controls
53 lines (51 loc) · 1.34 KB
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
import java.util.Scanner;
/*Array special product
Implement a function of array integers, by returning a new array. In which, every index carries the value of the product of remaining elements.
Example:
Given array [1, 2, 3, 4, 5] it will return [120, 60, 40, 30, 24].
Given array [4, 10, 3] it will return [30, 12, 40].
Function Description:
The function finds Special Product by accepting the following parameters:
The function must return a new array of size n, in which every index carries the value of the product of the remaining elements.
Format:
Input:
The first line specifies, the number of elements in the array. Next, n lines have a list of integers.
Output:
Output has an array of n integers with each integer in a line.
Constraints:
Division operator cannot be used.
0 <= n <= 5
0 <= in[i] <= 10
0 <= i <= n
Example:
Input:
5
1
2
3
4
5
Output:
120
60
40
30
24*/
public class Array_prod {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int prod = 1;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
prod = prod * a[i];
}
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = prod / a[i];
System.out.println(res[i]);
}
sc.close();
}
}