fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. // read numbers
  9. std::istream_iterator<int> numbers {std::cin}, eof;
  10. std::vector<int> arr(numbers, eof);
  11.  
  12. // mark numbers in [1, n] range that are in the array
  13. size_t n = arr.size();
  14. std::vector<bool> m(n+1, false);
  15. for (int x : arr)
  16. if (1 <= x && x <= n)
  17. m[x-1] = true;
  18.  
  19. // find the first absent number (position + 1)
  20. auto it = std::find(std::begin(m), std::end(m), false);
  21. std::cout << (std::distance(std::begin(m), it) + 1) << std::endl;
  22. }
  23.  
Success #stdin #stdout 0s 4412KB
stdin
1 2 3
4
stdout
5