This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 8 months ago.
int i = n;
while (i > 0){
cot += 1
i = i/2;
}
}
}
can someone please explain this with steps for each line? im having an extremely difficult time witht his. I know the answer is O(log n) but I dont know how to get there
The line cot += 1 is not used anywhere so I will ignore it.
In each loop you divide the initial n by 2 and do that until it reaches 0.
So for instance, let's say you have n=32. You divide it by 2 until it reaches 0. So you have the sequence i=32 -> 16 -> 8 -> 4 -> 2 -> 0.
So to figure out how many steps it will take you solve the equation N^k = n, where N is the number you're dividing with, k is your number of steps and n is the initial number. So for our example above where N=2, n=32 we solve for 2^k=32. The solution = log2(32) = 5.
The precise complexity would be log2(n), but you can rearrange this in a way that log(2) becomes a constant and you can ignore it (change of base formula).
Related
This question already has answers here:
What is time complexity and how to find it? [duplicate]
(2 answers)
What is complexity of this code? (Big O) Is that linear?
(1 answer)
Closed 2 years ago.
I am new to algorithm analysis, so I appreciate if anyone can help me. I have the following algorithm for sorting an array:
for(int i = 0 ; i < list ; i++){
if(list[i] > list[i+1]){
swap list[i] with list[i+1]
i = -1;
}
}
I claim that this algorithm is a linear algorithm (i.e, O(n)) but I did not know how to prove this.
I appreciate any help.
Thanks in advance.
This algorithm is actually cubic (O(n^3) where n = length of list) in the worst case scenario. Imagine the following input: list = [5,4,3,2,1].
First iteration: list[0] > list[1]. The swap is made such that list = [4,5,3,2,1], and i is reduced to -1, so the loop starts over.
Second iteration: list[0] < list[1].
Third iteration: list[1] > list[2]. The swap is made such that list = [4,3,5,2,1], and i is reduced to -1, so the loop starts over.
Fourth iteration: list[0] > list[1]. The swap is made such that list = [3,4,5,2,1], and i is reduced to -1, so the loop starts over.
The same pattern continues: we will need 6 more iterations to bring 2 to the start of the list, 10 iterations for 1, and 5 to skim over the list once it's all sorted. Overall 4+6+10+5=25 which is 5^2. So why n^3 and not n^2?
Intuition:
For a list that sorted in reverse like the one in the above example, we would need to bring each element to the head of the list from greatest to smallest. The j'th element in the initial list is the j'th greatest overall and will need (1+2+...+j)=O(j^2) steps to bring to the head of the list.
Therefore, in order to reverse the list of length n, we need approximately (1^2 + 2^2 + ... + n^2) steps. That is the sum of squares from 1 to n which is O(n^3) - if you don't know why, it's a very well-known formula in arithmetics: sum of squares.
Disclaimer: Of course, it wouldn't be exactly n^3 steps, but it will be approximately so (which is after all the definition of Big-O notation. It will be closer to n^3 the bigger n gets).
This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
How can I find the time complexity of an algorithm?
(10 answers)
Closed 4 years ago.
Hello Stack Users, I am having trouble finishing this growth according to size algorithm problem. I was able to figure out the first two of the problem which are not listed in the picture. That would be 1. O(1) and 3. O(N) I was able to place these into their correct slots. I still cannot figure out how to determine the growth rate for 2,4,5,6 into the slots provided. Any suggestions on how to determine this?
O(N)
The first for loop takes N and the second also takes N so
O(N) = N + N = 2N = N
O(N^2)
The first for loop takes N and the second also N, but in this case it is nested. inner loop takes N for every other loop of outer loop
O(N) = N * N = O(N^2)
O(N)
The first for loop it takes N and the second also 5, but it is nested so
O(N) = 5 * N = 5N = O(N)
O(log(N))
to divide a number N by 2 continuously until it reaches 1, it takes
log(N)
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Folks, hope you could point me, what I missing out. I am new to programming and now solving problems for Project Euler. One of the tasks is to calculate the sum of even numbers of Fibonacci sequence. I wrote code which passed 4 of 5 test. Here it is:
//N is input (N<=4*10^16), let's find n by Binet's formula
double a = log10(sqrt (5) * N);
double b = log10((1 + sqrt (5)) / 2);
int n = (int) (a / b);
//Using same formula, we find every Fibonacci number till n
for (int i = 1; i <= n; i++){
a = (1 + sqrt(5)) / 2;
long fiboNum = Math.round (Math.pow(a,i) / sqrt(5));
And here is my problem: when I run a test with big numbers, there is some rounding happening around F(71).
By my code, F(71) is 308 061 521 170 130, according to WolphramAlpha calculations 308 061 521 170 129.
Because of this, final sum is wrong.
Also, while testing I printed out all numbers and recognize, that even numbers repeat every 3. But even if I simplify it to the small loop of sum every third number, still problem exist.
If I do not use Math.round, I am getting the wrong sequence...
double has a limited precision. Try using BigDecimal which has precision only limited by the size of available memory.
Here is how you compute square root with BigDecimals: Square root of BigDecimal in Java
pow(): Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?
Rounding: Java BigDecimal: Round to the nearest whole value
Other operations like addition and division are built-in into BigDecimal as methods.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
f(n) = f(n-1) + f(n-2) + f(n-1)*f(n-2);
1 <= n <= 10^9. Initial terms --> 1 <= f(0), f(1) <= 10^9.
Output the answer modulo 10^9 + 7.
The general term that I have found is,
f(n) = (((1 + f(1))^T(n)) * ((1 + f(0))^T(n-1))) - 1;
Here T(n) is the nth Fibonacci number.
I use matrix exponentiation and modular power to solve the problem, but it gives TLE.
As long as you haven't provided a sample of your code, I will give you a general idea.
Personally I wouldn't use recursion because n can be large thus leading to inefficient and long calculations filling stack and cause an overflow. So better inplement a cycle using array.
Next tip is to keep numbers relatively small, computing modulo on each step. (a+b+ab)%m is equal to (a%m + b%m + (a%m)*(b%m))%m (see wiki) so that we keep our numbers in range 0..10^9
Here is an example of solution:
const unsigned int mod = 1e9+7;
std::vector<unsigned int> vec(n+1); // prepare vector of needed size
// size is n+1 because we start from 0 and you need vec[n]
vec[0] = 1;
vec[1] = 1e9;
unsigned long long int r1, r2; // long long will prevent r1*r2 from overflowing
for (unsigned int i = 2; i <= n; ++i)
{
r1 = vec[i-1] % mod;
r2 = vec[i-2] % mod;
vec[i] = (r1 + r2 + r1*r2) % mod;
}
std::cout << vec[n];
You tagged your question with [c++] and [java]. Above code is in [c++] and I hope you would be able to port it if you need
k=1000000007 is prime. So a^b mod k is equivalent to a^(b mod (k-1)).
mod k, f(0)+1 is 2 and f(1)+1 is -6.
Raising things to powers can be efficiently done by repeated squaring and selecting squares to multiply together.
Calculating the nth Fibbonacci mod 1000000006 is tricky (for large n). I'm unaware of a simple closed form.
Aha! The Pisano period is the period over which the Fibbonnacci numbers mod some value repeat. k is decomposed into primes 2 * 500,000,003
Simply calculate the Pisano period of 2 and 500000003. Take the LCM of the pisano period of those two primes to get the pisano period of their product. We can use this to reduce the value n modulo this number before feeding it to Fibbonnacci.
This gives us a bounded Fibbonnacci calculation (hopefully tractible), to get us the Fibbonnacci result mod 1000000006, which we can then use the square technique (mod 1000000007) to raise 2 and -6 to that power, add them together and subtract 1.
Good luck calculating the Pisano period. If it is reasonable (and most look to be), you should be able to take a billion-digit n and solve in a fraction of a second.
Now, the Pisano period and closed forms of Fibbonnacci is based off some number theory and math. So instead we could try to directly generate a similar number theory/etc result for this recurrance relation rather than going through the identity you found. Arguably that wouod be harder, but might result in a cleaner answer.
This question already has answers here:
Longest positive subarray
(2 answers)
Closed 6 years ago.
Given an array of positive integers a, and an integer k, I'm trying to figure out an algorithm which will give me the length of the longest subarray, the sum of which is less than or equal to k. I have figured out how to solve it in O(n^2) time, but am trying to solve in in as close to O(n) as I can.
For a O(n) solution, I'm trying to create a start index and an end index, which will give me a window. I want to check if the sum within this window is <= k AND if the length of this window is greater than the last recorded length. However, when typing it out, my logic breaks down.
I think you mean
maxLen = currLen;
I don't think its part of your problem but you don't use end and I don't think it's updated correctly. Just remove it.