Algorithm/codility

Lesson3. Time Complexity - PermMissingElem

nuKeguyS 2018. 7. 23. 15:49
PermMissingElem - Find the missing element in a given permutation.

https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/

문제요약

길이가 N인 정수 배열 A가 있고, A가 1..(N+1)의 정수를 포함한다고 할 때 배열에 없는 하나의 정수를 찾아라.

Assume that:

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)].

Complexity:

expected worst-case time complexity is O(N);

expected worst-case space complexity is O(1) (not counting the storage required for input arguments).

풀이

1부터 N+1까지의 전체 합에서 A의 각 항목을 빼면 남는 값이 누락된 수