I searched on net. The solution is at link.
The thing which I could not get was :
Let us consider n’th element, it can be included in all subsets of remaining (n-1) elements. The number of subsets for (n-1) elements is equal to 2^(n-1).
What is it trying to say?
Got it. To count the number of times an element comes in the subsets of the set ... we fix the element and start counting number of subsets of length :
1 -> 1
2 ->NC1
3 ->NC2
.
.
.
N ->NC(N-1)
Total number of times an element appears in subsets of a given set = total number of subsets containing the element = 1 + NC1 + NC2 + NC3 + ..... + NC(N-1)
= 2^(N-1).
Related
I have asked this problem on math.stackexchange, but I didn't get any answer.
The problem is to solve a two-player number-guessing game. Let the first person be A and the second person be B. A chooses a number x between 1 and an upper limit n. B gives queries to A in form of (L,R) and A will answer with Yes/NO if the number exists in the interval (L,R) both inclusive.
B will need to ask a set of queries to uniquely determine the number x. So the problem is to find the number of such distinct sets of queries such that B will be able to uniquely determine x, irrespective of what the value of x. A returns the answers to the queries as a whole series -- B gets the yes/no responses in a batch, only after making all of the queries.
For example, let's say n is 2. The sets of possible queries would be,
{(1,1)},
{(2,2)},
{(1,1),(2,2)},
{(1,1),(1,2)},
{(2,2),(1,2)},
{(1,1),(2,2),(1,2)}
Question: How can I determine which of these query sets will uniquely identify any integer that A might choose?
I could think was the we need to basically isolate all the possible numbers from 1 to n in some way, otherwise it's not possible to uniquely determine the number. But I have no idea what to do with this information.
Set #1 to detect a number:
1 set with all single pairs: {{1,1},{2,2},...,{N,N}}
Set #2 to detect a number:
N sets with single pairs except one pair: {{1,1},{2,2},..,{x-1,x-1},{x+1,x+1},..,{N,N}}
Pairs that can't help to identify number:
N-1 pairs with length 2: {1,2},{2,3},,{N-1,N}
N-2 pairs with length 3: {1,3},{2,4},,{N-2,N}
...
2 pairs with length N-1: {1,N-1},{2,N}
1 pairs with length N: {1,N}
Total number of useless pairs is:
K = (N-1) + (N-2) + ... 2 + 1 = N*(N-1)/2
Total number of useless sets is:
Z = C(K,0) + C(K, 1) + ... + C(K, K) = 2^K
Number of sets of queries
To find the answer we need to combine all correct sets with all other types of sets.
ANSWER = (Number of set #1 + Number of sets #2) * Z = (1 + N) * (2^K)
UDP: Answer is wrong, see comment below
For example, for N = 3.
The permutations are:
[1,3,2]
[2,3,1]
Note: [1,2,3] and [3,2,1] are not valid here because [1,2,3] increases but doesn't decreases and vice-versa for [3,2,1].
I got this problem in TCS CodeVita 2017, they didn't even provide the editorial for this.
All these permutations have number N somewhere in the middle
All numbers that less than N can be divided into two groups: left and right. The left group is in increasing order, the right group is in decreasing order.
The left and the right groups could not be empty.
The answer will be equal to the number of different left groups because this group should immediately be followed by N and all remaining numbers in decreasing order.
The left group can contain all numbers besides N. And it can neither be empty nor contain all N-1 numbers.
Therefore, the answer is the number of subsets of numbers {1, 2, ..., N-1} minus two corner cases. That is 2^(N-1) - 2.
The algorithm would be as follows
The peak element would always be N
N cannot be at any of the 2 ends, so We can place it at N-2 positions
suppose N was at the i'th position, we can select i-1 numbers to be at the left side. these would be placed in a sorted manner, the other elements would simply be placed in reverse order after N
Number of ways to select i elements from n = nCi, we have to select (i-1) elements from (n-1) elements
N can be at any index from i = 2 to N-1 (assuming index starts from 1)
Answer would be
(n-1)C1 + (n-1)C2 + (n-1)C3 + (n-1)C4 + ....(n-1)Cn-2 = 2^(n-1) - 2 // -2 handles the case for n-1C0 and n-1Cn-1
We are given 2D matrix array (let's say length i and wide j) and integer k
We have to find size of smallest rectangle, that contains this or greater sum
F.e k=7
4 1
1 1
1 1
4 4
Anwser is 2, because 4+4=8 >= 7, if there wasn't last line, anwser would be 4, 4+1+1+1 = 7 >= 7
My idea is to count prefix sums Pref[k,l]=Tab[k,l]+Pref[k-1,l]+Pref[k,l-1]
And then compare every single rectangle
Is this possible to make it faster? My idea is T(n)=O(n^2) (Where n is number of elements in matrix)
I would like to do this in time n or n * log n
I would be really glad if someone would give me any tip how to do this :)
First, create an auxillary matrix: sums, where:
sums[i,j] = A[0,0] + A[0,1] + .... + A[0,j] + A[1,0] + ... + A[1,j] + ... + A[i,j]
I think this is what you meant when you said "prefix matrix".
This can be calculated in linear time with dynamic programming:
sums[0,j] = A[0,0] + ... + A[0,j]
sums[i,0] = A[0,0] + ... + A[i,0]
sums[i,j] = sums[i-1,j] + sums[i,j-1] - sums[i-1,j-1] + A[i,j]
^
elements counted twice
Now, assuming all elements are non negative, this is non decreasing, matrix, where each column and each row are sorted.
So, iterating the matrix again, for each pair of indices i,j, find the value closest yet smaller than sum[i,j]-k.
This can be done in O(sqrt(n)).
Do it for each such (i,j) pair, and you get O(n*sqrt(n)) solution.
I am working on this project where the user inputs a list of numbers. I put these numbers in an array. I need to find a set of numbers with a given length whose sum is divisible by 5.
For example, if the list is 9768014, and the length required is 6, then the output would be 987641.
What algorithm do I need to find that set of numbers?
You can solve this by dynamic programming. Let f(n,m,k) be the largest index between 1 and n of the number in a subset of indices {1,2,....,n} that gives a sum of k mod 5 that uses m numbers. (It's possible that f(n,m,k) = None). You can compute f(n+1,m,k) and f(n,m+1,k) if you know the values of f(N,M,k) for all N <= n + 1 and M < m and also for all N <= n and M < m + 1 and also for N=n,M=m, and all k = 0,1,2,3,4. If you ever find that f(n,m,0) has a solution where m is your desired number of numbers to use, then you're done. Also you don't have to compute f(N,M,k) for any M greater than your desired count of numbers to use. Total complexity is O(n*m) where n is the total count of numbers and m is the size of subset that you are trying to reach.
The problem statement is following:
Given N. We need to find x1,x2,..,xp such that N = x1 + x2 + .. + xp, p must be minimum(means number of terms in the sum) and we also must be able to get all the numbers from 1 to (N-1) from the sum of the subset of (x1,x2,x3..xp).And numbers in the set might be repeated also.
For example if N=7.
7 = 1+2+4
And 6= (2,4) , 5= (4,1), 4 = (4),3=(1,2) and so on.
Example 2:
8 = 1+2+4+1
Example 3:(invalid)
8 = 1+2+5
But we can't get 4 from the subset of (1,2,5).So (1,2,5) is not a valid combination
My approach is if 'N-1'can be written as sum of p terms than 'N' either have p or p+1 terms. But that approach will require to check all possible combinations which sums up to "N-1" and have "p" terms. Can anyone has better solution other than this?
Solution:
Step1:
Assume that we got "K" entries in our set as our answer. Therefore we can obtain 2^K different numbers of sums from these numbers because each entry either will appear or not appear in the sum. And also if the the number is "N", we need to compute the sum for '1' to 'N'. Therefore (2^K -1) = N K=log(N+1)
Step2:
After the step1, we know that our answer must include "K" entries but what these entries actual are? Assume that our entries are (a1,a2,a3...ak). So number P can be written as
P = a1*b1 + a2*b2 + a3*b3....+ ak*bk. Where all b[i] = 0 or 1. Here, we can see P as a decimal representation of binary number (b1 b2 b3 bk), therefore we can take a[i] = 2^(i-1).
You should take all numbers 1,2,4 ....2^k, N-(1+...+2^k). (The last one only if it doesn't equal to 0)
Proof
First of all, if we only get k numbers, we can get maximum 2^k - 1 different sums except 0. So if N>=2^k, We need at least k + 1 numbers. So you can see that if our group of numbers correct it's minimum by size(or one of the minimums)
It's easy to see that we can get any number from 0 to 2^(k+1) - 1 using first numbers. What If we need more? We just get last number because it's less than 2^(k + 1). And get difference using first elements
I haven't run out the numbers on this, but you should be very very interested in the fact that you have listed the first three powers of two.
If I were looking for a better solution, that's where I'd start.