-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxElement.java
More file actions
41 lines (36 loc) · 1.02 KB
/
MaxElement.java
File metadata and controls
41 lines (36 loc) · 1.02 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
package Arrays;
public class MaxElement {
// TC : O(n2)
static void max(int arr[]) {
int n = arr.length;
boolean isMax;
for(int i = 0; i < n; i++) {
// consider the current ith element as max element
isMax = true;
for(int j = 0; j < n; j++) {
if(arr[j] > arr[i]) {
// found a greater element than current element
isMax = false;
break;
}
}
if(isMax) {
System.out.println("Max Element : " + arr[i]);
return;
}
}
}
// TC : O(n)
static void max_2(int arr[]) {
int n = arr.length;
int max = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
System.out.println("Max Element : " + max);
}
public static void main(String[] args) {
}
}