Modify subset sum [closed] - arrays

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Given a array of N positive integers. Let the minimum element be L and sum of all elements is S.
I need to find out if, for each integer X,(where X is between L and S inclusive) can a subset of the array be chosen such that the sum of elements in this subset is equal to X.
EXAMPLE :
Let N=5 and array is {4,8,2,1,16} . Then here all elements can be made between 1 to 31 so here ans is "yes".
If suppose N=4 and array is {5,1,2,7} . Then for values between 1 and 15 the values 4 and 11 cannot be made. So answer here is "no".

I know to find the minimum number that cant be returned by this array,But dont know to how to solve this problem
First, does the array have only one element? If so, the answer is yes.
Otherwise, find the minimum impossible sum. Is it greater than S? If so, the answer is yes. Otherwise, the answer is no. (If the minimum is less than L, the array doesn't contain 1, and S-1 is an impossible sum.)
To find the lowest impossible sum, we sort the input, then find the lowest impossible sum of each prefix of the array. In Python:
def lowest_impossible_sum(nums):
nums = sorted(nums)
partial_sum = 0
for num in nums:
if num > partial_sum + 1:
return partial_sum + 1
partial_sum += num
return partial_sum + 1
Proof of correctness by induction:
Let A be the sorted array. If A[0] > 1, then 1 is the lowest impossible sum. Otherwise, the elements of A[:1] can produce all sums up to sum(A[:1]).
Suppose for induction that subsets of A[:k] can be selected to produce all sums up to sum(A[:k]).
If A[k] > sum(A[:k]) + 1, then sum(A[:k]) + 1 is the lowest impossible sum; it can't be produced by a subset of A[:k], and adding elements that aren't in A[:k] won't help, as they're all too big.
If A[k] <= sum(A[:k]) + 1, then subsets of A[:k+1] can produce every sum up to sum(A[:k+1]). Every sum up to sum(A[:k]) can already be produced by the inductive hypothesis, and sums from sum(A[:k]) + 1 to sum(A[:k+1]) can be produced by selecting A[k] and a suitable subset of A[:k] adding up to what's left.
Let x be the first index such that A[x] > sum(A[:x]) + 1, or len(A) if there is no such index. By induction, every sum up to sum(A[:x]) is possible. However, whether because x is past the end of the array or because A[x] > sum(A[:x]) + 1, it is impossible to produce the sum sum(A[:x]) + 1. Thus, we need merely search for x and return sum(A[:x]) + 1. That is what the algorithm does.

First sort all elements in the array. If you want to get all the values between L and S from the elements of the array, then L = 1 and the elements should be in the form of 2^i . And the greatest element may not be of form 2^i because the sum need not be of the form (2^i - 1).

Related

Find k missing elements in array of size n-k [duplicate]

This question already has answers here:
Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing
(49 answers)
Closed 1 year ago.
Given an array of n unique integers in [1, n], take random k elements away from the array, then shuffle it to be left with an array of size n-k of integers of size n-k
I want to find those k integers in the best complexity.
If k==1, we can sum all the elements in the array, and the missing element would be the difference between n(n+1)/2 (sum of all numbers from 1 to n) and the sum of the array elements.
I would like to extend this algorithm to k missing elements by k equations, but don't know how to build the equations. Can it be done?
Lets assume a[1], a[2], a[3], ....a[n] are the original unique integers and b[1], b[2],...b[n-k] are the integers after k integers are removed.
Sort the arrays a and b.
For each adjacent pair (i,i+1) in b do a binary search for b[i], b[i+1] in array 'a' and get the indices lets say p, q
If q != p + 1 then all the integers in array a between p, q are among the k integers taken away.
The complexity should be O(n log n )
Yes it can. It is not pretty though.
You need to realize that in a [1, n] range
the sum of squares is n(n + 1)(2n + 1) / 6
the sum of cubes is n**2(n + 1)**2 / 4
etc
The devil is in etc. The general formula of summing kth powers is
Sum(i: [1..n]) i**k = 1/(k+1) Sum(j: [1..k]) (-1)**j binom(k+1, j) Bj n**(k-j+1)
where Bj are the Bernoulli's numbers. It is a polynomial of the k+1th degree.
The Bernoulli numbers are notoriously hard to compute, and the resulting system of equations is not too pleasant to deal with.
Assuming that you overcame all the computational problems, the complexity will be O(nk).

Need help in Algorithms - Finding the outputted maximum sum

I'm currently studying algorithms and I was curious about a homework problem (yeah.. I know) that I have been finding conflicting answers on.
In pseudocode the algorithm is:
Algorithm-1(A:array[1..n] of integer) sum,max:integer
sum = 0
max = 0
for i = 1 to n
sum = 0
for j = i to n
sum = sum + A[j]
if sum > max then
max = sum
return max
Given the array, [-1,2,-3,4,-5,6,7,-8,9,-10] I found that the max value that the above function returns should be 4 based on my interpretation of the algorithm, but online the only two answers I've found are 14 and 10.
My understanding of this algorithm is that it takes the base array, finds its sum, and then takes the same array starting at the next index and finds that sum, and the value that's returned is the highest of these sums.
Basically if given the array of [1,2,-3,4,5] it looks at it this way:
Sum of [1,2,-3,4,5] = 9
Sum of [2,-3,4,5] = 8
Sum of [-3,4,5] = 6
Sum of [4,5] = 9
Sum of [5] = 5
And the final max value returned is 9 since 9 > 8 > 6 > 5
Based on this understanding, I'm thinking that the value of my array is 4 from the [6,7,-8,9,-10] sub array. Would that be correct?
Nope, I suppose you are not understanding the concept of subarrays, Subarrays are basically any set of contiguous elements in an array. Let's take your example.
For
[-1,-2,-3,4,-5,6,7,-8,9,-10]
For every array of size N, there are N*(N+1)/2 Subarrays. The answer in this particular case is the subarray [6,7,-8,9] which returns the sum as 14.
Have a look at the below link for better understanding:
Kadane Algorithm for maximum sum subarray

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)

