Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Simple linear search to find max min algo
maxmin(a,n,max,min){
max=min=a[1];
for i=2 to n do{
if a[i]>max then max:=a[i];
else if a[i]<min then min:=a[i];
}
}
1.Average case complexity of the above algo given that the first if conditions fails for n/2 elments
2.Average case complexity of the above algo if the first ccondition fails 1/2 times plz xplain
The average case complexity for both cases is O(n). if k is the number of times the first if fails, then the number of comparisons is 2*n - 2 - k.
maxmin(a,n,max,min){
max=min=a[1];
for i=2 to n do{ // goes through the loop n-1 times
if a[i]>max then max:=a[i]; // out of n-1 times succeeds k times and fails n-1-k times
else if a[i]<min then min:=a[i]; // runs this n-1-k times
}
}
n-1 + n-1-k -> 2*n - 2 - k
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Suppose I want to find partition number of n, aka p(n). Here Euler's Pentagonal number theorem, a dynamic programming based solution is present, which has time and complexities O(n^2), O(n^2\log(n)) respectively.
Is there any improvement over this algorithm to reduce complexity or is there any proof showing that this algorithm is the best possible for this problem/ reducing complexity bellow this is NP-hard. Also what about the space-time trade off. Can we reduce time/space complexity by increasing space/time complexity respectively (keeping in mind that each complexity should not be more that O(n^3).
The following recurrence can be directly translated to code:
where
,
import numpy as np
def num_partitions(n):
# recursive function with an auxiliary cache to avoid recomputing
# the same value more than once
def get(n, k, aux):
# terminate the recursion
if n < k:
return 0
if k == 1 or k == n:
return 1
# check if the value is already in the cache - if not, compute
# it recursively
if aux[n][k] == -1:
aux[n][k] = get(n-k, k, aux) + get(n-1, k-1, aux)
return aux[n][k]
return np.sum([get(n, k, np.ones((n+1,n+1)) * -1) for k in range(1, n+1)], dtype=np.int)
import sys
print(num_partitions(int(sys.argv[1])))
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a question about the time complexity of the solution for the "two number sum" problem.
You get an array of integer numbers and should return true if any two distinct numbers in the array sum up to a defined target sum or not.
The problem can be found here: https://leetcode.com/problems/two-sum/
Here is one possible solution:
vector<int> twoSum(vector<int> &numbers, int target)
{
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < numbers.size(); i++) {
int numberToFind = target - numbers[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind] + 1);
result.push_back(i + 1);
return result;
}
//number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}
The "for loop" time complexity is O(n) because it goes through n array elements.
The find() function's average time complexity is O(1). You look into that here: http://www.cplusplus.com/reference/unordered_map/unordered_map/find/
Since 1 * n is still n, the average case time complexity is still just O(n).
I thought when we talk about time complexity we always mean the worst-case time complexity.
Operations on a hash map are then O(n) or at least O(log n).
In a coding interview would you say the algorithm above runs in O(n^2) or O(n)?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
suppose that we have a m*n matrix that each rows are in order. so, i only know that order of best algorithm for this problem is O(m(log m + log n)).
(It was a test question and result is this order)
but i don't know how this algorithm works
One idea can be like this.
If I ask you what is the rank of a given number x in the original matrix? How do you answer this question?
One answer can be:
Just binary search the first occurrence of x or greater element on each row. and then add the individual ranks.
int rank = 1;
for (int i = 0; i < m; ++i) {
rank += std::lower_bound(matrix[i].begin(), matrix[i].end(), x);
}
This can be done in O(m * log n) time(m binary searches on n sized arrays).
Now we just need to do a binary search on x(between 0 and INT_MAX or matrix[0][k]) to find the kth rank. Since INT_MAX is const, that will make the overall time complexity O(m * log n) theoretically. One optimization, which can be done use intelligent ranges in place of matrix[i].begin(), matrix[i].end().
PS: Still wondering the O(m*(log m + log n)) or O( m * (log mn)) solution.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am currently working on program and want to convert ArrayList to an array but in less than O(n) time.
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != null) {
arr[i] = list.get(i);
}
}
If n is the length of the list then O(n) means in this case that you look at each element in the list and copy it.
Now you say you want to convert it in less than O(n). This means you have to ignore some elements in the list. if not it would be O(n) again. But which do you ignore? Remember you are not allowed to look at all elements else it would be in O(n) again.
Let's say you know that the list contains booleans where n/2 are true and the others are false. In the best-case scenario all true values would be in the first half of the list.
Now you can stop iterating at n/2 of the list but you need to add the false values again to your Array. Now you are in O(n) again.
Let's make another assumption. You can always ignore the last value of the list. This means that you only iterate n-1 times that then is O(n-1) but in big O notation, you ignore constants so it gets O(n) again.
It is not possible to copy all n elements of a list to an Array in lower than O(n).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is my fibonacci generator:
package main
import "fmt"
func main() {
for i, j := 0, 1; j < 100; i, j = i+j,i {
fmt.Println(i)
}
}
It's working, but I don't know how can I improve it, I'd like more expert approaches about it, Thanks...
I assume you are talking about improving the time complexity (and not the code complexity).
Your solution computes the Fibonacci numbers in O(n) time. Interestingly, there exists an O(log n) solution as well.
The algorithm is simple enough: Find the nth power of matrix A using a Divide and Conquer approach and report (0,0)th element, where
A = |1 1 |
|1 0 |
The recursion being
A^n = A^(n/2) * A^(n/2)
Time complexity:
T(n) = T(n/2) + O(1) = O(logn)
If you think about it with a piece of paper, you'd find that the proof is simple and is based upon the principle of induction.
If you still need help, refer to this link
NOTE: Of course, the O(logn) time is true only if you want to find the nth Fibonacci number. If, however, you intend to print ALL of the n fib numbers, theoretically, you can not have a better time complexity than you already have.