Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#Finding the Second Largest Element:
- //You are given a sequence of integers as input, terminated by -1. (That is, the input integers may be positive, negative or 0. -1 in the input signals the end of the input.)
- //-1 is not considered as part of the input.
- //Find the second largest number in the input. You should not use arrays.
- //Sample Test Cases
- //Test Case 1
- //Input
- //-840 -288 -261 -337 -335 488 -1
- //Output
- //-261
- public class Solution {
- public static void main(String[] args) {
- int max=-9999999;
- int secMax=-9999998;
- Scanner in = new Scanner(System.in);
- int n=0;
- while((n=in.nextInt())!=-1){
- if(n>max){
- secMax = max;
- max = n;
- }
- }
- if(secMax!=-9999999){
- System.out.println(secMax);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement