-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARRAY.java
More file actions
133 lines (106 loc) · 3.42 KB
/
ARRAY.java
File metadata and controls
133 lines (106 loc) · 3.42 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Arrays;
class ARRAY {
// public int[] twoSum(int[] nums, int target) {
// for (int i = 0; i < nums.length; i++) {
// for (int j = i + 1; j < nums.length; j++) {
// if (nums[i] + nums[j] == target) {
// return new int[]{i, j};
// }
// }
// }
// return new int[]{};
// }
// public static void main(String[] args) {
// ARRAY sol = new ARRAY();
// int[] nums = {2,7,11,15};
// int target = 9;
// System.out.println(Arrays.toString(sol.twoSum(nums, target)));
// }
// public int maxSubArray(int[] nums){
// int currentSum = nums[0];
// int maxSum = nums[0];
// for (int i = 1; i <nums.length; i++){
// currentSum = Math.max(nums[i], currentSum + nums[i]);
// maxSum = Math.max(maxSum, currentSum);
// }
// return maxSum;
// }
// public static void main(String[] args) {
// ARRAY sol = new ARRAY();
// int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
// System.out.println(sol.maxSubArray(nums));
// }
// public int maxProfit(int[] prices) {
// int minPrice = Integer.MAX_VALUE;
// int maxProfit = 0;
// for (int price : prices) {
// if (price < minPrice) {
// minPrice = price; // नया minimum मिला
// } else {
// maxProfit = Math.max(maxProfit, price - minPrice);
// }
// }
// return maxProfit;
// }
// public static void main(String[] args) {
// ARRAY sol = new ARRAY();
// int[] prices = {7,1,5,3,6,4};
// System.out.println(sol.maxProfit(prices)); // Output: 5
// }
// }
// public void moveZeroes(int[] nums){
// int lastNonZeroIndex = 0;
// for(int i = 0; i < nums.length; i++) {
// if (nums[i] !=0){
// nums[lastNonZeroIndex] = nums[i];
// lastNonZeroIndex++;
// }
// }
// for (int i = lastNonZeroIndex; i < nums.length; i++){
// nums[i] = 0;
// }
// }
// public static void main(String[] args) {
// ARRAY sol = new ARRAY();
// int[] nums = {0,1,0,3,12};
// sol.moveZeroes(nums);
// System.out.println(Arrays.toString(nums));
// }
// public int removeDuplicates(int[] nums) {
// if (nums.length == 0) return 0;
// int i = 0;
// for (int j = 1; j <nums.length; j++){
// if(nums[j] != nums[i]){
// i++;
// nums[i] = nums[j];
// }
// }
// return i + 1;
// }
// public static void main(String[] args) {
// ARRAY sol = new ARRAY();
// int[] nums = {0,0,1,1,1,2,2,3,3,4};
// int k = sol.removeDuplicates(nums);
// System.out.println("Unique counter:" + k);
// System.out.println("Array after removal:" + Arrays.toString(nums));
// }
public int[] productExceptSelf(int[] nums){
int n = nums.length;
int[] Output = new int[n];
Output[0] = 1;
for (int i = 1; i < n; i++){
Output[i] = Output[i - 1] * nums[i - 1];
}
int rightProduct = 1;
for (int i = 1; i < n; i++){
Output[i] = Output[i] * rightProduct;
rightProduct *= nums[i];
}
return Output;
}
public static void main(String[] args) {
ARRAY sol = new ARRAY();
int[] nums = {1,2,3,4};
System.out.println(Arrays.toString(sol.productExceptSelf(nums)));
}
}