This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
algorithm to find a number in which product of number of 4 & 7 is maximum in given range
Let F4(X) be the number of digits 4 in the decimal representation of X, and F7(X) be the number of digits 7 in the decimal representation of X. For example,
F4(456) = 1, F4(444) = 3, F7(1) = 0, F7(747) = 2.
how the below thing can be solved?
find max of {F4(X) ∙ F7(X) : L ≤ X ≤ R}
1 ≤ L ≤ R ≤ 10^18
what is the fastest algorithm for solving this?
1.one can try brute force by finding all numbers between L and R and finding the max product but such an approach will easily time out.
Since it's an homework, I will only give you suggestions in form of questions:
How many digits are there in a number between 1 and 10^18?
Can a digit be a 7 and a 4 simultaneously?
Related
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am getting curious about this question..
How many ways can you choose a subset from the set P = {1, 2, 3, .. n}? The subset S should meet the following condition:
When you choose x (x ∈ P, x is an element of the set P) to create S, you cannot choose a * x and b * x for S.
Constraints :
1 <= n <= 1000
2 <= a < b <= n
b % a != 0 ( b is not divisible by a)
Example :
n = 3 , a = 2, b = 3
so total subsets are 5 ,i.e, {}, {1}, {2}, {3}, {2, 3}
as if in a particular subset there is 1 so 1*2 = 2 and 1*3 cant be there.
so {1,2}, {1,3} and {1,2,3} can't be there
Updated
This is related to sequence A051026 : Number of primitive subsequences of {1, 2, ..., n} in OEIS, the Online Encyclopedia of Integer Sequences.
I don't think there is any easy way to calculate the terms. Even the recursive computations are not trivial, except when n is prime where:
a(n) = 2 * a(n-1) - 1
Both the problem here and "A051026" can be thought of subproblems of a generalization of the above sequence. "A051026" is the instance with (a,b,..) = (2,3,4,5...), e.g. "all the integers >= 2".
I believe it will be easier to calculate the complimentary- that is the number of subsets of S that are not allowed. This is the number of subsets of S for each there is at least one pair (a,b) such that a divides b. After you calculate that number M' simply subtract it from the total number of subsets of S that is 2n.
Now to calculate the number of subsets of S that are not allowed you will have to apply the inclusion-exclusion principle. The solution is not very easy to implement but I don't think there is an alternative approach.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
equal k subsets algorithm
Say I've a set of numbers, I want to divide the numbers into k subsets such that the numbers are evenly distributed. By evenly distributed, I mean the sum of values in the subsets are closest to other sums of other subsets. Can we implement a DP solution to this problem??
Please suggest!
All I can offer is my best attempt, here goes:
It seems to me that if m is the size of your set, then m/k = n; which is the number of elements in each set.
Now I am assuming you are working with integers, lets say we a set, s:
s ={1,2,3,4,5,6,7,8}
Now this is a simple idea that if you set is sorted then the sum of positions
-Sum(0 and last-0) = Sum(1,Last-1) = Sum(2,last-2) = Sum(3,last-3)... and so forth.
the variables would be:
m = 8
k = 2 (just for example)
n = 4
so we want 4 sets:
s1 = 1,8 = Sum is 9
s2 = 2,7 = Sum is 9
s3 = 3,6 = Sum is 9
s4 = 4,5 = Sum is 9
Now there will of course be some trickiness if the set size is odd and/or if k is odd, but these can be dealt with using special cases- implementing the situation that works best for your specific purpose.
Hope this gives you a push in the right, or pretty much any direction.
here is another dynamic programming problem that find the maximum L(chess horse - 4 item) sum in the given matrix (m x n)
For example :
1 2 3
4 5 6
7 8 9
L : (1,2,3,6), (1,4,5,6), (1,2,5,8), (4,5,6,9) ...
and the biggest sum is sum(L) = sum(7,8,9,6) = 30
what is the O(complexity) of the optimal solution ?
it looks like this problem (submatrix with maximum sum)
Say all items are positive
Both positive and negative
Any ideas are welcome!
If your L is constant size (4 elements, as you say), just compute its sum over all < n*m positions and find the maximum one. Repeat for the 8 different orientations you could have. That's O(nm) overall.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Easy interview question got harder: given numbers 1..100, find the missing number(s)
Hi guys, I wasn't sure where to ask this but since this is an algorithmic question here it goes. I've come face to face with a math problem and can't seem to get over it for the last couple of days. It goes like this:
You are given an adding machine that
sums a set of N+1 digits consisting of
positive integers 1 to N as it's given
the numbers (e.g. the machine is given
3 as the first number and outputs 3.
It's then given 6 as the second number
and outputs 9. It's given 11 as the
third number and outputs 20. Etcetera
until it has processed N+1 numbers).
One (and only one) of the digits is
repeated. How do you determine which
number is repeated?
It seems like a trick question and I'd be really annoyed if it is just that a question to which the answer is 'not possible' - any ideas here?
Subtract (1+2+..+N) = N*(N+1)/2 from the sum.
EDIT: in case N is not known, find the largest triangular number smaller than the given sum and subtract.
If you know N and the sum S, the answer is d = S - N*(N+1)/2.
This is because the sum of all numbers from 1 to N is a triangular number, and each number from 1 to N occurs once (except for one that is repeated).
If you do not know N, you can take N = floor((sqrt(8*S+1)-1)/2. That can be deduced from a quadratic equation (n^2 + n)/2 = a.
Ok, you have:
X = 1 + 2 + ... + N + p, where 1<=p<=N
Or
X = N(N+1)/2 + p, 1<=p<=N
Declare:
S(N) = N(N+1)/2
And you know that
S(N) < X < S(N+1), because 1<=p<=N
You can find N, by finding the S(N) such that S(N)X.
If you have found S(N), subtract it from X and you find the duplicate number.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to check if a number is a power of 2
fastest way to find a given number 'n' can be expressed as 2^m
ex: 16= 2^4
naive solution: divide given number by 2 until the remainder becomes 0 (if successful) or less than two (if not successful)
Can someone tell me whats the other fastest way to compute this ?
Fastest way:
if (n != 0 && (n & (n - 1)) == 0)
If the number is a power of two, it will be represented in binary as 1 followed by m zeroes. After subtracting 1, it will be just m ones. For example, take m=4 (n=16)
10000 binary = 16 decimal
01111 binary = 15 decimal
Perform a bitwise "and" and you'll get 0. So it gives the right result in that case.
Now suppose that n is not exactly 2m for some m. Then subtracting one from it won't affect the top bit... so when you "and" together n and n-1 the top bit will still be set, so the result won't be 0. So there are no false positives either.
EDIT: I originally didn't have the n != 0 test... if n is zero, then n & anything will be zero, hence you get a false positive.