What is the time complexity of this code?
let time = 0;
for(let i = 1; i <= n; i++)
{
for(let j = 1; j <= i; j++)
{
for(let k = 1; k <= n*n; k += i*i)
{
time++
}
}
}
Is the innermost loop O(1) or O(n^2)?
The answer is O(N^2logN).
Analysis:
For a constant i, the innermost loop will be executed for about (N*N)/(i*i) times. The second loop will be executed for i times. So time++ will be executed for about ((N*N)/(i*i))*i = (N*N)/i times.
So the total expense will be the sum for every i.
(N*N)*(1/1+1/2+1/3....1/N) ≈ N^2logN is your answer.
Related
I need a 2 counter, i & j that output the following counters for each iteratation:
0,1,2,3,4,1,2,3,4,2,3,4,3,4
Counter variable sequence to achieve the above when output to console.log is as follows:
i,j,j,j,j,i,j,j,j,i,j,j,i,j
With each iteration, when i increments, j must start it's incrementation at i + 1 and must complete it's loop for each incrementation of i.
Each counter value will be used to present option from an array for comparison with one another.
I've tried using a nested for loop and it has not worked. I am also calling a function containing the second loop with some success but want to know if there is a more elegant way to accomplish this.
function createValues() {
for (i = 0; i < 9; i++) {
var counter_i = i;
decreasingLoop(counter_i);
}
}
var k = 1; // counter for inner values loop
function decreasingLoop(get_i) {
for (j = k; j < 10; j++) {
// functions using the counters are entered here.
if (j == 9) { // increments k so that the next time the counter runs it starts at +1 from previous iteration)
k++;
}
}
j = k;
}
I am getting the desired results but want to know if there is a simpler way to achieve the desired outcome.
for(var i = 0; i< 4; i++){
console.log(i)
for(var j = i+1; j<= 4; j++){
console.log(j)
}
}
If you write out your desired output in a different format, the solution may become a little clearer:
i: 0
j: 1,2,3,4
i: 1
j: 2,3,4
i: 2
j: 3,4
i: 3
j: 4
As you can see from the above example, i increases linearly, and only takes the values of 0 through to 3. Each time i takes a new value, j loops from i+1 up to 4. Using this idea, you can create an outer for loop for i (which loops from 0 to 3) and an inner for loop for j which starts at i+1 and finished when j is equal to 4:
for(let i = 0; i <= 3; i++) {
console.log(i);
for(let j = i+1; j <= 4; j++) {
console.log(j);
}
}
Try following nested loop - notice that in second loop initial for value is j=i+1
let n=4, m=5;
for(let i=0; i<n; i++) for(let j=i+1; j<m; j++) {
console.log({i,j});
}
I understand that
var arr; // This is an array of arrays
for (i = 0; i < arr.length; i++)
{
for(j = 0; j < arr[i].length; j++)
{
// Some code
}
}
is n^2, however my code below is a double nested for loop and im just curious on what the complexity for this type of function would look like
var arr; // This is an array of arrays
for (i = 0; i < arr.length; i++)
{
for(j = 0; j < arr[i].length; j++)
{
// Some code
}
for(k = 0; k < arr[i].length; k++)
{
// Some code
}
}
The complexity of consecutive pieces of code is the maximum complexity of each of them. So if you have two consecutive loops, and they're both O(n), then the complexity of both together is also O(n).
Since these two loops are nested inside an O(n) loop, the complexity of the whole thing is O(n^2). Just like the complexity of the original code.
A loop has the complexity O(n * content), a block of statement has the complexity of all its members added up O(n1 + n2 + ...). Now in your case that is
// v outer loop
// v inner loop 1
// v inner loop 2
O(n * ((n * 1) + (n * 1)))
= O(n * (n + n))
= O(n * 2n) // constants can be ignored
= O(n * n)
= O(n²)
Can you please explain to my why i get to different results;
This is the finished code and the result I wished:
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
for(var i = 0; i < arr.length; i++){
var sum = 0;
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
This gives me the wished result: [11,12,23,45,88]
But when i declare the sum variable outside the for loop like this
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
var sum = 0;
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
The result is: [20,20,5,5,1]
Can you please explain to me why ?, and how to scope here works?
if possible please with pictures (graphical) ..
Can you please explain to me why?
In the first case, you reset the value of sum to zero on each iteration of the outer loop. In the second case, you don't so sum just keeps accumulating.
and how to scope here works?
Variables declared with var have function scope (or global if declared globally). Your problem isn't to do with scope, it's because of where you assign 0 to sum.
if possible please with pictures (graphical)
Sorry, no pictures. :-(
First Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 0;
j iterates till end of loop
sum = 1+2+2+3+1;
i=2
sum = 0;
j iterates till end of loop
sum = 11+12+23+45+88;
....
Second Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 1+2+3+4+5;
j iterates till end of loop
sum = (1+2+3+4+5)+1+2+2+3+1;
i=2
sum = ((1+2+3+4+5)+1+2+2+3+1);
j iterates till end of loop
sum = ((1+2+3+4+5)+1+2+2+3+1)+11+12+23+45+88;
...
The for statement does not define a scope in javascript, only functions do (as per ES5).
Your problem is because the moment you assign 0 to sum: inside the outer for.
As we know, we can omit the initialization inside the for loop :
var i = 0;
for(; i < 10; i++) {
// do Something with i
}
However, i figure out if i omit the initialization inside the nested loop as well:
var i = 0;
var j = 0;
for(; i < 10; i++) {
for(; j < 10; j++) {
// only do Something with j
// outner loop only run once
}
}
Solution :
var i = 0;
var j;
for(; i < 10; i++) {
j = 0;
for(; j < 10; j++) {
// everything is fine!
}
}
Can anyone explain whats going on? I'm newbie in Javascript.
The loop runs multiple times, but your 'j' is already ten after the first run of i, so the second loop no longer runs during the next values of i.
If you would write something like this.
for(; i < 10; i++) {
for(; j < 10; j++) {
console.log("test")
}
console.log("another test")
}
You would see that both messages get printed 10 times.
The reason that your "j" loop, only runs "ten times once", is because you do not set J back to zero after it has run 10 times. So the statement j < 10is true for every loop if iafter the first run.
You could solve this by setting j = 0 inside the first for loop (if you do not want to put it inside the variable initialisation of the second loop.)
for(; i < 10; i++) {
j = 0;
for(; j < 10; j++) {
console.log("test")
}
console.log("another test")
}
Every time i increments, you need to reset j to 0.
If you rewrite your script so that each variable is initialised within the for loop statement itself, then every time i increments, j will be reset to 0.
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
}
}
I want to use for. There are two things, one, the console.log doesn't work inside the for statement. Second, it is not summing up. code below:
var numbers = [1,2,3,4];
var total = 0;
for (var i= 0; numbers.length < i; i++){
total += numbers[i];
// console.log(total); doesn't work
}
// console.log(total); gives 0
Change condition in for should be i < numbers.length not numbers.length < i
var numbers = [1,2,3,4];
var total = 0;
for (var i= 0; i < numbers.length; i++){
total += numbers[i];
}
console.log(total);
Your for loop is exiting immediately because of the condition
for (var i = 0; numbers.length < i; i++) {
Because numbers.length (in this case) is 4, and i is 0, the for loop never executes.
You probably want it flipped around, to say something like
for (var i = 0; i < numbers.length; i++) {