Find missing element - arrays

I saw the following question:
Given an array A of integers, find an integer k that is not present in A. Assume that the integers are 32-bit signed integers.
How to solve it?
Thank you
//// update -- This is the provided solution - but I don't think it is correct ////
Consider a very simple hash function F(x)=x mod (n+1). we can build a bit-vector of length
n+1 that is initialized to 0 and for every element in A, we set bit F(A[i]) to 1.Since there are only n elements in the array, we can find the missing element easily.
I think the above solution is wrong.
For example,
A [ 2, 100, 4 ], then both 4 and 100 will match to the same place.

If I'm interpreting the question correctly, (find any integer not in A) and n is the number of elements in A, then the answer is correct as given.
The fact that the hash function can have collisions doesn't really matter; By the pigeonhole principle in reverse, there will be some bit in the bit vector that is not set - because it has more bits than there are elements in A. The corresponding value is an integer not in A. In the case where the hash function has collisions, there will actually be more than one bit not set, which works in favor of the algorithm's correctness rather than against it.
To illustrate, here's how your example would work out:
A = [2, 100, 4]
n = length(A) = 3
f(x) = x mod 4
map(f,A) = [2, 0, 0]
so the final bit vector will be:
[1,0,1,0]
From there, we can arbitrarily choose any integer corresponding to any 0 bit, which in this case is any odd number.

max (items) + 1
springs to mind. Of course it would fail if one of the elements was 2^31-1 and another was -(2^32).
Failing that, sort them and look for an interval.

Since there appear to be no upperbounds or other constraints:
for (int i = int.MinValue; i <= int.MaxValue; i++)
{
if (! A.Contains(i))
{
// found it !
break;
}
}

Related

Find Minimum Operand to Maximize sum of bitwise AND operator

Given an array of integers Arr and an integer K, bitwise AND is to be performed on each element A[i] with an integer X
Let Final sum be defined as follows:
Sum of ( A[i] AND X ) for all values of i ( 0 to length of array-1 )
Return the integer X subject to following constraints:
Final sum should be maximum
X should contain exactly K bits as 1 in its binary representation
If multiple values of X satisfy the above conditions, return the minimum possible X
Input:
Arr : [8,4,2]
K = 2
Output: X=12
12 Contains exactly 2 bits in its binary and is the smallest number that gives maximum possible answer for summation of all (A[i] AND X)
Approach Tried :
Took bitwise OR for all numbers in the array in binary and retained the first K bits of the binary that had 1 , made remaining bits 0, convert back to int
Passed 7/12 Test Cases
Can someone help me out with what mistake am I making with regards to the approach or suggest a better approach ? Thanks in advance.
Consider an input like [ 8, 4, 4, 4 ], K = 1. Your algorithm will give 8 but the correct answer is 4. Just because a given bit is more significant doesn't mean that it will automatically contribute more to the sum, as there might be more than twice as many elements of the array that use a smaller bit.
My suggestion would be to compute a weight for each bit of your potential X -- the number of elements of the array that have that bit set times the value of that bit (2i for bit i). Then find the K bits with the largest weight.
To do this, you need to know how big your integers are -- if they are 32 bits, you need to compute just 32 weights. If they might be bigger you need more. Depending on your programming language you may also need to worry about overflow with your weight calculations (or with the sum calculation -- is this a true sum, or a sum mod 2n for some n?). If some elements of the array might be negative, how are negatives represented (2s complement?) and how does that interact with AND?
Let dp[k][i] represent the maximum sum(a & X), a ∈ array, where i is the highest bit index in X and k is the number of bits in X. Then:
dp[1][i]:
sum(a & 2^i)
dp[k][i]:
sum(a & 2^i) + max(dp[k-1][j])
for j < i
sum(a & 2^i) can be precalculated for all values of i in O(n * m), where m is the word size. max(dp[k-1][j]) is monotonically increasing over j and we want to store the earliest instance of each max to minimise the resulting X.
For each k, therefore, we iterate over m is. Overall time complexity O(k * m + n * m), where m is the word size.

Can I sort an array in O(n) if all values are positive and with digits <= k?

Supposing I have an array A[1 ... n] , and no range for its values apart from the fact that they are positive. If I know they have up to k digits, is it possible to sort array in O(n)?
All examples I have come across for O(n) sorting give an upper bound for the values in the array. If there is a duplicate please let me know.
This depends on whether k is a constant or not.
If your numbers have k digits each, then you do have a bound on the numbers, since they can't be any bigger than 10k - 1. You could therefore use radix sort to sort the integers. The runtime of radix sort is O((n + b)logb U), where n is the number of numbers to sort, b is the base of your radix sort, and U is the maximum value that you're sorting. In your case, that works out to
O((n + b) logb 10k) = O(k(n + b)).
This is where the "it depends" comes in. If k is some fixed number that never changes - say, it's always 137 or something like that - then the above expression reduces to O(n + b), and picking b to be any constant (say, base-2 for your radix sort) gives a runtime of O(n). On the other hand, if k can vary (say, the numbers are allowed to be as big as you'd like them to be, and then after seeing the numbers you work out what k is), then the above expression can't be simplified beyond O(kn) because k is a parameter to the algorithm.
Hope this helps!
If k <= n, you can, otherwise it's not guaranteed.
If I give you n = 4 and k = 5, where would you place an element 3?

Determine whether there exists a contiguous subarray of length > 1 with an integer mean

