#include <iostream> #include <unordered_set> using namespace std; void FindNext(int a[], int b[], int n, int q) { unordered_set<int> seen(a, a+n); for(int i = 0; i < q; ++i) { int nextVal = b[i] + 1; if(seen.find(nextVal) != seen.end()) ++nextVal; b[i] = nextVal; } for(int i = 0; i < q; ++i) cout << b[i] << " "; } int main() { int n, q; cin >> n >> q; int a[n]; for(int i = 0; i < n; ++i) cin >> a[i]; int b[q]; for(int i = 0; i < q; ++i) cin >> b[i]; FindNext(a, b, n, q); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter