-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayProblems.java
More file actions
119 lines (94 loc) · 3.79 KB
/
ArrayProblems.java
File metadata and controls
119 lines (94 loc) · 3.79 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
import java.util.Arrays;
public class ArrayProblems {
// Trapped Water
// public static int trappedRainwater(int height[]){
// int n = height.length;
// //calculate left max boundary
// int leftMax[] = new int[n];
// leftMax[0] = height[0];
// for(int i=1; i<n; i++){
// leftMax[i] = Math.max(height[i], leftMax[i-1]);
// }
// // calculate right max boundary
// int rightMax[] = new int[n];
// rightMax[n-1] = height[n-1];
// for(int i=n-2; i>=0; i--) {
// rightMax[i] = Math.max(height[i], rightMax[i+1]);
// }
// int trappedWater = 0;
// for(int i=0; i<n; i++){
// //waterlevel = min(leftmax bound, rightmax bound)
// int waterLevel = Math.min(leftMax[i], rightMax[i]);
// //trapped water = waterLevel - height[i]
// trappedWater += waterLevel - height[i];
// }
// return trappedWater;
// }
// ----------------------------------------------------------------------------------------
// Buy And Sell Stocks
// public static int buyAndSellStocks(int prices[]) {
// int buyPrice = Integer.MAX_VALUE;
// int maxProfit = 0;
// for(int i=0; i<prices.length; i++){
// if(buyPrice < prices[i]){
// int profit = prices[i] - buyPrice;
// maxProfit = Math.max(maxProfit, profit);
// } else {
// buyPrice = prices[i];
// }
// }
// return maxProfit;
// }
// ----------------------------------------------------------------------------------------
//Duplicate arrays
// public static void containDuplicate(int arr[]) {
// boolean hasDuplicate = false;
// for(int i = 0; i < arr.length; i++) {
// for(int j = i + 1; j < arr.length; j++) {
// if(arr[i] == arr[j]) {
// hasDuplicate = true;
// break; // duplicate found, stop inner loop
// }
// }
// if(hasDuplicate) break; // stop outer loop too
// }
// System.out.println(hasDuplicate ? "True" : "False");
// }
// ----------------------------------------------------------------------------------------
public static void triplets(int nums[]){
Arrays.sort(nums); // Sort the array first
for (int i = 0; i < nums.length - 2; i++) {
// Skip duplicate values for i
if (i > 0 && nums[i] == nums[i - 1]) continue;
for (int j = i + 1; j < nums.length - 1; j++) {
// Skip duplicate values for j
if (j > i + 1 && nums[j] == nums[j - 1]) continue;
for (int k = j + 1; k < nums.length; k++) {
// Skip duplicate values for k
if (k > j + 1 && nums[k] == nums[k - 1]) continue;
if (nums[i] + nums[j] + nums[k] == 0) {
System.out.println(nums[i] + " " + nums[j] + " " + nums[k]);
}
}
}
}
}
public static void main(String[] args) {
//Triplets
int nums[] = {-1, 0 , 1, 2, -1, -4};
triplets(nums);
// ----------------------------------------------------------------------------------------
//Duplicate arrays
// int arr[] = {1, 2, 3, 4};
// containDuplicate(arr); // Output: False
}
// ----------------------------------------------------------------------------------------
// Buy And Sell Stocks
// int prices[] = {7, 1, 5, 3, 6, 4};
// System.out.println(buyAndSellStocks(prices));
// ----------------------------------------------------------------------------------------
// Trapped Water :
// int height[] = {4,2,0,6,3,2,5};
// System.out.println(trappedRainwater(height));
// }
}