-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThirdLargest.java
More file actions
78 lines (64 loc) · 1.88 KB
/
Copy pathThirdLargest.java
File metadata and controls
78 lines (64 loc) · 1.88 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
69
70
71
72
73
74
75
76
77
78
package array;
public class ThirdLargest {
static void thirdLargest1(int arr[], int arr_size)
{
/* There should be at least three elements */
if (arr_size < 3)
{
System.out.printf(" Invalid Input ");
return;
}
// Find first largest element
int first = arr[0];
for (int i = 1; i < arr_size ; i++)
if (arr[i] > first)
first = arr[i];
// Find second largest element
int second = Integer.MIN_VALUE;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > second && arr[i] < first)
second = arr[i];
// Find third largest element
int third = Integer.MIN_VALUE;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > third && arr[i] < second)
third = arr[i];
System.out.printf("The third Largest element is: "+third);
}
static void thirdLargest2(int arr[], int arr_size) {
/* There should be at least three elements */
if (arr_size < 3) {
System.out.printf(" Invalid Input ");
return;
}
// Initialize first, second and third Largest element
int first = arr[0], second = Integer.MIN_VALUE, third = Integer.MIN_VALUE;
// Traverse array elements to find the third Largest
for (int i = 1; i < arr_size; i++) {
/* If current element is greater than first,
then update first, second and third */
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second */
else if (arr[i] > second) {
third = second;
second = arr[i];
} /* If arr[i] is in between second and third */
else if (arr[i] > third) {
third = arr[i];
}
}
System.out.printf("The third Largest element is: "+third);
}
//Driver code
public static void main(String []args)
{
int arr[] = {12, 13, 1, 10, 34, 16};
int n = arr.length;
thirdLargest1(arr, n);
thirdLargest2(arr,n);
}
}