# Daily: Find missing array value | C++

## aedrarian

### December 23rd, 2021 07:21:53 PM

```					/*
Good morning! Here's your coding interview problem for today.

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.
*/
#include <iostream>
using namespace std;

int calcMissing(int* input, int size)
{
int sum = 0;
int n = 1; //add one to account for missing value
for(int i = 0; i < size; i++)
{
if(input[i] > 0)
{
sum += input[i];
n++;
}
}

//If no numbers higher than 0, answer is 1
if(sum == 0)
return 1;

return (n*(n+1)/2) - sum; //Formula is expectedSum - actualSum
/* expectedSum = n*(n+1)/2, the formula for sum(1, n) */
}

int main()
{

cout << calcMissing(new int[4]{3, 4, -1, 1}, 4) << endl;
cout << calcMissing(new int[3]{1, 2, 0}, 3) << endl;

//No positive numbers
cout << calcMissing(new int[1]{0}, 1) << endl;
}
```