Autoscale complex function Context: We are working on a complex image drawing program to produce mugs, t-shirts, and so on. Here is an example of such function



However, we are facing the following problem: Random generated functions are mostly white and black (where black is close to zero and white close to infinity). The idea of this contest is to find a number to scale all numbers so that corresponding colors are in the visible spectrum.

Problem

The input is, formatted the way you want:


An array of double precision A[1..n] randomly sorted of positive numbers (>0).
An expected inclusive lower bound L > 0
An expected inclusive upper bound U > 0


The expected output is


A number f so if we multiply all elements of A by f, it maximizes the number of the new elements between L and U. If there are multiple solutions, f should be as close to 1 as possible.


Write a program computing f in a few operations as possible.

To check, post the result of your program on the following (sorted for convenience) input:

A = [0.01, 0.02, 0.5, 1 , 5, 9.5, 9.8, 15, 18, 20, 50, 100]
L = 1
U = 5


The result should be f = 0.25

Winning conditions: The winner will be the one with the best time complexity. If there are multiple algorithms of the same complexity, the fastest one will win. If your program is obfuscated or hard to read, please also attach an unobfuscated version with explanations.
C++
Comments indicate how code works.
#include&lt;iostream&gt;
#include&lt;vector&gt;
using namespace std;

int main(){
    // input stores input, sum_values stores the sum of values in the valid range after multiplying
    vector&lt;double&gt;input;
    vector&lt;double&gt;sum_values;

    // stores input, and ends of range
    double num, lower, higher;

    cout&lt;&lt;&quot;Enter the array of values, use 0 to exit: &quot;;

    // input
    while(1){
        cin&gt;&gt;num;
        if(num==0)break;
        input.push_back(num);
    }

    cout&lt;&lt;&quot;Enter the mimimum and maximum values: &quot;;

    cin&gt;&gt;lower&gt;&gt;higher;

    // if array is empty or lower is greater than higher, print 1 and exit
    if(lower&gt;higher||input.size()==0){cout&lt;&lt;&quot;1&quot;;return 0;}

    // iterate through all values between .0001 and .9999
    for(double d=.0001;d&lt;1;d+=.0001){
        
        // check each d for valid ranges
        for(int i=0,valid=0;i&lt;input.size();i++){
            
            // check if input * d is in valid range
            if(d*input[i]&gt;=lower&amp;&amp;d*input[i]&lt;=higher)valid++;

            // store final number
            if(i==input.size()-1)sum_values.push_back(valid);
        }
    }

    // iterate through compiled sum of ranges and find highest
    int position=0, biggest=-.1;
    for(int i=0;i&lt;sum_values.size();i++){

        // return value closest to 1 for equal values
        if(sum_values[i]&gt;=biggest){
            biggest=sum_values[i];
            position=i;
        }
    }

    // print value
    cout&lt;&lt;(double)position*.0001;

    return 0;
}