So I have an two arrayLists that represent two numbers. This is so I don't have to use BigInt. so for example
ArrayList<Integer> LargeInt = [2,3,6,4] would really equal 2,364
ArrayList<Integer> LargeInt2 = [8,7,9,4,6] would be 87,946
my goal is to figure out a way to multiply the two numbers and make the answer to a string. I know that multiplying two numbers would need to be put into another array before put into a string so it won't crash with larger numbers. I also know it will be one for loop put into another. But i am finding it difficult to make a code that multiplies the two numbers. the two arrays being multiplied can be any number.
Assuming that this is homework, here is a no-code explanation of what you need to do:
Define a class that wraps ArrayList<Integer>; let's say you call it ArrayInt
Define an operation that adds two ArrayInts together, and returns a third ArrayInt that equals their sum. You can do it digit by digit, taking care of a possible carry into an extra digit, so you need to size your result accordingly.
Define an operation that multiplies your number by a power of ten by adding zeros to the array list. Again, the operation should return a new ArrayInt, rather than modifying the current one
Define an operation that multiplies a number by a single digit. You can use multiplication, or a simple loop that uses addition. The loop would not run more than nine times, so it shouldn't be too bad.
Combine the three operations that you have (addition, multiplication by a digit, and multiplication by a power of ten) into a simple multiplication algorithm that you learned in the elementary school.
Related
Today, I have a question about multiplying, dividing numbers with a specific format. The format is like this: I will make the number into a linked list, and do the operation with this linked list. For example, if I want to multiply 1234567 and 7890123, I will make these two numbers into linked list by cutting the numbers into 3 digits at one time, like 1,234,567 and 7,890,123. Then, I will put one chunk at one node(1, 234, 567 are three chunks, and they all go into different nodes) for every numbers. Now, the problem is, I have to multiply, divide, and get the quotient and remnant of the operation between the two numbers without changing them back to the real 'numbers'. The operation must be done with the form of linked list. So, for the multiplying, I've thought 'let's multiply the numbers in each node first, and put the zeros in the back to adjust the cipher, then add them all to finish the operation. for example, if I want to multiply 1,234 and 5,678, I will do (234*678 + (1*678)000 + (5*234)000 + (1*5)000,000). But, I can't be certain this method is right, And I can't imagine about the dividing and getting the quotient and remnant part. So, please help..!
My teacher and I were discussing whether or not a recursive permutation function could be written without the use of substrings and/or arrays in Java.
Is there a way to do this?
The answer is yes, this can be done. I'm assuming that "without the use of substrings and/or arrays" refers to the info being passed to the recursion. You have to have some sort of container for the elements that are to be permuted.
In that case it can be done by pulling some hideous tricks with numerically encoding the indices of the elements as digits of a numeric argument. For instance, if there are 3 elements and I use 1 as a sentinel value in the left-most digit (so you can have 0 as the leading index sometimes), 1 means I haven't started, 10 means the first element has been selected, 102 means the first and third, and 1021 means I'm ready to print the permutation since I now have a 4 digit argument and there are 3 elements in the set. I can then deconstruct which elements to print using % 10 and / 10 arithmetic to pick them off.
I implemented this in Ruby rather than Java, and I'm not going to share the actual code because it's too horrible to contemplate. However, it works recursively with only the input array of elements and an integer as arguments, no partial solution substrings or arrays.
I came across this question in a recent interview.
Given an array find the maximum product that can be obtained multiplying any 3 numbers in the array.
The solution I came up with is,
Sort the array
Multiply the 3 largest number.
This is O(nlogn)
Is there a O(n) solution to the problem?
Instead of sorting all the values, you can retain the top three, which is O(3n) which is O(n)
Doing this in a single pass is likely to be more efficient than three passes.
One way of doing this is to do an insertion sort into an array of 3, discarding the lowest value each time. (you can start at the lowest value in the array)
You can also implement this using a series of if/else comparison to update 3 variables.
How about negatives?
The only complication is if you can have negative values e.g. 5 * -4 * -4 > 5 * 5 * 3
For this reason it would makes sense to search for the three largest and the two most negative. You can check whether the largest * the next two largest or largest * the two most negative is bigger.
what if they are all negative?
In this case, you also need the three smallest negative values as well to get the product closest to positive infinity.
You can do it in O(n) as follows:
Find the largest number in O(n); remove the number from the array (make it zero)
Find the largest number among the remaining numbers in O(n); make it zero as well
Find the largest number among the remaining numbers, again in O(n)
You repeat the same O(n) loop three times, so it's O(3*n), which is the same as O(n) because constants are ignored.
This won't cater to negatives
It is relatively easy to modify this algorithm to work with negative numbers. Each pass through the array needs to find the largest and the smallest negative value, so at the end you would have three large positive numbers P0, P1, P2 and thee large negative numbers N0, N1, N2, in ascending order by magnitude. Now you need to compare P0*P1*P2 vs. N1*N2*P2, and pick the larger one.
There are O(n)-time complexity solutions to this problem, but these will only occur depending on the input of your array, as you can read in this article
You can perform this in 1 pass by storing largest elements from array A to to array of 3 say B.
For each element n in array A if n is greater than any element in array B then replace array B element with array A element.
Finally multiply the 3 elements in resulting array. This is constant time. This may still be 3n since you are checking against array of 3 as well.
For negative it would be additional 2 elements which contain the smallest. So it may be 5n but still constant time
I need to store two extremely large numbers (Strings, since they won't fit in int) in two linked lists, add them and then display the result (again, a String).
I can store the numbers directly into the list.
312312 can be stored as 2->1->3->2->1->3 (actual number will be extremely long)
111119 can be stored as 9->1->1->1->1->1
Then I can add them
11->2->4->3->2->4
Normally I could do 11*10^0 + 2*10^1 +...+ 4*10^5 and get 423431 but all those operations (multiplication, addition and exponentiation) would again be integer operations and since the actual numbers are going to be extremely big, int or long won't support the operations. The final result has to be a string.
So I need a way to convert 11->2->4->3->2->4 into 423431 without using int. Also, I cannot use BigInteger. Can anyone help me?
Well, first thing you need to do is implement carry.
For each digit (that is >= 10), you need to increase the next digit by that digit /10 and set that digit to that digit %10.
So 11->2->... becomes 1->3->....
Then to actually produce the string.
For the most performant option, I suggest StringBuilder.
Just append each digit in the linked-list, then just reverse().toString() (since you started with the smallest number).
Think about how you would do it by hand on paper. If the sum of a pair digits is greater than 9 you write down a carry digit of 1, which you add into the sum of the next pair of digits.
In a computer program you can use a local variable for that: add digits from first and last numbers and the carry from earlier, if sum is greater than.. set carry to 1, else set carry to 0, move on to the next pair...
I have a n-digit number composed of 1's only. I want to replace 1's with 0's in all possible combinations and store the combinations in an array.
How do I find all combinations?
I was thinking of starting with one zero and then increasing the number if zeroes that will replace 1's.
If there are 2 zeroes, for example, then i will keep the position of one zero fixed and move the other, till it reaches the end then i'd do the same for the other zero. But then i'll have to root out the repeated combinations.
Basically, this is getting complicated. I want to know a better way to find combinations!
You are simply trying to generate n-digit binary numbers. It means that you can generate 2^n different numbers. So here you go:
Firstly, calculate 2^n.
Implement a loop, starting from 0 and ending at 2^n.
For every loop variable, take the variable as a decimal and convert
it to binary and append some leading zeros (if necessary) to make it
length of n.
Add the binary to your permutations array.