Given an array A of size N, you need to find its maximum, 2nd maximum, and 3rd maximum element. Try solving it in O(N) per test case
Input
The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The next line contains N (space separated) elements of A.
Constraints:
1 <= T <= 100
3 <= N <= 10^6
1 <= A[i] <= 10^9
In my code, every test case is passing except one which is showing MLE.
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
while(size>0){
int n = sc.nextInt();
int myarray[] = new int [n];
for(int j=0; j<n;j ) {
myarray[j]= sc.nextInt();
}
printNumber(myarray);
size--;
}
}
public static void printNumber(int [] myarray){
int first=0;
int second=0;
int third=0;
for(int i=0;i<myarray.length;i ){
if (myarray[i] > first){
third=second;
second=first;
first=myarray[i];
}
else if (myarray[i] > second){
third = second;
second = myarray[i];
}
else if (myarray[i] > third)
third = myarray[i];
}
System.out.println(first " " second " " third);
}
}
CodePudding user response:
For the above problem, posted by me I was using a scanner so I think that's why it's showing MLE now I have used a buffered reader now, and it's working.
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args)throws IOException {
// Your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size =Integer.parseInt(br.readLine());
while(size>0){
int n =Integer.parseInt(br.readLine());
String str[] = br.readLine().trim().split(" ");
printNumber(str);
size--;
}
}
public static void printNumber(String [] myarray){
int first=0;
int second=0;
int third=0;
int num =0;
for(int i=0;i<myarray.length;i ){
num = Integer.parseInt(myarray[i]);
if (num > first){
third=second;
second=first;
first=num;
}
else if (num > second){
third = second;
second = num;
}
else if (num > third)
third = num;
}
System.out.println(first " " second " " third);
}
}
CodePudding user response:
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int query=sc.nextInt();
for(int i =0;i<query;i ){
int n=sc.nextInt();
int first = 0;
int second=0;
int third=0;
for(int i =0;i<n;i ){
int curNum = sc.nextInt();
if (curNum > first){
third=second;
second=first;
first=curNum;
}
else if (curNum > second){
third = second;
second = curNum;
}
else if (curNum > third)
third = curNum;
}
System.out.println(first " " second " " third);
}
}
}