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
for(i=1, count=0;i<=n;i=i+k)
for(j=1;j<=k;j++)
count++;
I can't figure out how many times the inner for loop is executing.
I need the answer in terms of n and k
Let's look at the the two loops individually.
The outer loop starts with i=1, and increments it by k in each iteration, until it's greater than n. In other words it can run n/k times, or, to be accurate, floor(n/k) times, since a loop can't run a non-whole number of times.
The inner loop is relatively simpler - it starts with j=1 and increments it by one in each iteration until it's greater than k, for a total of k times.
Put these two together and you'll get floor(n/k)*k.
EDIT:
As pointed out in the comment, this analysis is true if n>=k. If n<k the outer loop will run exactly once. I.e., the total times run would be: max(1, floor(n/k))*k.
Answer: floor(n/k) * k. To check it exactly, I think you can print with print.
Related
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 1 year ago.
Improve this question
What mistake am I making here? I'm not very good with loops, I just started off.
My answer so far:
Edit
Thank you so much everyone for helping out, I fixed the issue :) I'll stop posting the picture of my code from now on.
for (int i = 1; i >= n; i++)
Let's translate this to english:
While i is bigger than (or equal to) n (i >= n), you add 1 to i (i++) and run what's inside the curly brackets. That's just an infinite loop. i is just going to get bigger and bigger, and will never be smaller than n, which is required for the loop to stop. (It runs as long as or while i >= n).
Unless n is > 1 to begin with, then the loop won't even iterate once.
You got the inner loop right, but in the outer loop you have to write i <= n instead of i >= 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 4 years ago.
Improve this question
Time Complexity of Juggling algorithm for array rotation(Suppose 'd' times) is computed as O(n), where n is the size of the array. But for any number of rotation(i.e. for any value of 'd'), the algorithm runs exactly for n times. So, shouldn't the time complexity of the algorithm be "Theta(n)" ? It always loops for n times in any case.If not, can anyone provide a test case where it doesn't run for n times?
It is unclear what you ask, but if we look at https://www.geeksforgeeks.org/array-rotation/ we see that it is described as O(n) time but if we want to rotate zero steps it could be done in O(1) time, so it doesn't always take n times - i.e. Theta(n) would be wrong; but O(n) is correct.
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 5 years ago.
Improve this question
How to find the maximum difference between two numbers in an array using single loop and single iteration?
Ex: Consider an array A[20]={10,3,6,8,9,4,3} How to find the maximum difference between two numbers in the array using single loop and single iteration?
To solve this problem in a single loop consider how the numbers A and B that produce maximal difference A-B influence the difference:
The difference will be greater when you pick larger A, and
The difference will be greater when you pick smaller B
Once you make this observation, it becomes clear that you are looking for the largest A and the smallest B in order to achieve the maximum difference. This can be done in a single loop in O(n) time and O(1) space.
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 7 years ago.
Improve this question
How would I do this in 1 loop since I need to loop again and close every second door? Do they want me to loop through the program 100 times? Should I be using pointers ?
Yes, you should loop through the program 100 times if you want to simulate this behavior.
But if you want to know the final condition(Open/Close) then you can have better algorithm:
As every perfect square number only have odd number of factor, if number is perfect square then final condition of door is open otherwise door is close.
If you are interested see perfect square number and Why perfect squares only have odd numbers of factor .
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
How would I write a function that contains a nested for-loop but is only order(n)? Im not sure if i need to use recursion or not.
If the inner for loop is a constant number of loops rather than a variable number of loops, while the outer loop is a variable number of loops (or vice versa) the time complexity is O(n*C) where C is a constant, which just means O(n) (since big O notation is only concerned with growing factors).