Advertisement
webd

FirstLargestThanNeighbors

Apr 9th, 2022
31
282 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class FirstLargestThanNeighbors {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int n = Integer.parseInt(scan.nextLine());
  7.         String line = scan.nextLine();
  8.         String[] array = line.split(" ");
  9.  
  10.         boolean isTrue = false;
  11.         int[] numbers = new int[n];
  12.         for (int k = 0; k < n; k++) {
  13.             numbers[k] = Integer.parseInt(array[k]);
  14.         }
  15.         //Arrays.sort(newArray);
  16.         int currentElement = numbers[0];
  17.         int nextElement = numbers[2];
  18.         for (int i = 1; i < n; i++) {
  19.             int current = numbers[i];
  20.  
  21.             if (current > currentElement && current > nextElement) {
  22.                 System.out.print(i);
  23.                 isTrue = true ;
  24.                 break;
  25.             }
  26.             else {
  27.                 currentElement = numbers[i];
  28.             }
  29.         }
  30.         if (!isTrue) {
  31.             System.out.print("-1");
  32.         }
  33.     }
  34. }
Advertisement
RAW Paste Data Copied
Advertisement