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:
class Solution { public int solution(int[] A); }
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");
long solution(long A[], long 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
long nthPosition = -1;
long nPlusOnePosition = -1;
long i = 0;
while(i < N)
{
long 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(long i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
Segmentation Fault
Segmentation Fault
Segmentation Fault
[3, 1]
[1, 3]
// 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
long nthPosition = -1;
long nPlusOnePosition = -1;
long i = 0;
while(i < N)
{
long 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(long i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
main.o: In function `main': /tmp/user.cpp:114: undefined reference to `solution(std::vector<int, std::allocator<int> >&)' collect2: error: ld returned 1 exit status
// 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
long nthPosition = -1;
long nPlusOnePosition = -1;
long i = 0;
while(i < N)
{
long 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(long i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
[3, 1]
[1, 3]
// 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
long nthPosition = -1;
long nPlusOnePosition = -1;
long i = 0;
while(i < N)
{
long 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(long i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
main.o: In function `main': /tmp/user.cpp:114: undefined reference to `solution(std::vector<int, std::allocator<int> >&)' collect2: error: ld returned 1 exit status
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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)
{
intlo 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;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(55,21): error CS0136: A local variable named `i' cannot be declared in this scope because it would give a different meaning to `i', which is already used in a `parent or current' scope to denote something else
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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)
{
intlo 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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
}
Compilation failed: 10 error(s), 0 warnings Solution.cs(20,13): error CS0246: The type or namespace name `intlo' could not be found. Are you missing an assembly reference? Solution.cs(22,17): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(24,31): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(27,22): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(29,36): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(34,23): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(34,32): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(36,31): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(36,23): error CS0841: A local variable `elem' cannot be used before it is declared Solution.cs(37,25): error CS0841: A local variable `elem' cannot be used before it is declared
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 0;
}
}
[3, 1]
[1, 3]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 1;
}
}
function result: 2
function result: 2
function result: 0
[3, 1]
[1, 3]
[]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
return 1;
}
}
function result: 2
function result: 2
function result: 0
[3, 1]
[1, 3]
[]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
function result: 2
function result: 2
function result: 0
[3, 1]
[1, 3]
[]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
function result: 2
function result: 2
function result: 0
function result: 2
[3, 1]
[1, 3]
[]
[1]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
function result: 2
function result: 2
function result: 1
function result: 2
[3, 1]
[1, 3]
[]
[1]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
function result: 2
function result: 2
function result: 1
function result: 2
[3, 1]
[1, 3]
[]
[1]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
if (N <= 0) { return 1;}
//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(i = 1; i < N; i++)
{
if (A[i] != i)
{
return i;
}
}
}
Console.WriteLine("this is a debug message");
return 1;
}
}
The following issues have been detected: wrong answers.
For example, for the input [1, 2] the solution returned a wrong answer (got 2 expected 3).