Time complexity of this nested for loop - java

for (int i = 0; i < this.tiles.length * this.tiles.length; i++) {
int row = i / this.tiles.length;
int col = i % this.tiles.length;
for (int j = i+1; j < this.tiles.length * this.tiles.length; j++) {
int compareRow = j / this.tiles.length;
int compareCol = j % this.tiles.length;
if(this.tiles[compareRow][compareCol] < this.tiles[row][col]) {
count++;
}
}
}
I have to calculate the time complexity of this function, i first thought it was ~n*n-1 but i'm pretty sure that's actually wrong. Can anybody explain what the time complexity of this piece of code is?

There are 2 for loops each iterating (tiles.length*tiles.length) times . So it is :
Number of times of comparison :
First set of comparison(i=0) : tiles.length2
Second set of comparison (i=1): tiles.length2 -1
.
.
Last set of comparision(i=tiles.length2 -1 ) : 1
= ( ( tiles.length2 ) + ( tiles.length2 -1 ) + ( tiles.length2 - 2) ....... + 2 + 1 )
=
O(tiles.length3)

for (int i = 0; i < this.tiles.length * this.tiles.length; i++) { //O(n)
int row = i / this.tiles.length;
int col = i % this.tiles.length;
for (int j = i+1; j < this.tiles.length * this.tiles.length; j++) { //O(n^2) it's squared because there are two loops
int compareRow = j / this.tiles.length; //n +
int compareCol = j % this.tiles.length; //+ n
if(this.tiles[compareRow][compareCol] < this.tiles[row][col]) { //n
count++;
}
}
}
O(n^2 + n) == O(n^2)
The way I was taught was that for every loop it's O(n) so, a nested loop would naturally be O(n^2), and with every condition or operation would be n + n ..nth which is where O(n^2 + n) = O(n^2)
Hope that helped a bit.
checkout the resource below for a more depth explanation.
Resources:
https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

Related

How would I calculate big-O for this Algorithm

I have got this algorithm
int count = 0;
for(int i = n; i >= 1; i = i/2) {
for ( int j = 1; j <= i; j++) {
count++;
}
}
Am I right in saying that the Big-O for this would be n/2?
TL;DR The time complexity is O(n).
More details
Am I right in saying that the BigO for this would be n/2?
No that is accurate, in big-O notation you drop the constant part so (1/2)n simplifies to O(n).
I am not sure where that n/2 comes from because only the outer loop
for(int i = n; i >= 1; i = i/2) {
...
}
is log2n not n/2.
And with both loops together:
int count = 0;
for(int i = n; i >= 1; i = i/2) {
for ( int j = 1; j <= i; j++) {
count++;
}
}
the count would vary between N and 2N.
Let us go through the calculations:
int count = 0;
for(int i = n; i >= 1; i = i/2) {
for ( int j = 1; j <= i; j++) {
count++;
}
}
The inner loop will execute N iterations then N/2, then N/4 ... until N/N.
In another words we have (N + N/2 + N/4 ... N/N) which can be simplified to N * (1/2 + 1/4 + .. + 1/2^L)), with L = Log2 N.
This (1/2 + 1/4 + .. + ) series is well-known for being 1. Therefore, we can simplified N * (1/2 + 1/4 + .. + 1/2^L)) to O(N).
You are correct! This is basically a geometric progression with a quotient of 2 and the number of elements is lg(n) as we divide i by 2 each iteration of the outer loop.
1, 2, 4, ..., n
Using a known formula to calculate the sum, we get:
The reason we have lg (n) elements, is because we divide i each iteration by 2, thus we need to solve for the number of iterations k:

finding the correct time complexity of these codes

I was trying to find time complexity of these 2 codes but i am not sure about my answers .
code1
int i = 1;
int count = 0;
while (i < n) {
for (int j = 0; j < i; j++) {
count++;
}
i *= 2;
}
I calculated the number of steps in loops and I reached to (log n)^2 but i am not sure about it .
Code2
int k=0;
for (int i = 2; i <= n; i++) {
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
k++;
break;
}
}
}
and for this one I got ( n * log n)
actually I am new to calculating time complexity and I am not sure about them , could you help me find the correct answer .

Minimum Adjustment Cost

