-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxandminarray.java
72 lines (66 loc) · 2.07 KB
/
maxandminarray.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
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
import java.util.Scanner;
public class maxandminarray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter size of the array");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("enter " + n + " elements ");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " ");
}
System.out.println("your 3rd maximum " + thirdmax(arr));
}
// static int maxandmin(int[] arr) {
// int max = arr[0];
// int secondMax = Integer.MIN_VALUE;
//
// for (int i = 1; i < arr.length; i++) {
// if (arr[i] > max) {
// secondMax = max;
// max = arr[i];
// } else if (arr[i] > secondMax && arr[i] != max) {
// secondMax = arr[i];
// }
// }
//
// if (secondMax == Integer.MIN_VALUE) {
// System.out.println("There is no second max");
// return Integer.MIN_VALUE; // you return any suitable value depending on your requirements
// }
//
// return secondMax;
// }
static int maxsh(int[] arr) {
int mx = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > mx) {
mx = arr[i];
}
}
return mx;
}
static int secondmax(int[] arr) {
int vib = maxsh(arr);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == vib) {
arr[i] = Integer.MIN_VALUE;
}
}
int secondmax = maxsh(arr);
return secondmax;
}
static int thirdmax (int [] arr) {
int mb = secondmax(arr);
for (int i = 0; i <arr.length; i ++) {
if (arr[i] == mb) {
arr[i] = Integer.MIN_VALUE;
}
}
int third = secondmax(arr);
return third;
}
}