DanikYakush

Task28

Jul 6th, 2022 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. class Task28 {
  2.     public static void findSecondMax(int[] a) {
  3.         int max = a[0];
  4.         for (int i = 0; i < a.length; ++i) {
  5.             if (max < a[i]) {
  6.                 max = a[i];
  7.             }
  8.         }
  9.         boolean check = true;
  10.         int secondMax = 0;
  11.         for (int i = 0; i < a.length; ++i) {
  12.             if (a[i] < max) {
  13.                 if (check) {
  14.                     secondMax = a[i];
  15.                     check = false;
  16.                 } else {
  17.                     if (a[i] > secondMax) {
  18.                         secondMax = a[i];
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.         if (!check) {
  24.             System.out.println(secondMax);
  25.         }
  26.     }
  27. }
Add Comment
Please, Sign In to add comment