CodeForces: Creating the Contest I wrote a solution to this question. The task is to take an input of n increasing positive integers (1 ≤  n ≤ 2∙105), each entry as large as 109, and find the length of the longest subsequence where each element is no more than double the previous element.

Example input

10
1 2 5 6 7 10 21 23 24 49


Example output

4


… because the longest subsequence is [5, 6, 7, 10].



My solution works for small numbers, but exceeds the time limit for big inputs.

#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
using namespace std;
int main()
{    

 int n,a,c=1,max_count=1;
 cin&gt;&gt;n;
 vector&lt;int&gt;v;
 for(int i=0;i&lt;n;i++)
 {
     cin&gt;&gt;a;
    v.push_back(a);
}
for(int i=0;i&lt;n;i++)
{
    int l=i+1;
    while((l&lt;n)&amp;&amp;(v[l]&lt;=2*v[i]))
    {
        c++;
        l++;
        max_count=max(max_count,c);
    }

    c=1;
}
cout&lt;&lt;""&lt;&lt;max_count;
return 0;
}
There is no need to store the entries in a vector. There is also no need for a nested loop.  This solution will run in O(n) time and O(1) space.

Technically, int is only guaranteed to hold up to 16 bits. Use long to ensure that you can accommodate the limits.

Also, please avoid using namespace std;. You can drop the useless cout&lt;&lt;"".

#include &lt;algorithm&gt;
#include &lt;iostream&gt;

int main() {
    long n, a, seq_len = 0, longest = 0;
    std::cin &gt;&gt; n;
    for (long prev_a = 0; n--; prev_a = a) {
        std::cin &gt;&gt; a;
        if (a &lt;= 2 * prev_a) {
            seq_len++;
        } else {
            seq_len = 1;
        }
        longest = std::max(longest, seq_len);
    }
    std::cout &lt;&lt; longest &lt;&lt; '\n';
}