What could we the complexity proof of it? - algorithm

In an online assessment I got one coding challenge and I wrote one recursive code for it.
The question was -
Given an integer n, return all the reversible numbers that are of length n.
A reversible number is a number that looks the same when rotated 180 degrees (looked at upside down).
Example:
Input: n = 2
Output: ["11","69","88","96"]
I wrote some kind of recursive approach and it passed.
vector<string> recursion(int n, int m) {
if (n == 0) {
vector<string> res = {""};
return res;
}
if (n == 1) {
vector<string> res = {"0", "1", "8"};
return res;
}
vector<string> ans = recursion(n - 2, m);
vector<string> res;
for (auto subAns: ans) {
// We can only append 0's if it is not first digit.
if (n != m) {
res.push_back('0' + subAns + '0');
}
res.push_back('1' + subAns + '1');
res.push_back('6' + subAns + '9');
res.push_back('8' + subAns + '8');
res.push_back('9' + subAns + '6');
}
return res;
}
vector<string> getAllNumbers(int n) {
return recursion(n, n);
}
I thought because we are calling 5 recursion it is something like 5^N but I want to do exact space and time complexity analysis for it.
Can anyone help me out what could be the exact solution, it is very tricky for me to figure out the exact space time complextiy for recursive approaches

Observe first that there are Θ(5n/2) valid numbers of length
n. Given the recurrence
C(−2) = 0
C(−1) = 0
C(0) = 1
C(1) = 3
∀n ≥ 2, C(n) = 5 N(n−2),
there are C(n) − C(n−2) numbers. If n = 2k where k is an integer, then
C(n) = 5k. If n = 2k + 1, then C(n) = 3 (5k).
The running time is Θ(5n/2 n). We can write a recurrence
T(0) = O(1)
T(1) = O(1)
∀n ≥ 2, T(n) = T(n−2) + Θ(5n/2 n),
where the latter term counts the cost of constructing Θ(5n/2)
numbers each of length n. This is not a terribly interesting recurrence;
we end up with a sum whose terms decrease faster than geometrically, so
it's Θ of its largest term.
Space usage will be asymptotically the same since space usage is bounded
above by time and below by the total size of the output, which are the
same Θ.

Related

What is the Time complexity of isPascal() method

static int isPascal(int n) {
int sum = 0;
int nthVal = 1;
while (sum < n) {
sum = sum + nthVal;
nthVal++;
}
return sum == n ? 1 : 0;
}
Here the function checks given number is pascal number or not. Pascal number is a number that is the sum of the integers from 1 to i for some i.
For example 6 is a Pascal number because 6 = 1 + 2 + 3
What will be the Time complexity of this function? Will it be O(logn) time? If so what will be base of log here?
If you consider calculating the square root a O(1) operation, you can do this check in O(1), with the help of the formula for the sum of the first i natural numbers
sum(i) = (i^2 + i)/2
Now in your case you don't know i but you know sum(i), because that's your n you want to check if it's a pascal number. So you have
n = (i^2 + i) /2
or
i^2 + i - 2n = 0
Solving this quadratic equation with the respecitive formula gives
i = -1/2 + sqrt(2*n + 1/4)
You can discard the second solution to this equation, because i must be > 0 to be a valid solution. If that resulting i is an integer, n is a Pascal number. Otherwise it isn't.
From that formula also follows, your iterative solution is in O(sqrt(n))

Computing expected time complexity of recursive program

I wish to determine the average processing time T(n) of the recursive algorithm:
int myTest( int n ) {
if ( n <= 0 ) {
return 0;
}
else {
int i = random( n - 1 );
return myTest( i ) + myTest( n - 1 - i );
}
}
provided that the algorithm random( int n ) spends one time unit to return
a random integer value uniformly distributed in the range [0, n] whereas
all other instructions spend a negligibly small time (e.g., T(0) = 0).
This is certainly not of the simpler form T(n) = a * T(n/b) + c so I am lost at how to write it. I can't figure out how to write it due to the fact that I am taking a random number each time from 0 to n-1 range and supplying it twice to the function and asking for the sum of those two calls.
The recurrence relations are:
T(0) = 0
T(n) = 1 + sum(T(i) + T(n-1-i) for i = 0..n-1) / n
The second can be simplified to:
T(n) = 1 + 2*sum(T(i) for i = 0..n-1) / n
Multiplying by n:
n T(n) = n + 2*sum(T(i) for i = 0..n-1)
Noting that (n-1) T(n-1) = n-1 + 2*sum(T(i) for i = 0..n-2), we get:
n T(n) = (n-1) T(n-1) + 1 + 2T(n-1)
= (n+1) T(n-1) + 1
Or:
T(n) = ((n+1)T(n-1) + 1) / n
This has the solution T(n) = n, which you can derive by telescoping the series, or by guessing the solution and then substituting it in to prove it works.

What is the complexity of these functions with explanation?

i would like to know how to find the complexity of these functions using T(n) .. and stuff like this .. Because i can only Guess.
First Function :
int f(int n)
{
if (n == 1)
return 1 ;
return 1 + f(f(n-1));
}
Time&Space complexity ??
Second Function :
time&space Complexity of Function f() ??? :
void f(int n)
{
int i ;
if(n < 2) return ;
for(i = 0 ; i < n/2 , i+= 5)
printf("*");
g(n/3);
g(n/3);
}
void g(int n)
{
int i ;
for(i = 0 ; i < n ; i++)
printf("?") ;
f(3*n/2);
}
Many Thanks :)
It may surprise you, but the second one is easier to start with. O_o ikr
Second function:
g(n) = n + f(3n/2), f(n) = n/10 + 2g(n/3). Therefore f(n) = 21n/10 + 2f(n/2).
Substitute n = 2^m, therefore f(2^m) = 21(2^m)/10 + 2f(2^(m-1)) = 2*21(2^m)/10 + 4f(2^(m-2)) etc...
The first term sums to m*21(2^m)/10, which may be obvious to you.
The second term (with the f()) grows geometrically; now f(1) = 1 (as there is only 1 operation), so if you expand to the last term you will find this term is 2^m * f(1) = 2^m. Therefore the total complexity of f is f(2^m) = m*21(2^m)/10 + 2^m, or f(n) = n(2.1*log(n) + 1), where log is base-2 logarithm.
Thus f(n) is O(n log(n)).
First function:
Ok I'll be honest I didn't know how to start, but I tested the code in C++ and the result is exactly f(n) = n.
Proof by induction:
Suppose f(n) = n, then f(n + 1) = 1 + f(f(n)) = n + 1. Thus if true for n then also true for n + 1
Now f(1) = 1 obviously. Therefore it's true for 2, and for 3, 4, 5 ... and so on.
Therefore by mathematical induction, f(n) = n.
Now for the time complexity bit. Since f(n) returns n, the outer call in the nested f(f(n-1)) will effectively be a second call, as the argument is the same: f(n-1); f(n-1);. Thus T(n) = 2*T(n-1) and therefore T(n) = 2^n. O(2^n).

Time complexity of an algorithm with two loops and two recursive calls

Here is an algorithm of sorting an array with two recursive calls.I have tried to calculate its time complexity roughly but not sure.I know that two for loops will take n+n times but what to do with recursive calls and how can i calculate them?
Any one can help in calculating in simple mathematical way.
MySort (a[1..n] , n) {
If (n <= 2) {
If (first element > second) && (n = 2) then do
{ Interchange a[1] & a[2]; }
End if
}
Else
{ Assign value to min and max by very first element of array.
for (i : = 1 to n) do
{ If (a[i] > max) then
max = a[i];
Else if (a[i] < min) then
min = a[i]; //Get max and min element from the array. }
End for
Calculate MID value of the MAXIMUM & MINIMUM element found.
For i : = 1 to n do
{
If(a[i] < mid) then { Increment Count1 by 1; and P[Count1]=a[i] }
Else if (a[i] > mid) then { Increment Count2 by 1; and Q[Count2]=a[i] }
//Divide the major array to sub-arrays;
//Count1 and Count2 are counters to make check on the size of sub-arrays generated.
}
End for
MySort (P, Count1);
MSort (Q, Count2); }
End if}
There are two loops followed by two recursive calls. Ideally, each call would halve the size of the input, resulting in n/2 values. This gives a recurrence relation:
T(n) = n + n + T(n/2) + T(n/2)
T(n) = 2n + 2T(n/2)
This matches the last row of the table given on the Master theorem page:
T(n) = O(nlogn)
If the input input is not evenly divided each call, then it instead takes n^2 time, as the size could possibly only be reduced by 1 each call:
T(n) = 2n + T(n-1) + T(1)
T(n) = nT(1) + 2n + 2(n-1) + 2(n-2) + ...
T(n) = O(n^2)

Complexity formula of this algorithm

I have this algorithm (golden ratio):
public static float golden(int n){
float res;
if(n == 0) {
res = 1;
} else {
res = (float) (1.0 + (1.0 / golden(n-1)));
}
return res;
}
I suppose The T(n) formula is T(n-1). I can get the complexity following this formula.
T(n) = aT(n - b) + c^n p(n)
and this one:
What's p(n) and c?
For each n > 0 the call to golden(n) results in a constant number of arithmetic operations and one recursive call, therefore you can write the recurrence for the time complexity function T(n) as
T(n) = T(n-1) + k, n>0
where k is the number of operations in each call. This is the recurrence relation for sequential search.
Now you can apply the formula that you mention in the question. Recall that d in the formula is the degree of the polynomial p(n). Here
a = 1, c = 1, b = 1, and p(n) = k,
therefore d = 0, now applying your formula you get a=c^b, and therefore the second case applies, so
T(n) = Theta(n^{d+1}c^n) = Theta(n).
This gives the final answer T(n) = Theta(n).

Resources