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.
Related
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).
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:
Random shuffling of an array
(31 answers)
Closed 7 years ago.
Intro: This is an interview question I had which I couldn't solve. A code solution will be good (in any language), but an algorithm/pseudocode is also great.
The problem: This problem is about designing an algorithm that solves the following problem:
You are given a function int getRand(int x) that gets an int x and returns an int in the range from 0 to x randomly (exclusively -> [0, k) ). Each call to getRand() is performed in O(1) time.
You are also given an array int[] arr of size k containing integers.
Write a function getRandUnique() that when called will return a random member from arr such that after k requests exactly you will have a full permutation of the members of arr (this actually means that getRandUnique() will return a different member of arr for each call).
Each call to getRandUnique() has to be performed in O(1) time.
You are allowed to use/store global variables etc...
E.g.: Assume arr = [2, 3, 5 , 1]. A call to getRandUnique() will return either 2, 3, 5, 1 in 1/4 probability. A consequent call to getRandUnique() will return on of the remaining 3 members in 1/3 probability and so on...
Attempt: I actually solved this myself (after much trial and error) and posted the solution "Q&A Style". I would love to get some other possible ideas/solutions. I will accept any solution that works as the correct answer (I don't want to accept my own)!
Question: How can this be achieved with all the above restrictions?
Edit: Now I am aware that this problem corresponds to the Fisher–Yates shuffle, even though the specifications are a little different/more strict here.
My solution is as follows:
Define index = 0.
Call and assign index = getRand(k) (remember that k is the size of arr).
Swap arr[index] with arr[k].
Now call and assign index = getRand(k-1). Now you can be certain that you won't get the index k again so you won't have to remove it.
Swap arr[index] with arr[k-1]
Continue doing this until you call the last getRand(1).
Now you have an array arr that is a random permutation of itself as requested (don't even need an additional array).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
nth fibonacci number in sublinear time
I was creating a program which is related to the stair problem, i.e you have n stairs and the player can climb on the stairs using them one by one or skipping one ...
Now to solve this problem I needed nth (n+1)th term for the Fibonacci for n number of stairs, but the problem is my input range is 1 ≤ n ≤ 1000000.
And for that much greater value of n if I use the loop based method or recursion to calculate the Fibonacci the method takes very much time and space. That I do not have.
So please can you tell me some method in the Java or C to handle Fibonacci series up to that range with correct output?
Note: Please I do not need any solution which has recursion or loop.
Look at the following page, maybe it will help: https://www.nayuki.io/page/fast-fibonacci-algorithms
For me their Java example managed to calculate 1000000th Fibonacci number. It is 208988 digits long.