fork(1) 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. bool Negative=false;
  10. int flag;
  11. int i=0;
  12.  
  13. for(i=0;i<A.size();++i)
  14. {
  15. if(A[i]<=0)
  16. {
  17. Negative=true;
  18. std::cout<<"we found a -ve number or 0\n";
  19. }
  20. else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
  21. {
  22. Negative=false;
  23. std::cout<<"comparing @ ="<<i<<" which = "<<A[i]<<'\n';
  24. std::cout<<"comparing @ ="<<i+1<<" which = "<<A[i+1]<<'\n';
  25.  
  26. flag=A[i]+1; //The faulty statement
  27.  
  28. std::cout<<"missing number(A[i]+1) @ ="<<A[i]+1<<'\n';
  29. std::cout<<"missing number(flag) @ ="<<flag<<'\n';
  30. break;
  31. }
  32.  
  33. }
  34. //do something more
  35. }
  36.  
  37. int main() {
  38. std::vector<int> A { -9, 12, -1, 0, 1 };
  39. solve(A);
  40. std::cout << "and we're done\n";
  41. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing @ =3 which = 1
comparing @ =4 which = 12
missing number(A[i]+1) @ =2
missing number(flag) @ =2
and we're done