-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patharr_pr_21.java
45 lines (36 loc) · 1.06 KB
/
arr_pr_21.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
package com.company;
public class arr_pr_21 {
static class pair {// initializing
int min;
int max;
}
static pair getmin_max(int[] array, int n) {
pair min_max = new pair();
if (n == 1) {
min_max.max = array[0];
min_max.min = array[0];
return min_max; //return type
}
if (array[0] > array[1]) {
min_max.max = array[0];
min_max.min = array[1];
} else {
min_max.max = array[1];
min_max.min = array[0];
}
for (int i = 2; i < n; i++) { // loop
if(array[i]> min_max.max){
array[i] = min_max.max;
}
else if(array[i]< min_max.min){
array[i] = min_max.min;
}
}
return min_max;
}
public static void main(String[] args) {
int[] arr = {2,4,6,78,9,5}; // initializing
int n = arr.length; //length
System.out.println("max and min : " + getmin_max(arr, n));
}
}