Using advanced for loop instead of for loop

I am a bit confused and I would need some clarification. Not too sure if I’m on the right track, hence this thread.

Here is my code that I want to decipher into advanced foreach loop.

    int[] arrayA = {3, 35, 2, 1, 45, 92, 83, 114};
    int[] arrayB = {4, 83, 5, 9, 114, 3, 7, 1};
    int n = arrayA.length;
    int m = arrayB.length;
    int[] arrayC = new int[n + m];
    int k = 0;

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(arrayB[j] == arrayA[i])
            {
                arrayC[k++] = arrayA[i];
            }
        }
    }
    for(int i=0; i<l;i++)
        System.out.print(arrayC[i] + " ");
    System.out.println();

So far this is the point where I am stuck at:

    int[] a = {3, 8, 2, 4, 5, 1, 6};
    int[] b = {4, 7, 9, 8, 2};
    int[] c = new int[a.length + b.length];
    int k = 0;
    for(int i : a)
    {
        for(int j : b)
        {
            if(a[i] == b[j]) 
            {
                c[k++] = a[i];
            }
        }
        //System.out.println(c[i]);
    }
    for(int i=0; i<c.length;i++)
        System.out.print(c[i] + " ");
    System.out.println();
}

Solution:

You are almost there

for(int i : a)
{
    for(int j : b)
    {
        if(i == j) 
        {
            c[k++] = i;
        }
    }
}

With for(int i : a) access the elements in the array a using i.
If a is {3, 8, 2, 4, 5, 1, 6}, then i would be 3,8,2,.. on each iteration and you shouldn’t use that to index into the original array. If you do, you would get either a wrong number or a ArrayIndexOutOfBoundsException


Since you want to pick the numbers that are present in both the arrays, the length of array c can be max(a.length, b.length). So, int[] c = new int[Math.max(a.length, b.length)]; will suffice.

If you want to truncate the 0s at the end, you can do

c = Arrays.copyOf(c, k);

This will return a new array containing only the first k elements of c.

For Loop in Linux Terminal

I am using this for loop in the linux terminal:

for i in {1..21}; do

Here the script makes the loop go from 1 to 21.

How would I write the for loop, so that it goes through specific numbers; let’s say:

9, 24, 29, 32, 38.

I am using Terminal on Linux.

Solution:

For a fixed list of numbers, just put them after in separates by spaces:

for i in 9 24 29 32 38 
do 
  echo $i
done

Big-O analysis of for loop algorithm

I’m having trouble analyzing the following for loop algorithm:

for (int i = 1; i < n; i = i * C)
    for (int j = 0; j < i; j++)
        Sum[i] += j * Sum[i];

I know that the first line has a complexity of O(logn) (as long as C > 1), but what has me stumped is the second line. I believe I understand the basics of what is happening with it:

For example,

if n=20, the inner loop will do 1+2+4+8+16 “work”.

But I don’t know how to write that out. I’m nearly sure the total work done altogether in the loops is O(n), and that the first line is O(logn), but how do I more concretely specify what the middle line does?

Solution:

i will have values of a form:

C^0, C^1, C^2, ...., C^ log_c(n)

Hence the inner loop will run
C^0 + C^1 + C^2 + ... + C^log_c(n) times. This is a geometric series which sum up to:

enter image description here

So substiture r with C, n with log_c(n) we get:

(1-C^log_c(n)) / (1-C) = (1-n)/(1-C), which is positive for any C > 1 and proportional to n. Hence the complexity is O(n) indeed.

(The formula image is taken from Wikipedia )

Why can I not use math (subtraction) in a for-loop condition?

I have two lists of strings, listA and listB. listA is longer, and I want to make listB the same size by adding empty strings to it.

This works:

int diff = listA.size() - listB.size()
for (int i = 0; i < diff; i++) {
    listB.add("");
}

But this doesn’t:

for (int i = 0; i < (listA.size() - listB.size()); i++) {
    listB.add("");
}

Why is that?

Solution:

The first example the variable diff is constant whereas the second example the condition is evaluated on each iteration of the loop and therefore since you’re adding objects during iterations the size of the list will change hence why both code snippets don’t do the same thing.

I’d recommend you proceed with the first approach i.e caching the limit for the loop condition beforehand and not re-evaluating it during each iteration.