Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Given an unsorted array, assign every element to its immediate larger number after the current number, assign to -1 if no such number exists
Eg. 3 1 2 5 9 4 8 should be converted to
5 2 5 9 -1 8 -1
O(nlogn) or O(n) approach ?
Following is a way to do it in O(nlogn) :-
int newarr[];
MinHeap heap;
heap.push(0);
for(int i=1;i<n;i++) {
while(arr[heap.top()]<arr[i]) {
k = heap.pop();
newarr[k] = arr[i];
}
heap.push(arr[i]);
}
// No larger elements
while(!heap.isEmpty) {
k = heap.pop();
newarr[k] = -1;
}
Time Complexity : There are only n inserts and n deletes possible in the heap from the above code hence it is O(nlogn) where it take O(logn) for insert and delete in heap
Here is an sketch of a n log(n) solution:
copy your array in copy: O(n)
sort copy: O(n log(n))
for each i in input: (n loops)
perform a dichotomic search to find i in copy. O(log(n))
replace i in input O(1)
=> loop is in O(n log(n))
There are several place where it could be optimized, but I seriously doubt there could be an asymptotically better (eg: O(n)) algorithm. The reason is that if instead of replacing each number but the value of the next number you write the position of the next number then you have a sorted linked list in your array and sorting is know to be at least O(n log(n)). However, I agree that is is not a real proof and I might be wrong.
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 2 years ago.
Improve this question
enter image description here
Pseudocode
Can you help me finding out the complexity of the algorithm, as these j <-2j; i<-i+1 throw me out a bit.
Let us first consider the inner loop :
j = 1
while j<=n
O(1)
j = 2*j
This loops runs for the values of j = 1,2,4,8,... while j<=n.
This loop will thus have logarithmic time complexity i.e O(log n).
Now consider the outer loop :
i = 2
while i<=n
O(1)
//inner loop
i = i+1
This loops run for the values of i = 2,3,,4,...,n
Thus outer loop have linear time complexity ,i.e O(n).
So total time complexity = O(n)*O(log n) = O(n*log 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 2 years ago.
Improve this question
How to reduce the time compexity of this code,this is not exeuting within the time
exact ques from hackerrank--
Complete the circularArrayRotation function in the editor below. It should return an array of integers representing the values at the specified indices.
circularArrayRotation has the following parameter(s):
a: an array of integers to rotate
k: an integer, the rotation count
queries: an array of integers, the indices to report
vector<int> circularArrayRotation(vector<int> a, int k, vector<int> queries) {
int i,temp;
vector<int> ans;
//to perform number of queries k
while(k--)
{ //shift last element to first pos and then move rest of elements to 1 postion forward
temp=a[a.size()-1]; //last element
for(i=a.size()-1;i>0;i--)
{
a[i]=a[i-1];
}
a[0]=temp;
}
for(i=0;i<queries.size();i++)
{
ans.push_back(a[queries[i]]);
}
return ans;
}
The vector a is being rotated by k in the while loop. The whole loop can be removed by adding k and using modulo when accessing elements in a:
ans.push_back(a[(queries[i]+k)%a.size()]);
Note: you might need handling of negative values of k.
Note: Maybe it should be minus instead of plus.
An alternative could be to use std::rotate.
Furthermore, the ans vector should be pre-allocated to reduce the number of allocations to one:
ans.reserve(queries.size());
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
You have an array A of length N. you can perform an operation (you can perform the operation multiple times) on the elements of the array A.In the operation you can divide any element by its smallest factor greater than 1. You will be given Q tasks.In each task, you will be given an integer K and you have to tell the maximum number of elements in array A that can be reduced to 1 by using the operation at most K times.
Input:
First line contains two space-separated integers, N and Q.
Second line contains N space separated integers denoting the elements of array A.
Next Q lines contain an integer each, denoting the value of K.
Output:
For each task, print the answer of the tth task in new line.
Example Input: Example Output:
3 3 1
8 9 12 3
3 0
10
1
Explanation:
Number of operations required are 3,2,3 respectively.
For the first task, we can reduce any one of the three elements to 1.
For second task, we can reduce all the elements of the array to 1.
For third task, we cannot reduce any elements to 1.
Sorry for my english.
At first, you shuold build an Sieve of Eratosthenes, but instead of a boolean one, save the information on it of the smaller prime that divides the number, for example:
for (int i=4; i < MAXN; i+=2) sieve[i] = 2;
for (int i=3; i < MAXN; i+=2){
if (!sieve[i]) for (int j=i*i; j <= MAXN; j+=2*i){
if (!sieve[j]) sieve[j] = i;
} }
Then, using the sieve you may easy and optimal build an array B containing the number of operations needed for each element in A. After that, sort array B and you may use binary search to answer the queries.
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 5 years ago.
Improve this question
Am I correct in saying that this code will execute in O(N) time? This is to iterate through a map< int, vector< int > >.
int counter = 0;
unsigned int u = 0;
unsigned int v = 0;
bool notDone = true;
while (notDone) {
if (u < map.size()) {
if (v < map.at(u).size()) {
if (map.at(u)[v] == -1) {
++v;
}
else {
++counter;
++v;
}
}
else {
v = 0;
++u;
}
} else {
notDone = false;
}
}
If N is the number of map entries, and TV is the sum of the sizes of all the vectors, then your program is O(N + TV) regardless of whether it is written as a "while" or a nested "for".
If N is the number of map entries and MV is the mean of the sizes of all the vectors, then your program is O(N * MV) regardless of whether it is written as a "while" or a nested "for".
UPDATE: Commenter Tim notes that map.at is O(lg N) in the size of the map, and it is called O(N) times, so that makes the actual complexities O(N * lg N + TV) and O(N * lg N * MV) respectively. Thanks!
I do not know enough about the C++ standard library to say if there is a more efficient way; I would suspect that an O(1) iterator over the map exists. If it does, then you could use it rather than your index to iterate the map, and the lg N term would vanish again.
In a comment you said,
I have a map of vectors and I want to loop through every element in the map and every element of the vector, but I'm trying to do it in O(N) time.
That is not possible.
If you have M elements in the map and N elements in each of the vectors, you have M*N elements all together. There is no way you can iterate over them in O(N) time.
You iterate over the map of length N, and in each loop you iterate over a vector of length M. Assuming all vectors have the same length, your algorithm will run in O(N*M).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This question is for revision purposes from a past exam paper
I just want to know if I am on the right track
1. int i=1;
2. while (i <= n) {
3. for (int j=1; j<10; j++)
4. sum++;
5. i++;
6. }
7. for( int j = 1; j <= n; j++ )
8. for( int k = 1; k <= n; k=k*2 )
9. sum++;
1.) How many times is statement 4 executed? A. O(n) B. O(n^2) C. O(log n) D. O(n log n) E. none of the
above
Here I chose A
2.) How many times is statement 9 executed? A. O(n) B. O(n^2) C. O(log n) D. O(n log n) E. none of the above
Because of line 8 (k=k*2) I chose C
3.) What is the running time of the entire code fragment? A.
O(n) B. O(n^2) C. O(log n) D. O(n log n)
Since O(n)+O(logn)=O(n) so I chose A
Your answer 1 is correct, it's inside a loop controlled only by n.
Answer 2 is incorrect. It would be O(log n) if line 7 did not exist but, because line 7 is forcing lines 8 and 9 to run multiple times dependent on n, the answer is O(n log n).
Answer 3 is the correct reasoning but suffers from the fact answer 2 was wrong. O(n) + O(n log n) simplifies down to O(n log n).
So the answers are A, D and D.
I dont know how the questions where formulated, but if the wording is like you say, your examiner didnt know the right definition of big O (at least when he expects the "right" answers) – as "Big O functions include smaller". So something that executes as a function of n in f(n) = 10 n which is linear is also in O(n), O(n^2), O(n log n).
If one asks for the "smallest" possible, your answers would be
Statement 4 is executed 10 n times, so A
Statement 9 is executed n*log n times, so D
Here it is executed the sum of both, n + n*log n so (here you lost an *n), so D would be the right.
So if multiple answers were possible and it was just asked for how much it is executed, the right answers would be
A,B,D
B,D
B,D
Ans 1: A ie. O(n) as the statement 4 would be executed 10*n times.
Ans 2: D ie. O(nlog(n)) as the statement 9 would be executed n*log(n) times.
Ans 3: D as the overall complexity [O(n) + O(nlog(n))] would be n*log(n).