-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLargestElementArray.java
More file actions
68 lines (51 loc) · 2.14 KB
/
Copy pathLargestElementArray.java
File metadata and controls
68 lines (51 loc) · 2.14 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
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
import java.util.Arrays;
public class arrayExamples {
// Time complexity = O(n^2)
// approach 1 ---------------------------------- worst approach
static void approach_1(int [] arr, int n){ // nave approach (Brute force approach)
boolean isMax;
for(int i =0; i<arr.length; i++){
isMax=true; // this flag value will be initialize everytime to true hwen the first loop will run again
for(int j = 0; j<n;j++){ // this 2nd loop will compare all the values with the 1st loop's index value
if(arr[j]>arr[i]){
isMax = false;
break;
}
}
if(isMax){ //at the end of the 2nd for loop this if will check the flag value
System.out.println("The greatest number is : "+arr[i]);
return;
}
}
}
//--------------------------------------------2nd approach -----------------------------------------
// time complexity = O(n)
static void approach_2(int[] arr, int n){
int max = arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}
System.out.println("Largest element is : "+max);
}
// ----------------------------------------- 3rd approach-------------------------------------------------
// by sorting the array in ascending order and finding the last element
// time complexity = O(n)
static void approach_3(int[] arr, int n){
Arrays.sort(arr); // importesd the built in library for sorting the array------ this uses the pivot sort (Quick sort)
int max = arr[n-1];
System.out.println("max num is : "+max);
}
public static void main(String[] args) {
int [] arr = {95,12,41,56,44,231,1};
int n = arr.length;
approach_1(arr, n);
approach_2(arr, n);
approach_3(arr, n);
}
}
// ----------------------------------------Output--------------------------------------------------
// The greatest number is : 231
// Largest element is : 231
// max num is : 231