I have this for loop and I am preparing for an exam. I want to see how many times this loop executes.
without knowing the value of K and somevalue, how can we determine the number of times SMALL is printed. The book answer says it is K-1.
for(int i=2; i<= k; i++)
if(arr[i]< someValue)
System.out.println("SMALL");
If it were simply
for (int i = 0; i < k; i++) {
The loop would run k times since you are starting at 0 but not reaching k
So add 2 to the starting point you get k-2 times
add back in 1 because it is <= and you get k-1 times.
It tends to get a little less obvious when the increment isn't 1
Note: That is how many times the loop will execute. The number of times SMALL will print can't be determined without more information.
in this case somevalue doesn't matter, you can calculate the count of executed loop your self by using another variable, use this:
int count = 0;
for (int i = 2; i <= k; i++) {
if (arr[i] < someValue) {
System.out.println("SMALL");
}
count++;
}
System.out.println("this for loop executed: " + count + " times.");
The question needs more inputs because without the values of arr[] and somevalue, we cannot find the answer.
However, since the loop starts at 2 and runs till its equal to k, we can say that it will run till k times -1 (since loop starts 2 for a <= condition instead of the usual 1), which translates to k-1 executions of the loop.
Still it doesn't guarantee the number of times "SMALL" will be printed. That is only possible if all values in the array arr are less than somevalue
Related
people of the internet,
I am studying algorithms and their complexity and I wrote a "naive" code for finding the number of inversions in an Array. First, it seemed easy and then I started wondering if that j=i+i changes the second loop's complexity from O(n) in a worst-case scenario to something lower ?
Here is my code written in java :
public static void naiveInversionCount(int[] T){
int count = 0;
for(int i = 0; i < T.length -1; i++){ // O(n)
for(int j = i+1; j < T.length; j++){ // O(n) ???
if(T[i]> T[j]) count++; // O(1)
}
}
System.out.println("Naive method returns : " + count);
}
Thank you very much
The outer loop runs exactly n times.
The inner loop runs n−1, n−2, …, 0 times per outer loop. That is, on average, n/2 times.
And count++ runs exactly once per loop.
Thus the nested loop runs 1·n(n/2) times, which is in 𝑂(n²).
I have debugged this code and it appears to run a for loop even though the termination condition is met.
This program takes two types of input:
Line 1 - How many data points there are following (N)
Lines 2 to N - The data points
The program should then print the smallest difference between all of the data points.
So, for instance, a sample input would be (on separate lines): 3 5 8 9
There are 3 data points (5 8 9), and the smallest difference is between 8 and 9, so the program should return 1.
I am trying to build the program in a way in which the data points are populated into an array at the same time as the comparisons are made. Obviously I could separate those concerns, but I am experimenting. Here it is:
package com.m3c.vks.test;
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); //How many data points there will be
int strengthArray[] = new int[N]; //Initialise the array to be of length = N, the first input line
int tempStrengthDifference=99999; //junk difference set arbitrarily high - bad practice I know
int lowestStrengthDifference=99999;
for (int i = 0; i < N; i++) //Go through all elements of array
{
strengthArray[i] = in.nextInt(); //read a value for the ith element
System.out.println("i: " + i); //test
if (i > 0) //do not execute the next for loop if i = 0 as cannot evaluate sA[-1]
{
for (int j = i - 1; j < 1; j--) // **this is line 20** from wherever the array has populated up to, work backwards to compare the numbers which have been fed in thus far
{
System.out.println("j: " + j); //test
tempStrengthDifference = Math.abs(strengthArray[i] - strengthArray[j]); //finding the difference between two values
if (tempStrengthDifference < lowestStrengthDifference) //store the lowest found thus far in lowestSD
{
lowestStrengthDifference = tempStrengthDifference;
}
}
}
}
System.out.println(lowestStrengthDifference);
}
}
Everything is fine up until when i = 1 on line 20. At this point, j is set to i - 1 = 0 and the difference is found. However, when the for loop comes back around again, the termination condition of j < 1 is not met, and instead the loop continues to set j = -1, at which point it throws an out of bounds error as it clearly cannot evaluate strengthArray[-1]
Any ideas? Thanks
Have a look at your loop: for (int j = i - 1; j < 1; j--)
You start with j = 0 when i == 1 and thus j < 1 is ok.
The next iteration has j = -1 (0-1) and hence you get the problem.
Do you mean to use j >= 0 as your loop condition instead? Note that the second parameter is not a termination condition but a continuation condition, i.e. as long as that condition is met the loop will execute.
The reason behind your failure is the inner loop variable change.
When i=1, j=0 and after executing the loop once, J is decremented, and thus j becomes - 1. The condition j<1 is satisfied since you have written j--, change it to j++ and you should be fine.
I have multiple test arrays that have to pass through a method I'm writing. I can't for the life of me figure out why this is failing.
Here is the code:
for (int i = 0; i < testArray1.length; i++) {
for (int j = 0; j < testArray1.length; j++){
if (testArray1[i].equals(testArray2[j])){
System.out.println(testArray1[i]);
testArray2[j] = null;
counter++;
i++;
}
}
}
What I'm trying to do is have the double loop test for one element from testArray1 and once it's found
have that entry set to null (in the case that it's a repeated entry)
increase the counter (which I'm using later to compare if the number of entries counted as found equals the number of entries in the array thus making the arrays equal)
and increase i so that it skips to the next entry since the remaining ones don't need to be tested
Doing this I get the output of:
Bag{Size:4 [Jill] [John] [Jack] [Jack] }
Bag{Size:4 [Jack] [Jill] [John] [Jack] }
Jill
John
Jack
counter: 3 Array size: 4
My problem is that the last and final Jack entry isn't tested at all.
Another thing is that I'm not sure if I should be resetting my j counter. I'm assuming that the j for loop doesn't reset once I increment the i counter. I tried to set j = 0 after counter++ but doing that broke the program with an ArrayOutOfBounds for i for god knows what reason.
EDIT: I just realized why it's out of bounds for j=0. The loop finds the last entry, sets j, but also increases i to 5... I think? Still not sure how to get this test to run how I want it to though.
Just to explain better -
Bag{Size:4 [Jill] [John] [Jack] [Jack] }
Bag{Size:4 [Jack] [Jill] [John] [Jack] }
JillJillJillJill i=0, j=1
nullnullnullnull and you do a i++ , so i=1, counter = 1
JohnJohnJohnJohn i=1, j=2, counter = 2
nullnullnullnull and you do a i++ , so i=2, counter = 2
JackJackJackJack i=2, j=3, counter = 3
nullnullnullnull and you do a i++ , so i=3, counter = 3
counter: 3 Array size: 4
the outermost loop wouldn't execute this for the second time(note all the above iteration is from your inner loop), since there is another incremenet i++ within the loop making i=4 and exiting the loop.
Hope that helps.
Edit - The question seems to have changed in terms of output shared above, but the logic to be corrected still prevails.
I believe the issue revolves around the confusion over this inquiry
and increase i so that it skips to the next entry since the remaining
ones don't need to be tested
Incrementing i in your if-statement won't end the current loop you're in to move on to the next entry in testArray1. It'll just increment i and keep checking the rest of testArray2. If you want to move on to the next entry in testArray1, you should use a break statement to get out of the loop iterating through testArray2 like so,
for (int i = 0; i < testArray1.length; i++) {
for (int j = 0; j < testArray1.length; j++){
if (testArray1[i].equals(testArray2[j])){
System.out.println(testArray1[i]);
testArray2[j] = null;
counter++;
break;
}
}
}
static int sumAfterPos(int[] A) {
int N = A.length;
for (int i = 0; i < N; i += 1) {
if (A[i] > 0) {
int S;
S = 0;
for (int j = i + 1; j < N; j += 1) {
S += A[j];
}
return S;
}
}
return 0;
}
I am having trouble figuring out whether this code runs in O(n^2) or in O(n). I am not sure whether the return S will have a big impact on the runtime.
It is O(N) in time.
For example, A[K] > 0, then you have already have K steps. Then you run another N-K steps and return. So totally you have O(N).
Let us say, all A[i] < 0, this will make the inner loop away. So it is O(N) in this case.
Now let us say, A[0] > 0, this will make the out loop only repeat once and the inner loop will run from 1 to N - 1, so totally you have 1 + (N-1 - 1 + 1) = N.
Now let us say, A[1] > 0, this will make the out loop only repeat twice and the inner loop will run from 2 to N - 1, so totally you have 2 + (N-1 - 2 + 1) = N.
...
Now let us say, A[k] > 0, this will make the out loop only repeat k + 1 times and the inner loop will run from k + 1 to N - 1, so totally you have k + 1 + (N-1 - k -1 + 1) = N.
Now let us say, A[N-1] > 0, this will make the out loop only repeat N and the inner loop will never run, so totally you have N times.
This looks to be O(n), or rather, exactly N iterations will occur. If the statement (A[i] > 0) is true, then a value would be returned after the inner for loop completes its iterations. If there are N elements in the array A, and the outer for loop is at iteration i, and the conditional is met, then the inner for loop will iterate at most N-i times and then return. If the conditional is never met, then the outer for loop will iterate N times. Thus, exactly N iterations between the outer- and inner- for loops will execute.
Return statements are generally never taken into account in determining the runtime complexity of an algorithm (unless the return statement for some reason is non-trivially complex (eg recursion)).
EDIT: For another perspective, note that as the index i increases linearly, the starting point for the index j increases linearly at the same rate, and thus the possible remaining work is decreasing linearly at the same rate as well. Since the the function is guaranteed to return once the inner loop is reached, the total number of iterations between the two for loops is i+j=N by the end of execution.
http://puu.sh/8ekfm.png
Zonko's government (The Kingdom of Zumbania) decided to ban directly adding numbers greater than 1 to other numbers. Zonko decided to create his own addition method (for positive integers) to circumvent the ban. Can you help finish his code?
To get the sum of a and b, Zonko starts by setting a variable sum equal to a. He then repeatedly adds 1 to sum until it reaches a+b. 1 is added every iteration of the loop until its been added the right number of times. What is the correct condition in the loop so it stops at the right time?
public int add(int a, int b){
int sum = a;
for(int i=1; LOOP-CONDITION; i=i+1){
sum = sum + 1; //this will add 1 to sum every iteration
}
return sum;
}
Can someone explain this and tell me how they got the answer?
The Java idiomatic for loops to perform an action a certain number of times are:
for(int i=1; i <= [numTimes]; i=i+1){
and
for(int i=0; i < [numTimes]; i=i+1){
The former has the correct starting condition, so your loop condition would be: i <= b.
The trick is in the words :
Zonko's government (The Kingdom of Zumbania) decided to ban directly
adding numbers greater than 1 to other numbers.
This means a cannot be added to b directly when b is greater than 1.
Assume you want to add a = 5 and b = 3. You can get the answer by adding 1 to 5 three times.
a = 5;
loop(b times){
a = a + 1;
}
Here is how the loop will go:
a = 5;
loop starts
a = 5 + 1;
a = 6 + 1; // we made it 6 in previous step
a = 7 + 1;
loop ends