Related
Really stuck the complexity analysis of this problem .
Given digits 0–9 , we need to find all the numbers of max length k whose digits will be in increasing order .
for example if k = 3 , numbers can be 0,00,000,01,02,03,04,.... 1,11,111,12,...
So the question basically that if repetitions allowed for digits,
How many such combinations are possible to find all the numbers less than size k (less than digit length k) such that digits from left to right will be non-decreasing order.
Numbers with at most k digits that are weakly increasing are in 1-1 correspondence with binary strings of length k+10, with exactly ten 1's. The number of consecutive 0s just before the ith one and one in the binary string is the number of i digits in the original number. For example, if k=7, then 001119 maps to 00100011111111010 (2 zeros, 3 ones, 0 twos, 0 threes, ..., 0 eights, 1 nine, 1 digit left over to make the number of digits up to 7).
These binary strings are easy to count: there's choose(k+10, 10)-1 of them (missing one because the empty number is disallowed). This can be computed in O(1) arithmetic operations (actually 10 additions, 18 multiplications and one division).
I don't have enough reputation neither, so I cannot answer Paul's or Globe's answer.
Globe's answer choose(k+9,9) is not perfect, because it only counts the solutions where the numbers have exactly k digits. But the original problems allows numbers with less digits too.
Paul's answer choose(k+10,10) counts these shorter numbers too, but it also allows numbers with zero digits. Let's say k=7 then the following binary string describes a number with no digits: 11111111110000000. We have to exclude this one.
So the solution is: choose(k+10,10)-1
I don't have enough reputation to comment on Paul's answer, so I'm adding another answer. The formula isn't choose(k+10, 10) as specified by Paul, it's choose(k+9, 9).
For instance if we have k=2, choose(2+10, 10) gives us 66, when there are only 55 numbers that satisfy the property.
We pick stars and separators, where the separators divide our digits into buckets from 0 to 9, and stars tell us how many digits to pick from a bucket. (E.g. **|**||*||||||* corresponding to 001139)
The reasoning behind it being k+9 and not k+10 is as follows:
we have to pick 9 separators between 10 digits, so while we have k choices for the stars, we only have 9 choices for the separators.
Given a string, we need to find the largest square which can be obtained by replace its characters by digits (leading zeros are not allowed) where same characters always map to the same digits and different characters always map to different digits. If no solution, return -1.
Consider the string "ab" If we replace character a with 8 and b with 1, we get 81, which is a square.
How to find it for given string ? It is given that string length can be at max 11.
Please help me find a suitable and efficient way
Sorry can't comment, not enough reputation for it so I'll answer here.
#mat7 about what you said in your question comments, no you don't have to do it for every letter from a to z. You only have to do it for the letters present in your string (so at max 12 letters, not 26).
The first thing I would even check is how much different letter you have, if it's 11 or 12 different letters you can directly return -1 since you can't have different letters having the same number.
Now, supposing the input string being "fdsadrtas", you take a new array with only each different letter => "fdsadrt"
And with this array you try all possibilities (exclude the obvious mismatching options, if you set 'f' to 4 and 'd' to 5, 's' can only be 12367890 (and f can never be 0)), this way you will exclude lots of possibilities, having as worst case 10! instead of 12^10. (actually 9*9! with the test of the first one never beeing 0 but it's close enough)
EDIT 2 : +1 samgak nice idea !
The last digit can only be 0,1,4,5,6,9 so the worst number of tests drop even to 9*6*8!
10! is by far small enough to be brute tested, keep the higher square value you found and you are done.
EDIT :
Actually It would work (in a finite reasonable amount of time) but it is the wrong approach now that I have thought about it.
You will use less time in looking all the squares numbers that could be a solution for your string, using the exemple I gave above it's a string of length 9, and checking each square who is length 9 if he could be successfully mapped into the string.
For a string of length 12 (the worst case) you will have to check the square values of 316'228 to 999'999, who is way less than the >2 millions check of the previous proposition. The other proposition might become faster if you start accepting long strings but with only 12 you are faster this way.
We have a char array. All chars in the array are from 0 to 9. For example : 1,9,2,3.
We need to find out the the minimum number of combined chars which is greater than the target value(for example :92), then the 93 is the value what I want.
one example : 1,9,2,3
target : 192
The minimum number which is greater than 192 : 193(i.e.:'1'+'9'+'3').
one more example:2,1,3
target :99
The minimum number which is greater than 99: 123
one more example:2,1,4
target :12
The minimum number which is greater than 12: 14
Please advice &help.
This is not home work, for sure. and there is no order in the char array.
for example:
target:23
the one i want:31
My question:do you need to find all possible combinations(two digit integer/three digit inters/four digit integer) and then find the closest integer to target number.
and length of char array could be 10. the target number could be greater than one million...
No repeat characters are allowed For instance for target 10 will the answer be 12 instead of 11
Any ideas?
Since no repeated digits are allowed, the very first thing to do is to remove repeated digits from the array. Also, sorting the array is a good idea.
If the target has d digits, the solution is either also a d-digit number or a d+1-digit number. If it's a d+1 digit number, it is the smallest you can construct from the array values. That part is very easy:
digit[1] = minimum of nonzero array elements
for p = 2 to d+1:
digit[p] = minimum of array elements not yet taken
If the solution is a d-digit number, its first digit is either equal to the first digit of the target, or it's larger. If it's larger, the constructed number will be larger than the target no matter what the following digits are, so for the remaining digits, you can copy part of the above case. If the first digit of the solution is equal to the first digit of the target, you have reduced the problem to that of finding a solution for a d-1-digit target with a smaller array of eligible digits. You can then recur.
For a dynamic programming approach, preserving the order in the original array, you could work out, after the first N characters, the maximum number possible using only 1,2,3...N characters. Then for the N+1th position the maximum number possible with i characters is either as before, or the previous answer with i-1 characters extended with the current character.
A hack, if you don't have to preserve the order, is to sort the original array.
Example given 1923
At position 1 you care about 1.
At position 2 you care about 19 and 9.
At position 3 you care about 192, 92, and 9.
A the end you care about 1923, 923, and 93.
Further comments:
There is an article on dynamic programming at http://en.wikipedia.org/wiki/Dynamic_programming. The main idea is to solve small problems, and then use those solutions to solve slightly larger problems, and then use those... and so on until you have worked your way up to the problem you actually want to solve.
In your case, you want to find how to take a small number of characters from 1923 so as to make a large number. Suppose you know how to take a small number of characters from 192 to make a large number. In that case, the best solution for 1923 will either be a best solution for 192 or that solution with the 3 that ends 1923 added on. This is because if you had a solution for 1923 that was better than any of the ones you could get as I described, you could get a better solution for 192 by taking it, and perhaps deleting its final character.
Of course, at the beginning you don't know the solution for 192 either, so you have to start at the very beginning, with the solution for 1, and from that work out the best solutions for 19, and then 192, and finally for 1923 - which is what I have shown in the example above.
Finally, I couldn't work out from your question whether e.g. 9321 or 932 are possible solutions. If they are, the problem is easier, but if you really want to you can solve it with much the same method. Just sort 1923 to give 9321 and then solve that as you solved for 1923.
I know the exact sequence and length of my TrueCrypt partition's password, but I can't recall which characters are up-shifted via the Shift key. I wrote a perl script (like CrackTC) that simply tries all passwords from a file but I'm seeking an algorithm to generate the password file quickly. The password is 42 characters so any advice will be helpful.
I realized that I would only have shifted numbers and punctuation, so only 17 of the characters need to be changed.
Note this is not a homework question.
Since each character can be upper or lower that's equivalent to one bit. You have 42 bits. Count in binary and set the character to upper or lower case according to the pattern of the binary bits. 2 to the power 42 is a very large number!! Too large to find by brute force! Good luck!
Assuming that you have to try all combinations, and that each password attempt is separated by a single character, the password file for all possible capitalizations of a 42 character password would be 189 terabytes in length. You probably don't want to work directly from a file.
The algorithm I would choose is a binary counting algorithm, but instead of toggling a single bit, toggle the capitalization of a single character. I would strongly suggest narrowing the pool with rules of thumb that you remember (like: Definitely had more than 5 uppercase, and fewer than 40 uppercase).
Quick python script:
def isShiftable(c):
return not c in "123456789"
def generate(str, i=0):
if len(str) <= i:
print "".join(str)
else:
if isShiftable(str[i]):
str[i]=str[i].upper()
generate(str, i+1)
str[i]=str[i].lower()
generate(str, i+1)
else:
generate(str, i+1)
generate(list("testit123"))
I'm sure it can be cleaned up, and made less bad, but it should suffice.
For 17 shiftable characters, there should be 131072 possibilities. Also, you will need to define your own 'upper'/'lower' function for shifting punctuation.
Not really an answer but it might reduce your search space:
Minimum and maximum of uppercase (could be 4 and 10)
Minimum and maximum sequence of uppercase (could as well be 1 and 1)
Minimum and maximum sequence of lowercase (could be 2 and 15)
Some combinations are more probable than the others, and should be checked first. For instance group combinations by maximum sequence of uppercase and first check those with minimal such value.
42 letter password, and you know the letters, then there are 2^41 (that is: 2 199 023 255 552) unique combinations. If you could check 10m of them in 1 second, then you could brute force it - and it would take at least: 61 hours 5 minutes 2 seconds.
You most likely wont have to do that much work. Let x be the number of uppercase letters:
If you had 7 > x uppercase letters: (42!)/(6! 36!)+(42!)/(5! 37!)+(42!)/(4! 38!)+(42!)/(3! 39!)+(42!)/(2! 40!)+(42!)/(1! 41!) = 6 220 767 combinations.
If you had 6 < x < 9 uppercase letters: (42!)/(8! 34!)+(42!)/(7! 35!) = 145 008 513 combinations.
If you can automate checking process and check 2000 elements in a second, then on worst case:
If you had 7 > x uppercase letters, it would take: 51.84 minutes
If you had 6 < x < 9 uppercase letters, it would take: 20.14 hours
Most likely you need some special case permutation algorithm to efficiently find the unique elements. This is quite complex problem, but I don't see any other way, as iterating over 2^41 elements is nuts.
While watching the rugby last night I was wondering if any scores were impossible given you can only score points in lots of 3, 5 or 7. It didn't take long to work out that any number greater than 4 is attainable. 5=5, 6=3+3, 7=7, 8=3+5, 9=3+3+3, 10=5+5 and so on.
Extending on that idea for 5, 7 and 9 yields the following possible scores:
5,7,9,10,12,14 // and now all numbers are possible.
For 7, 9 and 11:
7,9,11,14,16,18,20,22,23,25,27 // all possible from here
I did these in my head, can anyone suggest a good algorithm that would determine the lowest possible score above which all scores are attainable given a set of scores.
I modelled it like this:
forall a < 10:
forall b < 10:
forall c < 10:
list.add(3a + 5b + 7c);
list.sort_smallest_first();
Then check the list for a sequence longer than 3 (the smallest score possible). Seems pretty impractical and slow for anything beyond the trivial case.
There is only one unattainable number above which all scores are attainable.
This is called the frobenius number. See: http://en.wikipedia.org/wiki/Frobenius_number
The wiki page should have links for algorithms to solve it, for instance: http://www.combinatorics.org/Volume_12/PDF/v12i1r27.pdf
For 2 numbers a,b an exact formula (ab-a-b) is known (which you could use to cut down your search space), and for 3 numbers a,b,c a sharp lower bound (sqrt(3abc)-a-b-c) and quite fast algorithms are known.
If the numbers are in arithmetic progression, then an exact formula is known (see wiki). I mention this because in your examples all numbers are in arithmetic progression.
So to answer your question, find the Frobenius number and add 1 to it.
Hope that helps.