[Hacker Rank] M-th to Last Element

M-th to Last Element

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
long long m;
deque<int> A;
int n;
scanf("%lld",&m);
while(scanf("%d", &n) != EOF) {
A.push_back(n);
if(A.size() > m) A.pop_front();
}
if(A.size() == m) cout<<A[0];
else cout<<"NIL";
return 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/06/PS/HackerRank/m-th-to-last-element/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.