I am trying to solve this question:
Given an integer array, adjust each integers so that the difference of
every adjacent integers are not greater than a given number target.
If the array before adjustment is A, the array after adjustment is B,
you should minimize the sum of `| A[i]-B[i] |. You can assume each number in the array is a positive integer and not greater than 100.
`
I see a dp solution but I don't quite understand the recurrence equation.
public static int MinAdjustmentCost(ArrayList<Integer> A, int target) {
// write your code here
if (A == null || A.size() == 0) {
return 0;
}
// D[i][v]: 把index = i的值修改为v,所需要的最小花费
int[][] D = new int[A.size()][101];
int size = A.size();
for (int i = 0; i < size; i++) {
for (int j = 1; j <= 100; j++) {
D[i][j] = Integer.MAX_VALUE;
if (i == 0) {
// The first element.
D[i][j] = Math.abs(j - A.get(i));
} else {
for (int k = 1; k <= 100; k++) {
// 不符合条件
if (Math.abs(j - k) > target) {
continue;
}
int dif = Math.abs(j - A.get(i)) + D[i - 1][k];
D[i][j] = Math.min(D[i][j], dif);
}
}
}
}
int ret = Integer.MAX_VALUE;
for (int i = 1; i <= 100; i++) {
ret = Math.min(ret, D[size - 1][i]);
}
return ret;
}
Could someone explain it to me?
You need to minimize the cost of the adjustment, which is the value you increase/decrease every element such that the difference between every adjacent elements is less than or equal to target. The dp solution is to try every possible value and minimize the cost on the valid ones (when abs(A[i]-A[i-1]) <= target)
First thing is to fill the cost for adjusting first element to 1-100 which is done here:
for (int i = 0; i < size; i++) {
for (int j = 1; j <= 100; j++) {
D[i][j] = Integer.MAX_VALUE; // fill with MAX_VALUE because we want to minimize
if (i == 0) {
// for the first element we just set the cost of adjusting A[i] to j
D[i][j] = Math.abs(j - A.get(i));
}
Now you have D[0][j] as the cost to adjust the first element to be j. Then for every other element, you loop again (from k = 1 to k = 100) for other elements and try to change A[i] to j. And then you check if abs(k-j) is valid (less than or equal to target) then you can adjust A[i] to be j and A[i-1] to be k so you minimize on D[i][j].
Here D[i][j] means the cost of changing A[i] to j and D[i-1][k] is the cost of changing A[i-1] to k. so for every k and j if they are valid (abs(k-j)<=target) then you add them together and minimize the value saved in D[i][j] so you can use it for next element, which is done here:
else {
for (int k = 1; k <= 100; k++) {
// if abs(j-k) > target then changing A[i] to j isn't valid (when A[i-1] is k)
if (Math.abs(j - k) > target) {
continue;
}
// otherwise, calculate the the cost of changing A[i] to j and add to it the cost of changing A[i-1] to k
int dif = Math.abs(j - A.get(i)) + D[i - 1][k];
// minimize D[i][j]
D[i][j] = Math.min(D[i][j], dif);
}
}
At the end, you need to loop from 1 to 100 at the last element and check which is the minimum value over all, which is done here:
int ret = Integer.MAX_VALUE;
for (int i = 1; i <= 100; i++) {
ret = Math.min(ret, D[size - 1][i]);
}
I think if you split the initialization code and the DP calculation code it would be easier to understand, for example:
// fill the initial values
for (int i = 0; i < size; ++i) {
for (int j = 1; j <= 100; ++j) {
// on the first element just save the cost of changing
// A[i] to j
if (i == 0) {
DP[i][j] = abs(j-A.get(i));
} else {
// otherwise intialize with MAX_VALUE
D[i][j] = Integer.MAX_VALUE;
}
}
}
for (int i = 1; i < size; i++) {
for (int j = 1; j <= 100; j++) {
for (int k = 1; k <= 100; k++) {
// if abs(j-k) isn't valid skip it
if (Math.abs(j - k) > target) {
continue;
}
// if it is valid, calculate the cost of changing A[i] to j
// and add it to the cost of changing A[i-1] to k then minimize
// over all values of j and k
int dif = Math.abs(j - A.get(i)) + D[i - 1][k];
D[i][j] = Math.min(D[i][j], dif);
}
}
}
// calculate the minimum cost at the end
int ret = Integer.MAX_VALUE;
for (int i = 1; i <= 100; i++) {
ret = Math.min(ret, D[size - 1][i]);
}

Why does the inner loop range make a difference in the way this problem is solved?

Write a static method printNumbers that takes an integer max as an argument and prints out all perfect numbers that are less than or equal to max.
At first, I kept getting the wrong answer because the inner loop was set to j < max before I changed it to j < i. However, I don't understand why that range would matter, because wouldn't i % j != 0 anyway, even if the range of j were to be larger?
for (int i = 1; i <= max; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
System.out.print(sum + " ");
}
}
If I changed the inner loop j < max, then printNumbers(6) gives both 1 and 6, but printNumbers(500) gives only 1 and no other number.
If you set j < max in the inner loop, then when j = i, i % j == 0 returns true and skews your result.
This is a good example of a mathematical error to watch out for in coding.

What is the difference? And does this matter in bigger loops?

I was wondering if there is a complexity and time difference when doing these two operations:
1)
int sum = 0;
for (int i = 0; i < 1000000; i++) {
sum = sum + i;
}
2)
int sum = 0;
for (int i = 0; i < 1000000; i++) {
sum += i;
}
or maybe image the problem to be bigger numbers/data, this is just an example.
These variants are the same from performance point of view (both will be as iadd instruction in java bytecode)
But sum += 1 replaced with sum = (int) (sum + 1)
And it has differrens for types like byte or short for compilation
E.g. this code will be compiled
byte i = 0;
for(int j = 0; j < 10; j++) {
i += j; //i = (byte) (i + j)
}
but you will get compilation error for code
byte i = 0;
for(int j = 0; j < 10; j++) {
i = i + j;
}
There is a difference in priority, compound assignment sum += i is less primary than assignment and addition sum = i + 1.
Incrementation i++ is even more primary.
for more information : operator precedence

Categories

Resources