-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCS263_Lab_3.java
36 lines (31 loc) · 915 Bytes
/
CS263_Lab_3.java
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
// Question: Calculate the number of local maxima in an array and print them by using divide and conquer.
import java.util.*;
public class CS263_3_1 {
public static int count = 0;
public static int c = 0;
public static void recursion_min(int a[], int low, int high) {
c++;
if (low >= 0 && low <= high) {
int mid = (low + high) / 2;
if (mid >= 1 && a[mid] <= a[mid - 1] && a[mid] <= a[mid + 1]) {
count++;
return;
}
recursion_min(a, low, mid - 1);
recursion_min(a, mid + 1, high);
} else {
return;
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
recursion_min(a, 0, n - 1);
System.out.println("count: " + count);
System.out.println("recursion calls: " + c);
}
}