Prove that all prefix sums are non negative [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
We have an array having n integers whose sum is non negative.
I need to prove that there exists an index i, such that starting from i, all prefix sums are non negative, till we reach i again circularly.
Say the array is a1, a2, a3, ..... , an, such that a1 + a2 + a3 + ..... + an>=0.
So we need to prove that for some index i, all prefix sums are non-negative, i.e,
ai >= 0,
ai + ai+1 >=0,
ai + ai+1 + ai+2 >=0
.
.
ai + ai+1 + ... + an + a1 + .... + ai-1 >=0
I need this for the following question, https://www.interviewbit.com/problems/gas-station/. Though I've already used the above statement in the solution of this question, but I am still not able to prove it.
Suppose we repeat the array multiple times and then construct the prefix sums of this repeated array. The prefix sums will have the same pattern, except each repetition is higher by an amount equal to the sum of the array.
Consider the index x where the prefix sum is the smallest. This will occur within the first n samples (if the sum of the array is positive).
If you start computing prefix sums from this position x, then all subsequent prefix sums will be non-negative by construction.
Given the premise that you need to prove that there exists at least 1 index for which the property holds, I would propose the opposite to be true (i.e. for every index it holds that at least one prefix sum is negative), then deduce a contradiction implicating that it must be true that at least one index holds this property.
Across all pairs of indices {j, k} there exists one such pair with minimal (maximially negative) sum(j, k). Set i = k+1.
If there exists some sequence a(i), a(i+1), ... a(i+n) for n not in the range {j...k} such that sum(i, n) is negative (i.e., a negative prefix sum), then that would mean sum(j, n) < sum(j, k), which contradicts our initial statement. (This is true because sum (j, n) = sum (j, k) + sum (k+1, n) = sum(i, n), and we claim sum (i, n) to be negative.)
If there exists some n in the range {j...k} such that sum(a, n) is negative, then that means that sum(k+1, j-1) + sum(j, n) < 0. As we know the complete sum to be positive, sum(k+1, j-1) + sum (j, k) > 0 (as that includes all elements.) Therefor sum(j, n) < sum(j, k), which contradicts our initial constraint.
QED

fast algorithm of finding sums in array

I am looking for a fast algorithm:
I have a int array of size n, the goal is to find all patterns in the array that
x1, x2, x3 are different elements in the array, such that x1+x2 = x3
For example I know there's a int array of size 3 is [1, 2, 3] then there's only one possibility: 1+2 = 3 (consider 1+2 = 2+1)
I am thinking about implementing Pairs and Hashmaps to make the algorithm fast. (the fastest one I got now is still O(n^2))
Please share your idea for this problem, thank you
Edit: The answer below applies to a version of this problem in which you only want one triplet that adds up like that. When you want all of them, since there are potentially at least O(n^2) possible outputs (as pointed out by ex0du5), and even O(n^3) in pathological cases of repeated elements, you're not going to beat the simple O(n^2) algorithm based on hashing (mapping from a value to the list of indices with that value).
This is basically the 3SUM problem. Without potentially unboundedly large elements, the best known algorithms are approximately O(n^2), but we've only proved that it can't be faster than O(n lg n) for most models of computation.
If the integer elements lie in the range [u, v], you can do a slightly different version of this in O(n + (v-u) lg (v-u)) with an FFT. I'm going to describe a process to transform this problem into that one, solve it there, and then figure out the answer to your problem based on this transformation.
The problem that I know how to solve with FFT is to find a length-3 arithmetic sequence in an array: that is, a sequence a, b, c with c - b = b - a, or equivalently, a + c = 2b.
Unfortunately, the last step of the transformation back isn't as fast as I'd like, but I'll talk about that when we get there.
Let's call your original array X, which contains integers x_1, ..., x_n. We want to find indices i, j, k such that x_i + x_j = x_k.
Find the minimum u and maximum v of X in O(n) time. Let u' be min(u, u*2) and v' be max(v, v*2).
Construct a binary array (bitstring) Z of length v' - u' + 1; Z[i] will be true if either X or its double [x_1*2, ..., x_n*2] contains u' + i. This is O(n) to initialize; just walk over each element of X and set the two corresponding elements of Z.
As we're building this array, we can save the indices of any duplicates we find into an auxiliary list Y. Once Z is complete, we just check for 2 * x_i for each x_i in Y. If any are present, we're done; otherwise the duplicates are irrelevant, and we can forget about Y. (The only situation slightly more complicated is if 0 is repeated; then we need three distinct copies of it to get a solution.)
Now, a solution to your problem, i.e. x_i + x_j = x_k, will appear in Z as three evenly-spaced ones, since some simple algebraic manipulations give us 2*x_j - x_k = x_k - 2*x_i. Note that the elements on the ends are our special doubled entries (from 2X) and the one in the middle is a regular entry (from X).
Consider Z as a representation of a polynomial p, where the coefficient for the term of degree i is Z[i]. If X is [1, 2, 3, 5], then Z is 1111110001 (because we have 1, 2, 3, 4, 5, 6, and 10); p is then 1 + x + x2 + x3 + x4 + x5 + x9.
Now, remember from high school algebra that the coefficient of xc in the product of two polynomials is the sum over all a, b with a + b = c of the first polynomial's coefficient for xa times the second's coefficient for xb. So, if we consider q = p2, the coefficient of x2j (for a j with Z[j] = 1) will be the sum over all i of Z[i] * Z[2*j - i]. But since Z is binary, that's exactly the number of triplets i,j,k which are evenly-spaced ones in Z. Note that (j, j, j) is always such a triplet, so we only care about ones with values > 1.
We can then use a Fast Fourier Transform to find p2 in O(|Z| log |Z|) time, where |Z| is v' - u' + 1. We get out another array of coefficients; call it W.
Loop over each x_k in X. (Recall that our desired evenly-spaced ones are all centered on an element of X, not 2*X.) If the corresponding W for twice this element, i.e. W[2*(x_k - u')], is 1, we know it's not the center of any nontrivial progressions and we can skip it. (As argued before, it should only be a positive integer.)
Otherwise, it might be the center of a progression that we want (so we need to find i and j). But, unfortunately, it might also be the center of a progression that doesn't have our desired form. So we need to check. Loop over the other elements x_i of X, and check if there's a triple with 2*x_i, x_k, 2*x_j for some j (by checking Z[2*(x_k - x_j) - u']). If so, we have an answer; if we make it through all of X without a hit, then the FFT found only spurious answers, and we have to check another element of W.
This last step is therefore O(n * 1 + (number of x_k with W[2*(x_k - u')] > 1 that aren't actually solutions)), which is maybe possibly O(n^2), which is obviously not okay. There should be a way to avoid generating these spurious answers in the output W; if we knew that any appropriate W coefficient definitely had an answer, this last step would be O(n) and all would be well.
I think it's possible to use a somewhat different polynomial to do this, but I haven't gotten it to actually work. I'll think about it some more....
Partially based on this answer.
It has to be at least O(n^2) as there are n(n-1)/2 different sums possible to check for other members. You have to compute all those, because any pair summed may be any other member (start with one example and permute all the elements to convince yourself that all must be checked). Or look at fibonacci for something concrete.
So calculating that and looking up members in a hash table gives amortised O(n^2). Or use an ordered tree if you need best worst-case.
You essentially need to find all the different sums of value pairs so I don't think you're going to do any better than O(n2). But you can optimize by sorting the list and reducing duplicate values, then only pairing a value with anything equal or greater, and stopping when the sum exceeds the maximum value in the list.

Resources