Advertisement
JagadeeshUndavalli

Untitled

Jun 14th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. //#Finding the Second Largest Element:
  2. //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.)
  3. //-1 is not considered as part of the input.
  4. //Find the second largest number in the input. You should not use arrays.
  5. //Sample Test Cases
  6. //Test Case 1
  7. //Input
  8. //-840 -288 -261 -337 -335 488 -1
  9. //Output
  10. //-261
  11.  
  12. public class Solution {
  13.  
  14.     public static void main(String[] args) {
  15.         int max=-9999999;
  16.         int secMax=-9999998;
  17.         Scanner in = new Scanner(System.in);
  18.         int n=0;
  19.         while((n=in.nextInt())!=-1){
  20.             if(n>max){
  21.                 secMax = max;
  22.                 max = n;
  23.             }
  24.         }
  25.         if(secMax!=-9999999){
  26.             System.out.println(secMax);
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement