-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.java
More file actions
230 lines (194 loc) · 6.21 KB
/
Lab2.java
File metadata and controls
230 lines (194 loc) · 6.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.util.Scanner;
public class Lab2 {
static int maxNum(int a, int b) {
return (a > b ? a : b);
}
static int minNum(int a, int b) {
return (a < b ? a : b);
}
static int max3(int a, int b, int c) {
if(a>b && a>c){
return a;
}
else if (b>a && b>c){
return b;
}
else{
return c;
}
}
static int max4(int a, int b,int c, int d){
if(a>b && a>c && a>d){
return a;
}
else if(b>a && b>c && b>d){
return b;
}
else if(c>a && c>b && c>d){
return c;
}
else{
return d;
}
}
static int max3(int max,int a,int b, int c,int d){
if(a>b && a>c && max>a){
return a;
}
else if(b>a && b>c && max>b){
return b;
}
else if(c>a && c>b && max>c){
return b;
}
else{
return d;
}
}
static int max_n(int[] A, int start, int end) {
if (start == end) {
return A[start]; // Return the element instead of using an undeclared variable
} else {
int mid = (start + end) / 2;
return maxNum(max_n(A, start, mid), max_n(A, mid + 1, end));
}
}
static int[] min_max(int[] A, int start, int end) {
int[] result = new int[2];
if (start == end) {
result[0] = A[start]; // max
result[1] = A[start]; // min
} else {
int mid = (start + end) / 2;
int[] left = min_max(A, start, mid);
int[] right = min_max(A, mid + 1, end);
result[0] = maxNum(left[0], right[0]); // max
result[1] = minNum(left[1], right[1]); // min
}
return result;
}
static int[] max_sec(int [] A, int start, int end){
int []result=new int[2];
if (start == end) {
result[0] = A[start]; // max
result[1] = A[start]; // sec_max
} else {
int mid = (start + end) / 2;
int[] left = max_sec(A, start, mid);
int[] right = max_sec(A, mid + 1, end);
result[0] = max4(left[0], right[0],left[1],right[1]); // max
result[1] = max3(result[0],left[1], right[1],left[0],right[0]); // sec_max
}
return result;
}
static int missing(int []A, int start,int end){
int result=0;
int mid=(start+end)/2;
if ((A[end] - A[start]) != (end - start)) {
if (A[mid] != A[start] + (mid - start)) {
return A[start] + (mid - start); // Return the missing number
}
}
else{
result=missing(A,start,mid);
result=missing(A,mid+1,end);
}
return result;
}
public static void mergeSort(int[] array, int left, int right) {
if (left < right) {
// Find the middle point
int mid = (left + right) / 2;
// Recursively sort the left and right halves
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
// Merge the sorted halves
merge(array, left, mid, right);
}
}
// Function to merge two subarrays of the array
public static void merge(int[] array, int left, int mid, int right) {
// Find sizes of two subarrays to be merged
int n1 = mid - left + 1;
int n2 = right - mid;
// Create temporary arrays
int[] L = new int[n1];
int[] R = new int[n2];
// Copy data to temporary arrays
for (int i = 0; i < n1; ++i)
L[i] = array[left + i];
for (int j = 0; j < n2; ++j)
R[j] = array[mid + 1 + j];
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}
// Copy remaining elements of L[] if any
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
// Copy remaining elements of R[] if any
while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}
static int frequency(int []A,int start,int end){
mergeSort(A, start, end);
int median;
if(end%2!=0){
median=(A[end]+A[start])/2;
}
else{
median=((A[(end - 1) / 2] + A[end / 2]) / 2)-1;
}
return median;
}
static int max_product(int []A,int start,int end){
int product=1;
for(int x:A){
product=product*x;
}
int prod_arr[]=new int[end];
int MaxSum=A[start];
int CurrSum=A[start];
int i;
for(i=end;i<=0;i++){
MaxSum=max3(MaxSum,A[i],CurrSum+A[i]);
CurrSum=maxNum(A[i],CurrSum+A[i]);
}
return MaxSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 6;
int[] A = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter the element:");
A[i] = scanner.nextInt();
}
//int maxElement = max_n(A, 0, n - 1);
int[] minMax = min_max(A, 0, n - 1);
//System.out.println("The max element in the array is " + maxElement);
System.out.println("The max element in the array is " + minMax[0]);
System.out.println("The min element in the array is " + minMax[1]);
//int missing_num=missing(A,0,n-1);
//System.out.println("The missing element in the array is " + missing_num);
int max_freq=frequency(A,0, n-1);
System.err.println("element with maximum frquency is:"+max_freq);
scanner.close();
}
}