Official

A - Double Click Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) (https://atcoder.jp/contests/typical90) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
「C++入門 AtCoder Programming Guide for beginners (APG4b)」(https://atcoder.jp/contests/APG4b) is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.


Receive the input into a vector with a for statement, check for all \(i\; (1 \le i \le N-1)\) if \(T_{i+1} - T_i \le D\), and print the minimum such \(i\) if exists.

Note that you need to print -1 if there was no such -1.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;
int main() {
  int n,d;
  cin>>n>>d;
  vector<int> t(n);
  for(int i=0;i<n;i++) cin>>t[i];
  for(int i=0;i+1<n;i++){
    if(t[i+1]-t[i]<=d){
      cout<<t[i+1]<<endl;
      return 0;
    }
  }
  cout<<-1<<endl;
}

posted:
last update: