confusing question of maximum possible weight of a subsequence - algorithm

The weight of a sequence a0, a1, …, an-1 of real numbers is defined as
a0+a1/2+…+ aa-1/2n-1. A subsequence of a sequence is obtained by
deleting some elements from the sequence, keeping the order of the
remaining elements the same. Let X denote the maximum possible weight
of a subsequence of a0, a1, …,an-1 and Y the maximum possible weight
of a subsequence of a1, a2, …,an-1. Then X is equal to (A) max(Y,
a0+Y) (B) max(Y, a0+Y/2) (C) max(Y, a0+2Y) (D) a0+Y/2
Answer: (B)
Explanation: Using concepts of Dynamic Programming, to find the
maximum possible weight of a subsequence of X, we will have two
alternatives:
Do not include a0 in the subsequence: then the maximum possible weight will be equal to maximum possible weight of a subsequence of
{a1, a2,….an} which is represented by Y
Include a0: then maximum possible weight will be equal to a0 + (each number picked in Y will get divided by 2) a0 + Y/2. Here you can
note that Y will itself pick optimal subsequence to maximize the
weight.
Final answer will be Max(Case1, Case2) i.e. Max(Y, a0 + Y/2). Hence
B).
Why is the 2nd alternative Y/2 using Dynamic programming?
As per my understanding, the alternatives are:
without a0,
= the maximum possible weight of a subsequence of a1, a2, …,an-1 = Y
with a0,
= a0 + the maximum possible weight of a subsequence of a1, a2, …,an-1 = a0 + Y (but in above explaination it takes Y/2. Why?)

According to the question:
the weight of the subsequence a0, a1, a2,..., an-1
is:
X = a0 + a1/2 + a2/4 .... + an-1/2^(n-1)
and, the weight of the subsequence (with a0 not included) a1, a2, a3,..., an-1
is:
Y = a1 + a2/2 .... + an-1/2^(n-2)
Now, to get X from Ywe can observe that:
X = a0 + a1/2 + a2/4 .... + an-1/2^(n-1)
=> X = a0 + (a1/2 + a2/4 .... + an-1/2^(n-1))
=> X = a0 + 1/2(a1 + a2/2 .... + an-1/2^(n-2))
=> X = a0 + 1/2(Y)
Now, applying Dynamic Programming, the max weight of the subsequence a0, a1, a2,..., an-1 will be max(Y, X) = max(Y, a0 + Y/2)
Hence option B is correct.

Without a0, the subsequence sum is
Y = a1 + a2/2 + a3/4 ....
With a0 included, the sum becomes
a0 + a1/2 + a2/4 + a3/8 ... = a0 + [1/2 * (a1 + a2/2 + a3/4 ...)] = a0 + Y/2
So the correct answer would be option B.

Related

Product of distinct prime numbers as a sum of perfect square

Given: k distinct prime numbers say a1, a2, ....., ak
Objective: Minimum number of perfect squares required to write product of the given primes as a sum of perfect squares.
Examples:
k = 2, a1 = 3, a2 = 5
a1*a2 = 15 = 9 + 4 + 1 + 1 i.e sum 4 perfect squares
k = 3, a1 = 2, a2 = 5, a3 = 11
a1*a2*a3 = 110 = 100 + 9 + 1 i.e. sum 3 perfect squares
My Algorithm
let p = a1*a2*...........*ak
counter = 0
while p != 0:
find the largest perfect square <= p say z
p = p-z
counter = counter + 1
return counter
I have tested it for few examples. To me it seems to be correct. But it is incorrect to generalize on the basis of few examples. How to prove this(if algorithm is correct)?
Is the solution right?
Actually, your solution is wrong in these case:
k = 1, a1 = 61 => Your result: 61 = 49 + 9 + 1 + 1 + 1 / Best Result: 61 = 36 + 25
k = 2, a1 = 2, a2 = 37 => Your result: 74 = 64 + 9 + 1 / Best Result: 74 = 49 + 25
Solution using Legendre's three-square theorems
Legendre's Three-square Theorem is the all natural numbers n except n is the form of 4^a (8b + 7) can express sum of three squares.
There is also Lagrange's Four-square Theorem and all natural numbers can express sum of four squares.
So the algorithm is:
Compute whether n is the form of 4^a (8b + 7). You can use prime factorization. If so, the answer is 4.
Compute whether n is a square number. If so, the answer is 1.
Compute whether n can express for two squares. If so, the answer is 2.
If 1-3 is all false, the answer is 3.
You can do operation 1 for O(sqrt(n)), operation 2 for O(log(n)), operation 3 for O(sqrt(n) * log(n)), so the overall time complexity is O(sqrt(n) * log(n)).
EDIT:
Since n is a distinct prime product, so square number is not appeared, then case 2 is not appeared.
Case 1 appears if n mod 8 = 7.

Algorithm to find minimum number of bits to distinguish a set of binary numbers

Say we have K binary numbers (each of same length). We need to find minimum of number of bits needed (need not be continuous) to uniquely identify these K binary numbers. For example 100, 110 can be distinguished by 1 bit (at second position). 111, 110, 101 need 2 bits to be distinguished.
We can see those binaries as a set of linear equations. So, for example
, if we have these binary : 1111, 1100, 1001, we can represent them as follow:
x1 + x2 + x3 + x4 = y1
x1 + x2 + 0 + 0 = y2
x1 + 0 + 0 + x4 = y3
From here, we realize that, we can use Gaussian elimination to reduce those equations to eliminate extra variables (in above example, it is x1). The result of the reduction will be set of K distinct variables, and we remove one extra variable to obtain the result of the original question.
The Minimum Set Cover is defined in terms of a set U, and a set S of subsets of U. Each element in U must be covered by (at least) one of the sets in S.
If you can solve Set Cover, you can solve this problem as well. Suppose you build a set U whose each entry, ui, j (where i < j), corresponds to the pairs (i, j) and (j, i) of your k numbers (hence |U| = k (k - 1) /2). Now build n sets, S1, ..., Sn, corresponding to the n possible bit positions. Set Sj is the subset of all the elements corresponding to pairs position j differentiates. That is, if numbers k, l are different in position j, then set uk, l &in; Sj.
However, this is a NP-hard problem by reduction. By greedy approach, you may get a bounded approximation of minimum number of bits needed but not exactly minimum.

Count the number of triplets a*b=c

The problem is to count the number of triplets such that
a * b = c
where a1 <= a <= a2 and b1 <= b <= b2 and c1 <= c <= c2
Input will be a1,a2,b1,b2,c1,c2
The solution that I can think is to use two nested loop that iterates from a1 to a2 and second one from b1 to b2 and multiply each of them and see if the multiplied value lies in the range c1 to c2 then increment the count.
How to efficiently solve the problem when constraints are very high i.e all a1,a2,b1,b2,c1,c2 can have value between 0 to 1000000000.
Part 1: Solution with O(a2 - a1) complexity.
First, notice that given a certain a, we can easily count the number of b values with b1 <= b <= b2, which also satisfy c1 <= a * b <= c2. The latter condition is equivalent to c1 / a <= b <= c2 / a, thus we need to count the number of integers b which satisfy max(b1, c1/a) <= b <= min(b2, c2/a).
This number is N(a) = floor(min(b2, c2/a)) - ceil(max(b1, c1/a)) + 1 -- relation (1).
The solution to the problem is N(a1) + N(a1 + 1) + ... + N(a2).
This is more efficient than looping over all (a, b) pairs and checking their product, however, it may still not be fast enough for the given magnitude of the inputs -- the complexity is O(a2 - a1). Since the problem is symmetrical in a and b, it might be more advantageous to use the O(b2 - b1) complexity.
In the following two parts I will describe a more efficient solution.
Part 2: Reducing the problem to simpler ones.
Let us denote as N(a1, a2, b1, b2, c1, c2) the value that we need to calculate.
Notice that we can reduce the problem into two problems with c1 = 0, using:
N(a1, a2, b1, b2, c1, c2) = N(a1, a2, b1, b2, 0, c2) - N(a1, a2, b1, b2, 0, c1 - 1).
We can take this further and reduce a problem where c1 = 0 into two problems where b1 = 0 and c1 = 0. This can be done using:
N(a1, a2, b1, b2, 0, C) = N(a1, a2, 0, b2, 0, C) - N(a1, a2, 0, b1 - 1, 0, C).
Similarly, we can reduce a problem where b1 = 0 and c1 = 0 into two problems with a1 = 0, b1 = 0, c1 = 0.
Therefore, it is enough to solve simpler problems which require to compute values of the following form: N(0, A, 0, B, 0, C), i.e. we need to count the number of triplets of natural numbers (a, b, c), with c = a * b, a <= A, b <= B, c <= C.
Part 3: Solution with O(sqrt(c2)) complexity.
One next useful observation is that since a * b = c <= C, at least one of the following relations is true: a <= sqrt(C), or b <= sqrt(C) -- observation (2).
In the first part of the proof (relation 1) it was shown that we can efficiently calculate (in O(1)) the number of good b values if a is fixed. Using that relation, we can efficiently count the number of triples with a <= sqrt(C) -- in O(sqrt(C)).
What remains to do is to calculate the number of triplets with a > sqrt(C). According to observation (2), we know that in this case it is required to have b <= sqrt(C).
Thus, for any b in {0, 1, 2, ..., sqrt(C)} we have to count the number of good a values such that sqrt(C) < a < A. We can once again apply relation (1) (with reversed roles for a and b this time -- we are now given b and calculate the number of good a values, which is subject to the constraint of belonging to a certain interval). For each b in {0, 1, 2, ..., sqrt(C)}, we can count the number of good a in O(1) -- therefore the complexity for this case is again O(sqrt(C)).
Using the above results, we get an overall complexity of O(sqrt(C)). Returning to the initial problem, this involves an O(sqrt(c2)) complexity.
Use the fac that ab = ba. I.e. if you have tried a=2, b=3, there is no sense in also trying a=3, b=2.
Also, once a*b > c2, there is no sense in increasing a or b.

Counting subarray have sum in range [L, R]

I am solving a competitive programming problem, it was described like this:
Given n < 10^5 integer a1, a2, a3, ..., an and L, R. How many
subarrays are there such that sum of its element in range [L, R].
Example:
Input:
n = 4, L = 2, R = 4
1 2 3 4
Output: 4
(4 = 4, 3 = 1 + 2 = 3, 2 = 2)
One solution I have is bruteforce, but O(n^2) is too slow. What data structures / algorithms should I use to solve this problem efficiently ?
Compute prefix sums(p[0] = 0, p[1] = a1, p[2] = a1 + a2, ..., p[n] = sum of all numbers).
For a fixed prefix sum p[i], you need to find the number of such prefix sums p[j] that j is less than i and p[i] - R <= p[j] <= p[i] - L. One can do it in O(log n) with treap or another balanced binary search tree.
Pseudo code:
treap.add(0)
sum = 0
ans = 0
for i from 1 to n:
sum += a[i]
left, right = treap.split(sum - R)
middle, right = right.split(sum - L)
ans += middle.size()
merge left, middle and right together
treap.add(sum)
We can do it in linear time if the array contains positive numbers only.
First build an array with prefix sum from left to right.
1. Fix three pointers, X, Y and Z and initialize them with 0
2. At every step increase X by 1
3. While sum of numbers between X and Y are greater than R keep increasing Y
4. While sum of numbers between X and Z are greater than or equal to L, keep increasing Z
5. If valid Y and Z are found, add Z - Y + 1 to result.
6. If X is less than length of the array, Go to step 2.

Existence of a permutation under constraints (Interview Street - Manipulative Numbers)

I am trying to solve this problem: https://www.interviewstreet.com/challenges/dashboard/#problem/4f9a33ec1b8ea
Suppose that A is a list of n numbers ( A1, A2, A3, ... , An) and B ( B1, B2, B3, .. ,Bn ) is a permutation of these numbers. We say B is K-Manipulative if and only if its following value:
M(B) = min( B1 Xor B2, B2 Xor B3, B3 Xor B4, ... , Bn-1 Xor Bn, Bn Xor B1 ) is not less than 2^K.
You are given n number A1 to An, You have to find the biggest K such that there exists a permutation B of these numbers which is K-Manipulative.
Input:
In the first line of the input there is an integer N.
In the second line of input there are N integers A1 to An
N is not more than 100.
Ai is non-negative and will fit in 32-bit integer.
Output:
Print an integer to the output being the answer to the test. If there is no such K print -1 to the output.
Sample Input
3
13 3 10
Sample Output
2
Sample Input
4
1 2 3 4
Sample Output
1
Explanation
First Sample test
Here the list A is {13, 3, 10}. One possible permutation of A is, B = (10, 3, 13).
For B, min( B1 xor B2, B2 xor B3, B3 xor B1 ) = min( 10 xor 3, 3 xor 13, 13 xor 10 ) = min( 9, 14, 7 ) = 7.
So there exist a permutation B of A such that M(B) is not less than 4 i.e 2^2. However there does not exist any permutation B of A such that M(B) is not less than 8 ie 2^3. So the maximum possible value of K is 2.
==================================================================================
Here are the attempts I have made so far.
Attempt 1: Greedy Algorithm
Place the input in an array A[1..n]
Compute the value M(A). This gives the location of the min XOR value (i, (i + 1) % n)
Check whether swapping A[i] or A[(i + 1) % n] with any other element of the array increases the value of M(A). If such an element exists, make the swap.
Repeat steps 2 & 3 until the value M(A) cannot be improved.
This gives a local maxima for sure, but I am not sure whether this gives the global maxima.
Attempt 2: Checking for the existence of a permutation given neighbor constraints
Given input A[1..n], For i = 1..n and j = (i+1)..n compute x_ij = A[i] XOR A[j]
Compute the max(x_ij). Note that 2^p <= max(x_ij) < 2^(p+1) for some p.
Collect all x_ij such that x_ij >= 2^p. Note that this collection can be treated as a graph G with nodes {1, 2, .. n} and nodes i and j have an undirected edge between them if x_ij >= 2^p.
Check whether the graph G has a cycle which visits each node exactly once. If such a cycle exists, k = p. Otherwise, let p = p - 1, goto step 3.
This gives the correct answer, but note that in step 4 we are essentially checking whether a graph has a hamiltonian cycle which is a very hard problem.
Any hints or suggestions?
It is possible to solve this problem without going deep into graph theory.
Key inference
The property suggested by rich, is the key to solving this problem:
Following up on my comment: B1 Xor B2 < 2^K if and only if B1 and B2
agree on all but the K low order bits
Based on the above property, we only need to find the highest k for which there are at most n/2 occurrences of each unique higher order bits for every A[i].
In other words, amongst the group of values A[i] >> k, iff each of those values is repeated at most n/2 times, there exists a k-manipulative permutation with all the XOR values >= (2^k).
Why n/2
Suppose if you do have more than n/2 occurrences of any of the unique higher order bits, it is NOT possible to obtain a permutation B, with all the cyclic XORs being non-zero, i.e. there will be at least one B[i] XOR B[(i+1) % N] with all the higher order bits becoming zero, hence making M(B) < 2^k
Pseudocode
k = -1
for (bit = (MAX_BITS - 1) to 0) {
HashMap count
for (i = 0 to N - 1) {
higherOrderVal = A[i] >> bit
if (higherOrderVal exists in count) {
count[higherOrderVal] += 1
}
else {
count[higherOrderVal] = 1
}
}
isValid = true
for (element in count) {
if (element > N/2) {
isValid = false
break
}
}
if (isValid) {
k = bit
break
}
}
The time complexity for this solution is O(M * N) where M is the constant factor representing the maximum number of bits used to represent the numbers (32-bits, 64-bits, etc.), and N is the size of the input array A.
Following up on my comment: B1 Xor B2 < 2^K if and only if B1 and B2 agree on all but the K low order bits, so G has the very special structure of being complete multipartite, with partition labels consisting of all but the K low order bits. A complete multipartite graph is Hamiltonian if and only if there is no majority partition. Plug this fact into Attempt 2.
Greedy Approach #priority_queue
firstly insert all pairs xor value and consecutive index i and j ->pq.push(tuple(v[i]^v[j],i,j))
now pop the first maxm xor value and set the both index i and j
now again pop that maxm xor value which is comes from the i or j
this operation perform up to 1 to n
then return nth poped xor value

Resources