forked from Teju-1212/hacktoberfest-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatleasttwogreater.java
More file actions
46 lines (36 loc) · 1.1 KB
/
atleasttwogreater.java
File metadata and controls
46 lines (36 loc) · 1.1 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
import java.util.Arrays;
public class atleasttwogreater{
public static long[] findElements( long arr[], long n)
{
long firstgreatest=Long.MIN_VALUE;
long secondgreatest=Long.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>firstgreatest){
secondgreatest=firstgreatest;
firstgreatest=arr[i];
}
else if(arr[i]>secondgreatest){
secondgreatest=arr[i];
}
}
System.out.println(firstgreatest);
System.out.println(secondgreatest);
long[] newlist=new long[arr.length-2];
int count=0;
for(int i=0;i<arr.length;i++){
// System.out.print(arr[i]);
if(arr[i]<firstgreatest&&arr[i]<secondgreatest){
System.out.print(arr[i]+" ");
newlist[count]=arr[i];
count++;
}
}
return newlist;
}
public static void main(String[] args) {
// 1 2 5
long[] arr= {2, 8, 7, 1, 5};
long [] a= findElements(arr,arr.length);
System.out.println(Arrays.toString(a));
}
}