Official

A - Buildings Editorial by en_translator


For beginners

Use a for statement to check for each \(i=2,3,\ldots,N\) if \(H_i > H_1\).

If such an \(i\) was found, print that \(i\) and terminate the program at that point. Otherwise, print \(-1\).

Note that most programming languages adopt \(0\)-based indexing.

Sample code (C++):

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for(int i = 0; i < n; i++) cin >> h[i];
  for(int i = 1; i < n; i++) {
    if(h[i] > h[0]) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}

Sample code (Python):

n = int(input())
h = list(map(int, input().split()))
for i in range(1, n):
  if h[i] > h[0]:
    print(i + 1)
    exit()
print(-1)

posted:
last update: