How would I go about calculating the runtime of this algorithm, so I can solve similar questions in the future?
For input size n satisfies the recurrence relation (for n>= 1)
T(n) = (2/n) * (T(0) + T(1) + ... + T(n-1)) + 5n
A way to get rid of sums is to compute differences, after multiplying by $n$ (allow me to write LaTeX, even if this site doesn't use it; I hope the formulas are understandable):
$$
(n + 1) a_{n + 1} - n a_n
= 2 a_n + 5
$$
$$
(n + 1) a_{n + 1} - (n + 2) a_n = 5
$$
This is a linear recurrence of the first order. A recurrence of the form:
$$
x_n - u_{n - 1} x_{n - 1} = f_n
$$
can be reduced to a sum by dividing by the summing factor $S_n = \prod_{0 \le k \le n} u_n$ to get:
$$
\frac{x_n}{S_n} - \frac{x_{n - 1}}{S_{n - 1}} = \frac{f_n}{S_n}
$$
Summing over $1 \le n \le N$ gives a solution to the equation.
In your particular case $S_n = \prod_{0 \le k \le n} \frac{n + 2}{n + 1} = n + 1$, so that:
$$
\frac{a_{k + 1}}{k + 2} - \frac{a_k}{k + 1} = \frac{5}{(k + 1) (k + 2)}
$$
\begin{align}
\frac{a_n}{n + 1} - \frac{a_0}{1}
&= 5 \sum_{0 \le k < n} \frac{1}{(k + 1) (k + 2} \
&= - 5 \sum_{0 \le k < n} \left( \frac{1}{k + 2} - \frac{1}{k + 1} \right) \
&= - 5 \left( \frac{1}{n + 1} - 1 \right) \
&= 5 \frac{n}{n + 1}
\end{align}
Finally:
$$
a_n = a_0 (n + 1) + 5 n
$$
Related
Question:
In less O(n) find a number K in sequence 1,2,3...N such that sum of 1,2,3...K is exactly half of sum of 1,2,3..N
Maths:
I know that the sum of the sequence 1,2,3....N is N(N+1)/2.
Therefore our task is to find K such that:
K(K+1) = 1/2 * (N)(N+1)/2 if such a K exists.
Pseudo-Code:
sum1 = n(n+1)/2
sum2 = 0
for(i=1;i<n;i++)
{
sum2 += i;
if(sum2 == sum1)
{
index = i
break;
}
}
Problem: The solution is O(n) but I need better such as O(n), O(log(n))...
You're close with your equation, but you dropped the divide by 2 from the K side. You actually want
K * (K + 1) / 2 = N * (N + 1) / (2 * 2)
Or
2 * K * (K + 1) = N * (N + 1)
Plugging that into wolfram alpha gives the real solutions:
K = 1/2 * (-sqrt(2N^2 + 2N + 1) - 1)
K = 1/2 * (sqrt(2N^2 + 2N + 1) - 1)
Since you probably don't want the negative value, the second equation is what you're looking for. That should be an O(1) solution.
The other answers show the analytical solutions of the equation
k * (k + 1) = n * (n + 1) / 2 Where n is given
The OP needs k to be a whole number, though, and such value may not exist for every chosen n.
We can adapt the Newton's method to solve this equation using only integer arithmetics.
sum_n = n * (n + 1) / 2
k = n
repeat indefinitely // It usually needs only a few iterations, it's O(log(n))
f_k = k * (k + 1)
if f_k == sum_n
k is the solution, exit
if f_k < sum_n
there's no k, exit
k_n = (f_k - sum_n) / (2 * k + 1) // Newton step: f(k)/f'(k)
if k_n == 0
k_n = 1 // Avoid inifinite loop
k = k - k_n;
Here there is a C++ implementation.
We can find all the pairs (n, k) that satisfy the equation for 0 < k < n ≤ N adapting the algorithm posted in the question.
n = 1 // This algorithm compares 2 * k * (k + 1) and n * (n + 1)
sum_n = 1 // It finds all the pairs (n, k) where 0 < n ≤ N in O(N)
sum_2k = 1
for every n <= N // Note that n / k → sqrt(2) when n → ∞
while sum_n < sum_2k
n = n + 1 // This inner loop requires a couple of iterations,
sum_n = sum_n + n // at most.
if ( sum_n == sum_2k )
print n and k
k = k + 1
sum_2k = sum_2k + 2 * k
Here there is an implementation in C++ that can find the first pairs where N < 200,000,000:
N K K * (K + 1)
----------------------------------------------
3 2 6
20 14 210
119 84 7140
696 492 242556
4059 2870 8239770
23660 16730 279909630
137903 97512 9508687656
803760 568344 323015470680
4684659 3312554 10973017315470
27304196 19306982 372759573255306
159140519 112529340 12662852473364940
Of course it becomes impractical for too large values and eventually overflows.
Besides, there's a far better way to find all those pairs (have you noticed the patterns in the sequences of the last digits?).
We can start by manipulating this Diophantine equation:
2k(k + 1) = n(n + 1)
introducing u = n + 1 → n = u - 1
v = k + 1 k = v - 1
2(v - 1)v = (u - 1)u
2(v2 - v) = u2 + u
2(4v2 - 4v) = 4u2 + 4u
2(4v2 - 4v) + 2 = 4u2 - 4u + 2
2(4v2 - 4v + 1) = (4u2 - 4u + 1) + 1
2(2v - 1)2 = (2u - 1)2 + 1
substituting x = 2u - 1 → u = (x + 1)/2
y = 2v - 1 v = (y + 1)/2
2y2 = x2 + 1
x2 - 2y2 = -1
Which is the negative Pell's equation for 2.
It's easy to find its fundamental solutions by inspection, x1 = 1 and y1 = 1. Those would correspond to n = k = 0, a solution of the original Diophantine equation, but not of the original problem (I'm ignoring the sums of 0 terms).
Once those are known, we can calculate all the other ones with two simple recurrence relations
xi+1 = xi + 2yi
yi+1 = yi + xi
Note that we need to "skip" the even ys as they would lead to non integer solutions. So we can directly use theese
xi+2 = 3xi + 4yi → ui+1 = 3ui + 4vi - 3 → ni+1 = 3ni + 4ki + 3
yi+2 = 2xi + 3yi vi+1 = 2ui + 3vi - 2 ki+1 = 2ni + 3ki + 2
Summing up:
n k
-----------------------------------------------
3* 0 + 4* 0 + 3 = 3 2* 0 + 3* 0 + 2 = 2
3* 3 + 4* 2 + 3 = 20 2* 3 + 3* 2 + 2 = 14
3*20 + 4*14 + 3 = 119 2*20 + 3*14 + 2 = 84
...
It seems that the problem is asking to solve the diophantine equation
2K(K+1) = N(N+1).
By inspection, K=2, N=3 is a solution !
Note that technically this is an O(1) problem, because N has a finite value and does not vary (and if no solution exists, the dependency on N is even meanignless).
The condition you have is that the sum of 1..N is twice the sum of 1..K
So you have N(N+1) = 2K(K+1) or K^2 + K - (N^2 + N) / 2 = 0
Which means K = (-1 +/- sqrt(1 + 2(N^2 + N)))/2
Which is O(1)
I understand that why bubble sort is O(n^2).
However in many explanations I see something like this:
(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Sum = n(n-1)/2
How do you calcuate Sum from this part:
(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Can anyone help?
Here's the trick:
If n is even:
n + (n-1) + (n-2) + … + 3 + 2 + 1
= [n + 1] + [(n-1) + 2] + [(n-2) + 3] + … + [(n - (n/2 - 1)) + n/2]
= (n + 1) + (n + 1) + (n + 1) + … + (n + 1)
= n(n+1)/2
If n is odd:
n + (n-1) + (n-2) + … + 3 + 2 + 1
= [n + 1] + [(n-1) + 2] + [(n-2) + 3] + … + [(n - (n-1)/2 + 1) + (n-1)/2] + (n-1)/2 + 1
= (n+1) + (n+1) + (n+1) + … + (n+1) + (n-1)/2 + 1
= (n+1)(n-1)/2 + (n-1)/2 + 1
= (n^2 - 1 + n - 1 + 2)/2
= (n^2 + n)/2
= n(n+1)/2
For your case, since you're counting up to n-1 rather than n, replace n with (n-1) in this formula, and simplify:
x(x+1)/2, x = (n-1)
=> (n-1)((n-1)+1)/2
= (n-1)(n)/2
= n(n-1)/2
The simplest "proof" to understand without deriving the equation is to imagine the complexity as area:
so if we have sequence:
n+(n-1)+(n-2)...
we can create a shape from it... let consider n=5:
n 5 *****
n-1 4 ****
n-2 3 ***
n-3 2 **
n-4 1 *
Now when you look at the starts they form a right angle triangle with 2 equal length sides ... that is half of n x n square so the area is:
area = ~ n.n / 2 = (n^2)/2
In complexities the constants are meaningless so the complexity would be:
O(n^2)
I need to partition n elements into k groups and the sum of these groups are all the same.
For instance:
I have a list of numbers going from 1 to 99 [1, 2, 3, 4, 5...] and I need to partition the list into 3 groups. The sum of all elements of each groups must be equal. In this example, n=99 and k=3.
What is an efficient and elegant algorithm to achieve this?
I'm just asking for algorithm suggestions to use; I don't want a solution.
Let me start by clarifying something: when you say you want an algorithm, but no solution, I have to assume that what you mean is that you want an algorithm that'll give you a solution for k=3 and n any positive integer.
I believe the problem has a solution for any n >= 5, provided either n or n+1 is divisible by 3.
This is because the sum 1 + ... + n equals n*(n+1)/2, which is divisible by 3 if and only if either n or n+1 is divisible by 3.
Assuming n satisfies these conditions, the algorithm goes like this.
Let s = [n*(n+1)/2]/3.
Find the number m in the decreasing sequence n, n-1, n-2, ..., such that
n + (n - 1) + (n - 2) + ... + m <= s < n + (n - 1) + (n - 2 ) + ... + m + (m - 1).
Let h = s - [n + (n - 1) + (n - 2) + ... + m].
Then h + [n + (n - 1) + (n - 2) + ... + m] = s, and we have our first sum.
Note that, given how m was obtained, h is guaranteed to be in the decreasing sequence m-2, m-3, ..., 1, hence is available for our first sum.
We now find the number q in the decreasing sequence m-1, m-2, ..., h+1, h, h-1, ..., 1, such that
m + (m - 1) + (m - 2) + ... + q <= s < m + (m - 1) + (m - 2) + ... + q + (q - 1),
keeping in mind that these sums may include h+1 or h-1, but not h. I am abusing the notational conventions for sums here.
This is where things are getting a bit hairy.
I conjecture that the number p = s - [m + (m - 1) + (m - 2) + ... + q] is among the numbers left over (unused) from the original sequence, but I won't prove it. (Exercise for the reader, as they say.)
Then p + [m + (m - 1) + (m - 2) + ... + q] = s, and we have have our second sum.
(Again: the number h may not be in the sum [m + (m - 1) + (m - 2) + ... + q].)
The last sum is that of all the remaining numbers, left over from the original sequence.
For example (well, yes, that is a solution --- but for illustrative purposes only...):
1 + ... + 99 = 99*100/2 = 3*550
= (99 + 98 + 97 + 96 + 95 + 65)
+ (94 + 93 + 92 + 91 + 90 + 89 + 1)
+ (88 + ... + 66 + 64 + ... + 2).
Here n = 99, m = 95, h = 65, q = 89, and p = 1.
Please can anyone help me with this:
Solve using iteration Method T (n) = T (n - 1) + (n - 1)
And prove that T (n) ∈Θ (n²)
Please, if you can explain step by step I would be grateful.
I solved an easy way :
T (n) = T (n - 1) + (n - 1)-----------(1)
//now submit T(n-1)=t(n)
T(n-1)=T((n-1)-1)+((n-1)-1)
T(n-1)=T(n-2)+n-2---------------(2)
now submit (2) in (1) you will get
i.e T(n)=[T(n-2)+n-2]+(n-1)
T(n)=T(n-2)+2n-3 //simplified--------------(3)
now, T(n-2)=t(n)
T(n-2)=T((n-2)-2)+[2(n-2)-3]
T(n-2)=T(n-4)+2n-7---------------(4)
now submit (4) in (2) you will get
i.e T(n)=[T(n-4)+2n-7]+(2n-3)
T(n)=T(n-4)+4n-10 //simplified
............
T(n)=T(n-k)+kn-10
now, assume k=n-1
T(n)=T(n-(n-1))+(n-1)n-10
T(n)=T(1)+n^2-n-10
According to the complexity 10 is constant
So , Finally O(n^2)
T(n) = T(n - 1) + (n - 1)
= (T(n - 2) + (n - 2)) + (n - 1)
= (T(n - 3) + (n - 3)) + (n - 2) + (n - 1)
= ...
= T(0) + 1 + 2 + ... + (n - 3) + (n - 2) + (n - 1)
= C + n * (n - 1) / 2
= O(n2)
Hence for sufficient large n, we have:
n * (n - 1) / 3 ≤ T(n) ≤ n2
Therefore we have T(n) = Ω(n²) and T(n) = O(n²), thus T(n) = Θ (n²).
T(n)-T(n-1) = n-1
T(n-1)-T(n-2) = n-2
By substraction
T(n)-2T(n-1)+T(n-2) = 1
T(n-1)-2T(n-2)+T(n-3) = 1
Again, by substitution
T(n)-3T(n-1)+3T(n-2)-T(n-3) = 0
Characteristic equation of the recursion is
x^3-3x^2+3x-1 = 0
or
(x-1)^3 = 0.
It has roots x_1,2,3 = 1,
so general solution of the recursion is
T(n) = C_1 1^n + C_2 n 1^n + C_3 n^2 1^n
or
T(n) = C_1 + C_2 n + C_3 n^2.
So,
T(n) = Θ(n^2).
int i=1,s=1;
while(s<=n)
{
i++;
s=s+i;
}
time complexity for this is O(root(n)).
I do not understood it how.
since the series is going like 1+2+...+k .
please help.
s(k) = 1 + 2 + 3 + ... k = (k + 1) * k / 2
for s(k) >= n you need at least k steps. n = (k + 1) * k / 2, thus k = -1/2 +- sqrt(1 + 4 * n)/2;
you ignore constants and coeficients and O(-1/2 + sqrt(1+4n)/2) = O(sqrt(n))
Let the loop execute x times. Now, the loop will execute as long as s is less than n.
We have :
After 1st iteration :
s = s + 1
After 2nd iteration :
s = s + 1 + 2
As it goes on for x iterations, Finally we will have
1 + 2 ... + x <= n
=> (x * (x + 1)) / 2 <= n
=> O(x^2) <= n
=> x= O (root(n))
This computes the sum s(k)=1+2+...+k and stops when s(k) > n.
Since s(k)=k*(k+1)/2, the number of iterations required for s(k) to exceed n is O(sqrt(n)).