-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminMaxArrayElement.java
40 lines (31 loc) · 1.06 KB
/
minMaxArrayElement.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
package lbDsaSheet;
//we'll assume the very first element to be by default both min and max.
//as the index will increment min will be compared at let's say second element and if min<second/upcoming element that element value will be assigned to max var.
//time complecitiy = O(n)
//space complecitiy = O(1)
public class minMaxArrayElement {
public static void main(String[] args){
int[] arr= {6,2,3,4,1,9}; //min=1, max=9
findMinAndMax(arr);
}
static void findMinAndMax(int[] arr){
if(arr==null || arr.length ==0)return;
int min= arr[0];
int max= arr[0];
// USING MATH FUNCTION
// for(int i=1; i<arr.length; i++){
// min = Math.min(min, arr[i]);
// max = Math.max(max, arr[i]);
//}
for(int i=1; i<arr.length;i++){
if(arr[i]<min){
min=arr[i];
}
if(arr[i]>max){
max = arr[i];
}
}
System.out.println(max+" maximum");
System.out.println(min+" minimum");
}
}