OneCompiler

Second Highest

138

Github Link:- https://github.com/anishkumar127 Twitter Link:- https://twitter.com/anishbishnoixD
import java.util.;
/

output:- Second Highest: 4
TC O(N);
1 4 5 7 8
*/
public class Main {
public static void main(String[] args) {

  int max = 0, second_max = 0, temp, numbers;
    Scanner scanner = new Scanner(System.in);
    
    numbers = scanner.nextInt();
  
    for (int i = 0; i < numbers; i++) {
        if (i == 0) {
            max = scanner.nextInt();
        } else {
            temp = scanner.nextInt();
            if (temp > max) {
                second_max = max;
                max = temp;
            }
            else if(temp>second_max)
            {
             second_max=temp;
            }
        }
    }
    scanner.close();
    System.out.println(second_max);
}

}