java - How to multiply two big big numbers -


You are given a list of n numbers l = & lt; A_1, a_2, ... a_n & gt; . Each of them either 0 or form +/- 2 k , 0 and lt; = K & lt; = 30. Describe and implement an algorithm that gives the largest product of continuing colonization P = a_i * a_i + 1 * ... * a_j, 1 & lt; = I & lt; = J & lt; = N .

For example, input & lt; 8 0 -4 -2 0 1 & gt; It should return 8 (either 8 or (-4) * (- 2)).

You can use any standard programming language and assume that the list is given in any standard data structure, eg int [] , vector & Lt; Int & gt; , list & lt; Integer & gt; , etc.

What is Computational Complexity Your Algorithm?

I want to combine the observation of Amnon, the sobilist.

The lists have been terminated hard by 0. We can break the problem of finding the largest product in each sub-list, and then the maximum number of (Other people have mentioned it).

This is my third revision of this writing. But the attraction of 3 ...

approach

Looking at a list of non-0 numbers, (this is what started thinking too much) 3 Sub-case:

  1. There are also a number of negative numbers (possibly 0) in the list. This is a minor matter, the optimal result is the product of all numbers, it is guaranteed to be positive.
  2. There is a strange number of negative numbers in the list, so the product of all numbers will be negative. To change the signal, it becomes necessary to sacrifice a negative number. Two sub-cases:

    a left of left left and negative on left side; Or

    B to the right from the right to the negative and the number of sacrifices.

    In any case, return the product of the remaining numbers, after sacrificing a negative number exactly, the result is sure to be positive (A) and choose the winner (B).

Implementation

The input needed to split later is delimited by 0. The list can be processed at that place, if a driver method is made for loop through it and non -0 sequences start and end.

By making math in the long run, making arithmetic large products easy by simply changing the possible category log 2. This prevents program failure in large numbers of large sequences. Alternatively, it would be possible to make all maths in Bignam, but it would probably perform poorly.

Finally, the end result, still need to change a log2 number, printable form. Bignam comes in handy here. New BigInteger ("2") .po (log); which will increase the power of logs .

Complexity

This algorithm works sequentially through sub-lists, only once each process performs each sub-list Within, log2 is an annoying task of converting the result to input, but efforts are linear in the size of the list. In the worst case, the sum of most lists is counted twice, but it is also a linear complexity.


Comments