# Program to return the smallest positive integer (greater than 0) that does not occur in A vector [closed]

I am learning c++ and decided to do codility demo test. I have spent a few hours thinking about my program and developing this code:


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

bool isNegative(int i)
{
return i < 0;
}

int solution(vector<int>& A) {
int size = A.size();
bool all_negative_flag = 0;
std::sort(A.begin(), A.end());//Sorting the vector in ascending order

for (int i = 0; i < size; i++) {     //check if all integers are negative
if (A[i] > 0) {
all_negative_flag = 1;
goto check;
}
}

check:
if (all_negative_flag == 0) {
return 1;     //returns 1 if all integers are negative
}

// If we get to this point, that means we either only have all positive numbers or mix of positive and negative. Since the result should be a positive number, discard all negative numbers
A.erase(remove_if(A.begin(), A.end(), isNegative), A.end());
for (int i = 0; i < (size - 1); i++) {
if (A[i + 1] - A[i] > 1) { // if next vector number is highte than the previous by 2, that means there is a match.
return A[i] + 1;
}

}
// if nothing matched at this point, that means all vector numbers go in order. Return the last one +1
return A[size - 1] + 1;;
}


The codility score is: Example inputs and expected outputs:

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

A function int solution(vector& A) is supposed to take 1 parameter (vector) with numbers within the range [−1,000,000..1,000,000]

My idea of this program is very simple. I simply check if all numbers are negative. If that is the case the smallest positive integer will be 1. If there is mixture of positive and negative numbers, I discard all negative numbers because I am only interested in smallest positive integer. After all negative numbers are discarded, I loop through each number in the array and compare it to the one before. If the (next number - previous number) > 1, that means that the (previous number + 1) will be the smallest.

I would like to get some advice on how can I improve this program without changing the logic too much. I would also like to know what is the issue with the correctness of this code since I only scored 20%? Thanks in advance for all the tips and help.

• Please provide example input and expected output. For an example of questions of this nature please use the search bar to search for [programming-challenge]. We only review code that is working as expected, there are other sites that will help you debug your code. Please read Where can I get help? and How do I ask a good question?. Jul 30 at 12:31
• Welcome to Code Review! You are so close to a good question, but we only review working code, meaning that we don't help you pass code challenges. once you have passed the code challenge, you can totally post that code and ask us what you could do better.
– Malachi
Jul 30 at 13:54
• @pacmaninbw Hello. My code is working as expected, I just want to review this and understand how can I improve it :) I have updated my initial question and posted example input and expected output Jul 30 at 16:03
• @Malachi Hello, could you clarify to me please. This is working code, it is just not passing 100% according to codility hence I am trying to get help review it and find what I can improve. I have added a codility screenshot that I have passed it with 55%. Is this a wrong site to get help improve my code? Jul 30 at 16:06
• Code is not working correctly because it passes only 1 out of 5 correctness tests. Issues: if the smallest value in the array is 2, you need to return 1, but you don’t. You also need to remove the 0 from the array. Jul 30 at 19:28