-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathques5.java
More file actions
53 lines (44 loc) · 1.47 KB
/
ques5.java
File metadata and controls
53 lines (44 loc) · 1.47 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
//PRODUCT OF ARRAY EXCEPT SELF
//LEETCODE LINK --> https://leetcode.com/problems/product-of-array-except-self/
import java.util.Scanner;
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
int[] arr = new int[n];
// Calculate left product array
left[0] = 1;
for (int i = 1; i < n; i++) {
left[i] = left[i - 1] * nums[i - 1];
}
// Calculate right product array
right[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
right[i] = right[i + 1] * nums[i + 1];
}
// Calculate the result array
for (int i = 0; i < n; i++) {
arr[i] = left[i] * right[i];
}
return arr;
}
}
public class ques5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Solution sol = new Solution();
int[] result = sol.productExceptSelf(nums);
System.out.println("The product of array except self is:");
for (int res : result) {
System.out.print(res + " ");
}
}
}