Alternative solutions for additional questions for an interview

Source: Internet
Author: User

I haven't written a blog for a while. During a stroll today, I saw a blog titled "A company held an annual" Developer "gathering in South China in April 7-recording a company's written test ". There are Interview Questions recalled by the author. One of the questions attracted the attention of the author. The questions are as follows:

Question: An array a [n] is known to construct an array B [N]. The construction rules are as follows: B [I] = A [0] * A [1] * A [2]... A [n]/A [I];
Requirements:
1. Division is not allowed;
2. the time complexity is O (n), and the space complexity is S (0 );
3. You cannot use other variables except for the variables used for traversal;

 

It seems simple, and it's a waste of time to think about it.

The first thought is the method that the original author thought of. The Code is as follows (vb2008 is used ):

Public shared function cacub1 (byval A () as double ()
Dim B (A. Length-1) as double
Dim I as integer
B (0) = 1
For I = 0 to A. Length-1
B (0) * = a (I)
Next
For I = A. Length-1 to 0 step-1
B (I) = B (0)/A (I)
Next
Return B
End Function

This method is concise with no additional code. The only thing that does not meet the requirement is that division is used.

The Code is as follows:

Public shared function cacub2 (byval A () as double ()
Dim B (A. Length-1) as double
Dim I as integer, J as integer
For I = 0 to A. Length-1
B (I) = 1
For J = 0 to A. Length-1
If I <> J then B (I) * = a (j)
Next
Next

Return B
End Function

Although the calculation amount is increased, division is not used. However, the time complexity of the algorithm is O (n * n), which does not meet the question requirements. This method is also rigid and is not recommended by the author.

 

After thinking for a long time, there is always an imbalance between division and time complexity.

Suddenly, a thought flashed through. Division? How about turning a bend? What is the conversion to subtraction?

Use formula S/a = 10lgs-lga

So this question becomes

S = a (0) * A (1) * A (2 )...... * A (n)

B (I) = 10lgs-lga (I)

The Code is as follows:

Public shared function cacub3 (byval A () as double ()
Dim B (A. Length-1) as double
Dim I as integer
B (0) = 1
For I = 0 to A. Length-1
B (0) * = a (I)
Next
For I = A. Length-1 to 0 step-1
B (I) = 10 ^ (math. log10 (B (0)-math. log10 (A (I )))
Next
Return B
End Function

 

Have you met the question requirements? Yes, no division is used, and the time complexity is O (n ). The efficiency is a little lower.

But is efficiency really low? Not necessarily. vs optimizes the log10 function. Although it is a little lower, it is negligible.

This problem is solved by the logarithm formula of the high school stage. If anyone has a better algorithm, I hope you will be enlightened. Everyone learns from each other and improves together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.