Given an array A[] of length n, find a "missing" number k such that:
kis not inA0<=k<=n
I've seen similar questions asked where the A[] contains numbers 1 to n with one number missing, but in this question, A[] can contain any numbers. I need a solution in O(n) time. For example,
A = {1,2,3} -> 0
A = {0,1,2} -> 3
A = {2,7,4,1,6,0,5,-3} -> 3,8
I've gotten as far as checking if 0 or n are in the array, and if not, return 0 or n, but I cannot seem to think of any other solution. This problem seems considerably more difficult given the fact that A can contain any numbers and not necessarily numbers 1 to n or something like that.
A[]can contain any numbers, then what definesn. Is it the largest value inA? If so, what if the "missing" number is greater than the largest number? This question is underspecified.array A[] of length n, son = A.length, that's okay.