Three Max, Three Min Please
Description
Given an array of N integers, print the 3 distinct maximum and 3 distinct minimum elements of the array.
Input
Input Format
First line contains N
Second line contains N space separated integers which are elements of the array.
Constraints
0<=N<=100 (Please note that N can be 0 as well)
The values present in the array can be negative as well.
Output
In the first line, print the least 3 values (sorted) present in the array.
In the second line, print the top 3 values (sorted) present in the array.
In case there are not 3 min value, print "Not Possible" (without quotes).
In case there are not 3 max value, print "Not Possible" (without quotes).
So, according to the above statements, if both are not possible, you have to print "Not Possible" twice (once for each)
Sample Output 1
1 2 3 4 5 6
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int lenarray = sc.nextInt();
int []aray = new int[lenarray];
for (int i = 0;i<lenarray;i++){
aray[i]= sc.nextInt();
}
for (int i = 0;i<lenarray;i++){
for (int j = 0;j<lenarray;j++){
if (aray[i]<aray[j]){
int temp =aray[i];
aray[i]=aray[j];
aray[j] = temp;
}
}
}
int j = 0;
int []temp = new int[aray.length];
for (int i = 0; i < aray.length-1;i++){
if (aray[i]!=aray[i+1]){
temp[j++] = aray[i];
}
}temp[j++]=aray[aray.length-1];
int[] dummy = new int[j];
for (int i = 0 ;i< dummy.length;i++){
dummy[i]=temp[i];
}
if (dummy.length>=3){
System.out.println(dummy[0]+" "+dummy[1]+" "+dummy[2]);
System.out.println(dummy[dummy.length-3]+" "+dummy[dummy.length-2]+" "+dummy[dummy.length-1]);
}else {
System.out.println("Not Possible");
System.out.println("Not Possible");
}
}
}
No comments:
Post a Comment