Does radix sort work with numbers who have different number of digits? - algorithm

I know that radix sort works by comparing the digits of the numbers. My question is, assume we have different numbers with different number of digits. Does radix sort work here? We can simply assume that, for example, if we are comparing two numbers, one with 3 digits and one with 6 digits, the first 3 digits of the smaller number is 0. But how about the implementation? How can we make the program assume that if there are not enough digits, then those digits are zero?
Thank you.

You need to somehow add or simulate those nonexistent digits or sort the numbers in groups, each of which containing only numbers of the same length.
These 3 numbers
9912
999
123
can be transformed into
9912
0999
0123
and sorted using the regular radix sort or they can be sorted as 2 independent groups:
9912
and
999
123
The latter will give you (assuming ascending order)
123
999
and the former stays the same. Then you combine the sorted groups (from shorter numbers to longer numbers):
123
999
9912
That's all.

Assuming you have the number in an integer variable, then you can extract the digits like this (n = 0, 1, 2, ...):
digit = (number / radix ^ n) % radix

Related

Simple math task: 2 numbers given, we have to find the third. I just need a formula for it

I need a formula for counting the number of combinations within a given limit of numbers. There must only be 2 numbers given, we have to find the third.
For example, for 2(number of repetitions) and 3(limit number), the result would be 3, because there are 3 combinations for the digits: 1 and 2, 1 and 3, 2 and 3.
For 2 and 4 the result is 6,
For 3 and 5 the result is 10,
For 6 and 7 the result is 7, etc.
The first number has to be smaller than the second.
A formula is needed for figuring out the result, if the first number is A, the second is B, what would C is going to be?
You're describing combination. The formula is going to be C = B! / (A!*(B-A)!) (where ! is the factorial operation). It's also worth noting that the first number can be equal to the second -- there should only be one repetition in that case. By convention 0! == 1 and it is OK where both numbers are equal because C(n, n) = 1 and this means n!/(n! * 0!).
Unfortunately, since factorial grows very quickly (21! is too large for a 64-bit unsigned integer), you probably can't compute this directly. Wikipedia has a few algorithms you can use here.

Dynamic Programming to solve Permutation

I have four digits, "1", "2", "3", "4".
The input of the program is an integer which can only comprise of the above 4 digits. There is going to be a lot of inputs.
Example of inputs: 1123, 4123, 4444
I need to calculate the number of permutations of a given input that adheres to the following rules:
No two similar digits should be adjacent to each other. Example: 1223 is not allowed but 2123 is allowed.
The start end end digits should not be the same. They are considered as being circularly adjacent. Example: 2132 is not allowed.
If the input is 4 digits of length, your resulting permutation should also be of 4 digits of length.
Could I use any type of memoization to solve this problem? How do i store it in a 2d array? Do give tips thanks!
Since you are only interested in the number of allowed arrangements, most of the inputs lead to identical results.
the order of the digits in the input does not matter
only the frequency distribution of digits is important, i.e. 1123 and 1223 lead to the same answer.
Classifying the inputs according to digit frequencies leads to just 5 different cases for four digit inputs:
class examples
4 4444, 2222, ...
3 1 1211, 2232, ...
2 2 1331, 4422, ...
2 1 1 3413, 1123, ...
1 1 1 1 1234, 4231, ...
Once you have figured out the answer for each case, any new input can be handled very fast.

Find largest multiple for a number set

An array of digits(0-9) of size N is provided as input. A set of numbers(N1,...,Nm) of size m with the numbers separated by space is also as the input. The program has to print the largest number that can be formed using the digits in the array of size N that is divisible by the numbers N1,..,Nm
Example Input/Output1:
INPUT:
160
2 3 5
OUTPUT
60
Explanation
60 is the largest number that can be formed using the digits 1,6,0 which is divisible by 2,3,5
Example Input/Output2:
Input
91028
17 5 9
Output
9180
Boundary Conditions
1<=m<=5
2<=N<=50
Can somebody explain how to approach this problem.
Partial answer:
Try all permutations of all subsets of your digits, probably starting with the largest candidates.
If your factors contain 5 the last digit must be 0or 5
If your factors contain 3 or 6 or 9 the sum of all candidate digits muts be a multiple of 3
If your factors contain 2 or 4 or 6 or 8 the last digit must be even.
And so on.

An efficient algorithm to compute the number of '1' bit in a long decimal integer that is represented in string?

