-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMaxDC.java
More file actions
59 lines (46 loc) · 1.77 KB
/
MinMaxDC.java
File metadata and controls
59 lines (46 loc) · 1.77 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
import java.util.*;
public class MinMaxDC {
// Inner class to hold the min and max values
static class MinMax {
int min;
int max;
MinMax(int min, int max) {
this.min = min;
this.max = max;
}
}
// Method to find the minimum and maximum using Divide and Conquer
public static MinMax findMinMax(int[] arr, int low, int high) {
if (low == high) {
return new MinMax(arr[low], arr[low]);
}
if (high == low + 1) {
if (arr[low] < arr[high]) {
return new MinMax(arr[low], arr[high]);
} else {
return new MinMax(arr[high], arr[low]);
}
}
int mid = (low + high) / 2;
MinMax leftMinMax = findMinMax(arr, low, mid);
MinMax rightMinMax = findMinMax(arr, mid + 1, high);
int overallMin = Math.min(leftMinMax.min, rightMinMax.min);
int overallMax = Math.max(leftMinMax.max, rightMinMax.max);
return new MinMax(overallMin, overallMax);
}
// Main method to take user input and find min and max
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array:");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
MinMax result = findMinMax(arr, 0, n - 1);
System.out.println("Minimum element is: " + result.min);
System.out.println("Maximum element is: " + result.max);
sc.close();
}
}