finding largest and smallest in array is known, as i done in my below code but now how to print the two largest and two smallest elements from the array.
class twolargestsmallest
{
public static void main(String args[]) {
int a[]=new int[]{2,20,34,12,67};
int smallest=a[0];
int largest=a[0];
for(int i=0;i<a.length;i++){
if(a[i]<smallest){
smallest=a[i]; }
if(a[i]>largest){
largest=a[i]; }}
System.out.println(smallest+" "+largest); }}
Try to use Array.sort and get the 2 values from the beginning for the smallest and 2 values from the end for the largest