-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorld.java
50 lines (47 loc) · 1.18 KB
/
HelloWorld.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
46
47
48
49
50
Naive Approach
class HelloWorld {
static int sec_largest(int arr[]){
int largest=getlargest(arr);
int res=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=largest){
if(arr[i]>res)
res=arr[i];
}
}
return res;
}
static int getlargest(int arr[] ){
int res=arr[0];
for(int i=0;i<arr.length;i++)
{
if(arr[i]>res)
res=arr[i];
}
return res;
}
public static void main(String args[]) {
int arr[]={1,32,45,3,6};
System.out.println(sec_largest(arr));
}
}
class HelloWorld{
static int sec_largest(int arr[]){
int res=Integer.MIN_VALUE,largest=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>largest)
{res=largest;
largest=arr[i];
}
else if(arr[i]>res && arr[i]!=largest)
res=arr[i];
}
return res;
}
public static void main(String args[]) {
int arr[]={1,32,45,37,6};
System.out.println(sec_largest(arr));
}
}