Given an unsorted array of integers.
I'm trying to come up with an efficient solution (better than O(n2)) but the best I can come up with is an O(n2) solution:
for i from 0 to size of list:
sum = list[i]
for j from i + 1 to size of list:
sum += list[j]
if sum % (j - i + 1) == 0:
return true
return false
I've read stuff on sliding window technique, but it looks like that would be useful for subarrays of a specific length k.
This might be a trick question :) Two odd numbers sum to an even number and two even numbers sum to an even number. The only data set that would not include a contiguous subarray of length two that's also divisible by two would have to alternate [..., odd, even, odd, even, ...]. But then the data set would need to be even further restricted to prevent a subarray of length 4 to be divisible by four, since every other even number is divisible by four.
The probability of receiving such a list is extremely small and continues to reduce as the list gets larger (what's more, it lends itself to a subset of numerical patterns; could those be of interest?), which means unless someone worked painstakingly to create some, most if not all real-world situations would find a solution with a sliding window of size 4 that also checks for alternating parity.

Is it possible to do 3-sum/4-sum...k-sum better than O(n^2) with these conditions? - Tech Interview

this is a classic problem, but I am curious if it is possible to do better with these conditions.
Problem: Suppose we have a sorted array of length 4*N, that is, each element is repeated 4 times. Note that N can be any natural number. Also, each element in the array is subject to the constraint 0 < A[i] < 190*N. Are there 4 elements in the array such that A[i] + A[j] + A[k] + A[m] = V, where V can be any positive integer; note we must use exactly 4 elements and they can be repeated. It is not necessarily a requirement to find the 4 elements that satisfy the condition, rather, just showing it can be done for a given array and V is enough.
Ex : A = [1,1,1,1,4,4,4,4,5,5,5,5,11,11,11,11]
V = 22
This is true because, 11 + 5 + 5 + 1 = 22.
My attempt:
Instead of "4sum" I first tried k-sum, but this proved pretty difficult so I instead went for this variation. The first solution I came to was rather naive O(n^2). However, given these constraints, I imagine that we can do better. I tried some dynamic programming methods and divide and conquer, but that didn't quite get me anywhere. To be specific, I am not sure how to cleverly approach this in a way where I can "eliminate" portions of the array without having to explicitly check values against all or almost all permutations.
Make an vector S0 of length 256N where S0[x]=1 if x appears in A.
Perform a convolution of S0 with itself to produce a new vector S1 of length 512N. S1[x] is nonzero iff x is the sum of 2 numbers in A.
Perform a convolution of S1 with itself to make a new vector S2. S2[x] is nonzero iff x is the sum of 4 numbers in A.
Check S2[V] to get your answer.
Convolution can be performed in O(N log N) time using FFT convolution (http://www.dspguide.com/ch18/2.htm) or similar techniques.
Since at most 4 such convolutions are performed, the total complexity is O(N log N)

Algorithm to determine the maximum sum array by complementing bit mask

This question posed by my co-worker bamboozled me. I cannot even come up with a clean brute-force solution. To state the problem:
Given an array of size n containing non-negative integers, k = [10, 40, 1, 200, 5000, ..., n], a bit mask of size n, i.e. mask = 1001....b_n where |mask| = n. and an integer representing contiguous bits that can be complemented S = 3, find a configuration of mask that yields maximum sum array.
The complement size S is used to pick S contiguous bit from the bit mask and replacing it by its complement.
For example, if mask = 100001 with S = 2 you could
Change mask to 010001 by applying the mask at MSB
You can iteratively keep on complementing at any bit in the mask till you find array of maximum size.
Here is what I've come up:
Find all the 2^n bit mask configurations then apply them to find the maximum sum array
Given the initial mask configuration see if there exists a path to the maximum sum array configuration found in step 1.
Again mine is an exponential solution. Any efficient ones are appreciated.
Start off with the trivial observation that you would never apply your given bitmask G, which simply consists of S 1s, more than once on the same stretch of your original mask, M - this is because bitwise xor is commutative and associative allowing you to reorder as you please, and xor'ing a bitmask to itself gives you all 0s.
Given a bitmask B of length S, and an integral index ind in [0,n), let BestSum(ind, B) be the best possible sum that can be obtained on [ind:n) slice of your input array k when M'[ind, ind + S) = B, where M' is the final state of your mask after performing all the operations. Let us write B = b.B', where b is the MSB and consider the two possibilities for b:
b = M[ind] : In this case, you will not apply G at M[ind] and hence BestSum(ind, B) = b*k[ind] + max(BestSum(ind + 1, B'.0), BestSum(ind + 1, B'.1)).
b != M[ind] : In this case, you will apply G at M[ind] and hence BestSum(ind, B) = b*k[ind] + max(BestSum(ind + 1, (~B').0), BestSum(ind + 1, (~B').1)).
This, along with the boundary conditions, gives you a DP with runtime O(n*2^S). The best solution would be max over all BestSum(0, B).
Note that we have brushed all reachability issues under the carpet of "boundary conditions". Let us address that now - if, for a given ind and B, there is no final configuration M' such that M'[ind, ind + S) = B, define BestSum(ind, B) = -inf. That will ensure that the only cases where you need to answer unreachability is indeed the boundary - i.e., ind = n - S. The only values of (n-S, B) that are reachable at (n-S, M[n-S:n)) and (n-S, M[n-S:n) ^ G), thus handling the boundary with ease.
Would the following work?
Use DFS to expand a tree with all the possibilities (do one flip in each depth), the recursion's ending condition is:
Reached a state of all masks are 1
Keep coming back to the same position, means we can never reach the state will all masks are 1. (I am not sure how exactly we can detect this though.)

Resources