fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. void solve(std::vector<int>& A)
  6. {
  7. std::sort(A.begin(),A.end()); //sorting vector
  8.  
  9. int expected = 0;
  10. int missing = 0;
  11.  
  12. for(int i = 1; i < A.size(); ++i)
  13. {
  14. if (A[i] <= 0) {
  15. std::cout << "we found " << A[i] << "\n";
  16. continue;
  17. }
  18.  
  19. if (A[i-1] <= 0) {
  20. expected = 1;
  21. } else {
  22. expected = A[i-1] + 1;
  23. }
  24. if (A[i] == expected)
  25. continue;
  26.  
  27. std::cout << "A[" << i-1 << "] = " << A[i-1] << '\n';
  28. std::cout << "expecting " << expected << '\n';
  29. std::cout << "A[" << i << "] = " << A[i] << '\n';
  30.  
  31. missing = expected;
  32. std::cout << "missing number: " << expected << '\n';
  33.  
  34. break;
  35. }
  36. //do something more
  37. }
  38.  
  39. int main() {
  40. std::vector<int> A { -9, 12, -1, 0, 1 };
  41. solve(A);
  42. std::cout << "and we're done\n";
  43. }
  44.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
we found -1
we found 0
A[3] = 1
expecting 2
A[4] = 12
missing number: 2
and we're done