Your browser is not supported. Please, update your browser or switch to a different one. Learn more about which browsers are supported.
Task description
An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
int solution(int A[], int N);
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2 A[1] = 3 A[2] = 1 A[3] = 5the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- the elements of A are all distinct;
- each element of array A is an integer within the range [1..(N + 1)].
Task timeline
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0, i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
func.c: In function 'solution': func.c:11:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token for(int i = 0, i < N; i++) ^ func.c:11:30: error: expected ';' before ')' token for(int i = 0, i < N; i++) ^ func.c:15:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses] else if (elem = N + 1) { nPlusOnePosition = elem; } ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem = (N + 1)) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 2
function result: 3
function result: 2
function result: 4
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 2
function result: 3
function result: 2
function result: 4
function result: 2
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 2
function result: 3
function result: 2
function result: 4
function result: 2
function result: 2
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
N = 1
function result: 1
N = 1
function result: 2
N = 2
function result: 3
N = 2
function result: 2
N = 2
function result: 4
N = 4
function result: 2
N = 2
function result: 2
N = 2
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
for(int i = 0; i < N; i++)
{
int elem = A[i];
if (elem == N) { nthPosition = elem; }
else if (elem == N + 1) { nPlusOnePosition = elem; }
else { A[elem] = elem; }
i = elem;
}
printf("nthPosition = %d\n", nthPosition);
printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
N = 4 nthPosition = -1 nPlusOnePosition = 5
function result: 2
N = 1 nthPosition = 1 nPlusOnePosition = -1
function result: 1
N = 1 nthPosition = -1 nPlusOnePosition = 2
function result: 2
N = 2 nthPosition = -1 nPlusOnePosition = -1
function result: 3
N = 2 nthPosition = 2 nPlusOnePosition = -1
function result: 2
N = 2 nthPosition = -1 nPlusOnePosition = -1
function result: 4
N = 4 nthPosition = -1 nPlusOnePosition = 5
function result: 2
N = 2 nthPosition = -1 nPlusOnePosition = 3
function result: 2
N = 2 nthPosition = -1 nPlusOnePosition = 3
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
A[elem] = elem;
i = elem;
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 1
function result: 1
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 2
function result: 1
function result: 2
function result: 4
function result: 2
function result: 1
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
function result: 2
function result: 1
function result: 2
function result: 1
function result: 2
function result: 4
function result: 2
function result: 1
[1]
[2]
[1, 3]
[2, 3]
[1, 2]
[2, 3, 1, 5]
[3, 1]
[3, 2]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
//printf("N = %d\n", N);
//instead of resizing the array, I will use two ints to contain the Nth and (N+1)th elements
int nthPosition = -1;
int nPlusOnePosition = -1;
int i = 0;
while(i < N)
{
int elem = A[i];
if (elem == N)
{
nthPosition = elem;
i++;
}
else if (elem == N + 1)
{
nPlusOnePosition = elem;
i++;
}
else
{
if (A[elem] != elem)
{
A[elem] = elem;
i = elem;
}
else
{
i++;
}
}
}
//printf("nthPosition = %d\n", nthPosition);
//printf("nPlusOnePosition = %d\n", nPlusOnePosition);
//if the value of nthPosition did not change, then N is the missing number...
if (nthPosition == -1) { return N; }
else if (nPlusOnePosition == -1) { return N+1; }
else
{
// you can replace this with O(log(n)) next time
for(int i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
The following issues have been detected: wrong answers.
For example, for the input [] the solution returned a wrong answer (got 0 expected 1).