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
I am trying to find the time complexity of simultaneous loops, and I am stuck up on this question.
Find the time complexity of this simultaneous loop
for(int i=1,int j=0; i*i<=n && j<=n ;j=j*2 ,i++);
Can someone explain how to approach these types of questions?
Variable "i" is increased by 1 and stops at sqrt(n). Variable "j" is increased by two multiplied every step, but its initial value is zero, so it can not break the loop. So "i" variable reached to the end when n limits to a big number.
For complexity calculations, we focus the power of n or logarithmic of n values. Scalar values are not important. We discard 1, 2 or 10 steps. So the complexity of your statement is O(sqrt(n)). Initial values do not affect the complexity of statements.
Related
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 4 years ago.
Improve this question
PLEASE NOTE: I am not looking for code, but for a way how to solve this problem.
My input is world that looks like this:
The problem is, i have to find the biggest number, without using OWN variables I could declare myself, and I'm only allowed to use turnLeft(), turnRight(), move(), isLeft/Right/FrontClear(), getNumber() and putNumber() functions to move < around the world.
Could you please give me a 'verbal solution' or a hint how to do such thing?
While you cannot use any variable, note that you do have available memory (getNumber() and putNumber()). For instance, you could think about leaving a mark in positions you have already been to implement some kind of flood fill.
Further, you can fill the floor with the biggest number you have seen yet. Basically, encoding your own state in the floor.
Important questions:
Is the configuration of the maze always fixed?
Is the range of possible numbers in the floor fixed to a reasonable range (e.g. digits 1-9)?
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 6 years ago.
Improve this question
So I created a linked List in C using structs and it stores ints. My mission is to calculate the average of the values in the list without using recursion or loops.
I already have the list's item count I just need the sum.
Any ideas?
Simply have two varibles - count, total; Update them in Add and Delete.
When u want avg, just return total/count.
The list is not length bounded, but i found a solution.
I create a variable in the list's ateuct to save the sum of the list, and i change the sum of the list each time I add or remove a cell. When I want to calculate the sums I'll just divide the sum by the count.
Thank you anyway for your help :)
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
Given an array A[1..n] and Q queries on that array.
Each query is of the format (x a b), and for every query I need to find out, how many elements in the sub-array (a,b) of main array denoted by A[a..b] are less than x.
How to perform this job efficiently ?
OR If required what kind of data structures to build on top of array, to perform the job efficiently ?
Limits :
n <= 10^6
Q <= 10^4
HINTS
Use a Fenwick tree to store a cumulative histogram of which values you have seen.
Turn your queries (x a b) into a F(x,a)-F(x,b-1) where F(x,a) counts numbers less than x in the range 0..a.
Sort these F(x,a) queries by a.
Then iterate over the array index i, inserting element A[i] into the Fenwick tree, and answering any F(x,a) queries that have a==i.
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
Is Foreach loop a definite or indefinite one? Meaning do I know prior to the execution of the loop the number of iterations it is going to do?
No. foreach in most languages (c#, java) doesn't know beforehand how many iteration it will have to do. You could even use foreach to go through an infinite sequence.
That's why for exists
In most languages a foreach loop will iterate over a collection of elements until the collection ends. In most languages I can think of a collection can be a definitive set of elements contained in memory OR a calculated set of elements that could in theory be of an infinite length. This would make the foreach loop "indefinite" in most modern languages.
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 9 years ago.
Improve this question
I'm trying to decimate a given vector with a logarithmic rule instead of a linear one.
e.g.:
The given vector has 100 elements. I want to reduce it to a vector of 10 elements that are the same elements of the starting vector taken with a logarithmic rule on the int interval [0,99].
I hope to be clear enough. Any help is appreciated !
Have a nice day !
In pseudo-code :
int n; // Length of subsampled array
for i:
new_array[i] = array[(int)(log(i)/log(n)*length(array))]
Is that what you are trying to do ?