I came across this interesting question today. (Note that this is not for my homework or interview, etc.)
Given a decimal number that is represented in string, we want to compute the number of '1' bits for the large number in binary format. Here the string can have thousands of characters, and cannot be represented with one int or long long variable.
For example, countBits("10") = 2 as '10' in decimal can be represented as '1010' in binary format. Similarly, we have countBits("12") = 2, countBits("7") = 3
What is an efficiently algorithm for this? One possible solution is to convert the decimal string to another string in the binary format, and count the '1's. Can we do better?
When converting from a decimal representation to and integer, the *n*th digit from the end of the string represents the number of 1010n ( one zero base ten to the power of n ) that is added to total the integer value. If you then want to represent that integer in binary, you have to raise 1010 which is 10102 to that power and multiply that value by the digit's value.
Because one of the factors of the base you are translating from, 5, is relatively prime compared to 2, the powers of 1010 have increasing long representations in base 2 - 12, 10102, 11001002, 11111010002.
Note that these powers have trailing zeros ( 1010 = 2 × 5 and 2 is not relatively prime with the base we are translating into ), so will only effect 1, 3, 5, and 7 bits of the answer instead of all 1, 4, 7, 10 bits. But the number of bits they effect will still vary with O(N) where N is the length of the input, so to calculate the effected bits will take O(N2) operations.
If the base you were translating from did not have factors where were relatively prime to the base you are translating to - say translating base 16 to base 2 or base 9 to base 3 and counting non-zero digits, then there would be a O(N) algorithm as the sum of non-zero digits in the target base would equal the sum for each digit in the input translated individually, but since that is not the case then you are stuck at an O(N2) algorithm where you translate the decimal representation into binary and then count the bits in the binary representation.
You convert it to binary and use Hamming weight algorithm.
How it works? Suppose you have the number 8, which is 00001000.
The algorithm takes chunks of 2 bits, so it'll have 00 00 10 00.
Now it'll sum each two bits (by having a mask 10101010, multiplying and shifting), which will result: 00 00 01 00.
Now it does the same for each 4 bits (by having a mask 00110011..), so it'll have 0000 1000. After adding each side, you'll have 0000 00001.
The last stage is adding the two numbers, 0 + 1, which is 1 and that's the final result.

Min number of operations

Given a positive integer N, we are allowed to apply any of the following operations as many times as we want in any order:
First Operation: Add 1 the Given positive integer N; If N is 7, after that operation N becomes 8. If N is 999, after that operation it becomes 1000.
Second operation: choose any occurrence of any digit and replace it by another digit. (475->479, 101 -> 111, 299 -> 199 and so on)
Third operation: add any non-zero digit to the left of the decimal representation of N: 47 -> 247, 9999 -> 49999, 2474 -> 72474 and so on).
Find the minimum number of operations that is needed for changing N to the lucky number.(Lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.)
EXAMPLES:
N=25, answer=2
N=46, answer=1
N=99, answer=2
I found this problem while I was trying various problems on LUCKY NUMBER..
I am stuck at this problem...
Please help..
The "add 1 to the number" and "add any non-zero leading digit to the number" are red herrings.
The minimum number of operations is one per digit in N which is non-lucky. You just change each of the non-4, non-7 digits to either 4 or 7.
Adding a leading digit will never help you because there's no need to make the number longer. Adding 1 seems like it could help, but it will only do two things: either it does not carry (when you add to a digit less than 9), in which case a straight replacement can do the same thing, or it carries (when you add to a 9), in which case it's just created one or more non-lucky zeros you're now going to have to "fix" with digit replacement.
Given the rules, apparently, the answer is the number of digits minus the number of 4 or 7 occurrences. So for example, N=25 you replace each digit with either 4 or 7 taking only one at a time. for 46, you take 6 and replace it with 4 or 7 thus the answer 1.
You can try continuous modulo 10 evaluation to check if the digits are 4 or 7
$x = the number
$y = 0; #number of non 4 or 7
while($x>0){
if($x % 10 != 4 && $x % 10 != 7){
$y++;
}
if($x % 10 == 0){
$y +=4;
}
$x = floor($x/10);
}
Apparently 0 is not replaceable doing some edits
only second case is important. just take a string and count how many digits are not equal to 4 and 7
Just consider the second operation.........and find the number of digits different from 4 and 7....and thats the answer.....isn't it....:)
You can try a greedy solution:
Check all digits in the number and count how many are not 4 or 7
Take the count from the above operation, and see if there's a small count when adding only 1 to the number will get you to Lucky one.
Take the min from both - that's the solution
What's the point in adding leading digits to N ? This will not get you an optimal solution.

Resources