A puzzle related to nested loops - loops

For a given input N, how many times does the enclosed statement executes?
for i in 1 … N loop
for j in 1 … i loop
for k in 1 … j loop
sum = sum + i ;
end loop;
end loop;
end loop;
Can anyone figure out an easy way or a formula to do this in general. Please explain.

First, I written a C code to generate sum:
int main(){
int i =0, k =0, j =0, n =0;
int N =0;
int sum =0;
N =10;
for (n=1; n <= N; n++){
// unindented code here
sum =0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=1; k<=j; k++)
sum++;
printf("\n N=%d sum = %d",n, sum);
}
printf("\n");
}
Simple compile and generate result for N=1 to N=10 :
$ gcc sum.c
$ ./a.out
N=1 sum = 1
N=2 sum = 4
N=3 sum = 10
N=4 sum = 20
N=5 sum = 35
N=6 sum = 56
N=7 sum = 84
N=8 sum = 120
N=9 sum = 165
N=10 sum = 220
Then, Tried to explore How this works? with some diagrams:
For, N=1:
i<=N, (i=1)
|
j<=i, (j=1)
|
k<=j, (K=1)
|
sum=0. sum++ ---> sum = 1
That is (1) = 1
For, N=2:
i<=N, (i=1)-------(i=2)
| |-----|-----|
j<=i, (j=1) (j=1) (j=2)
| | |----|----|
k<=j, (K=1) (K=1) (K=1) (K=2)
| | | |
sum=0, sum++ sum++ sum++ sum++ --> sum = 4
That is (1) + (1 + 2) = 4
For, N=3:
i<=N, (i=1)-------(i=2)--------------------(i=3)
| |-----|-----| |---------|-------------|
j<=i, (j=1) (j=1) (j=2) (j=1) (j=2) (j=3)
| | |----|----| | |----|----| |-----|-----|
k<=j, (K=1) (K=1) (K=1) (K=2) (K=1) (K=1) (K=2) (K=1) (K=2) (K=3)
| | | | | | | | | |
sum=0, sum++ sum++ sum++ sum++ sum++ sum++ sum++ sum++ sum++ sum++
--> sum = 10
That is (1) + (1 + 2) + ( 1 + 2 + 3 ) = 10
N = 1, (1) = 1
N = 2, (1) + (1 + 2) = 4
N = 3, (1) + (1 + 2) + (1 + 2 + 3) = 10
N = 4, (1) + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) = 20
N = 5, (1) + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4 + 5) = 35
Finally, I could understood that sum of N in three loop is:
(1) + (sum 0f 1 to 2) + ... + (sum of 1 to (N-2)) + (sum of 1 to (N-1) ) + (sum of 1 to N)
or we can write it as:
=> (1) + (1 + 2) + ...+ (1 + 2 +....+ i) + ... + (1 + 2 + ....+ N-1) + (1 + 2 + ....+ N)
=> ( N * 1 ) + ( (N-1) * 2) + ( (N-2) * 3) +...+ ( (N -i+1) * i ) +... + ( 1 * N)
You can refer here for simplification calculations: (I asked HERE )
[YOUR ANSWER]
= ( ((N) * (N+1) * (N+2)) / 6 )
And, I think its correct. I checked as follows:
N = 1, (1 * 2 * 3)/6 = 1
N = 2, (2 * 3 * 4)/6 = 4
N = 3, (3 * 4 * 5)/6 = 6
N = 4, (4 * 5 * 6)/6 = 10
N = 5, (5 * 6 * 7)/6 = 35
Also, The complexity of this algorithm is O(n3)
EDIT:
The following loop also has same numbers of count, that is = ( ((N) * (N+1) * (N+2)) / 6 )
for i in 1 … N loop
for j in i … N loop
for k in j … N loop
sum = sum + i ;
end loop;
end loop;
end loop;

Related

How is the time complexity of the following while loop will be O(logn)?

I did not understand how this piece of code will generate a complexity of O(log n).how many times the statements inside the loop will work?
int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
}
Let N=16, number of iteration is
i | loop
--------
16 | 1
8 | 2
4 | 3
2 | 4
1 | 5
You can see log2(16) = 4 and the number of iterations is log2(16) + 1. Or you can create a formulation for it
f(N) = F(N/2) + 1. Let suppose we have N = 2^K and so we have:
F(N) = F(N/2) + 1
F(N) = F(N/4) + 1 + 1
F(N) = F(N/8) + 1 + 1 + 1 = F(N/(2^3)) + 3
...
...
...
F(N) = F(N/2^K) + K => F(2^K/2^K) + K => F(1) + K => 1 + K
So if N = 2K Then K = log2(N)
The number of iterations of the loop is the number of significant bits in N because i is halved at the end of each iteration. Hence the loop iterates log2(N) times for N>0 and none otherwise. Each iteration has 3 simple operations, an addition, a division and a test, so the time complexity is O(log(N)).

Exponentiation complexity

Based on this code:
int poow(int x,int y)
{
if(y==0)
return 1;
if(y%2!= 0)
return poow(x,y-1)*x;
return poow(x,y/2)*poow(x,y/2); //this line
}
I tried to see the complexity: We suppose that we have n=2^k
we have T(0)=1
T(n)=2*T(n/2)+C
T(n)=2^i * T(n/2^i)+i*c
for i=k we have T(n)=2^k * T(n/2^k) + k * c
T(n)=2^k * T(1) + k*c
T(n)=2^k * c2 + k * c
I am stuck here ? How can I continue the computation of complexity and what is the difference when changing this line:
return poow(x,y/2)*poow(x,y/2); //this line
with
int p=poow(x,y/2);
return p*p;
in term of complexity !
Start off with a proper recurrence. The complexity is solely based on y, so we can write the recurrence as
T(0) = 1
T(y) = y is even: 2 * T(y / 2)
y is odd: T(y - 1) + 1
Worst-case would be that every division by 2 leaves us with a odd number, which would lead to the complexity of
T(2^n-1) = 1 + 2 * (1 + 2 * (1 + 2 * ( ... * T(1)))) =
= 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + 2 ^ 3 + ... + 2 ^ (n - 1) + 2 ^ (n - 1) =
= 2 ^ n - 1 + 2 ^(n - 1) = 3 * 2 ^ (n - 1) - 1
T(y) = O(y)
Best-case would be a power of 2:
T(2^n) = 2 * 2 * ... * 2 * T(1) = 2 ^ n * (1 + 1) = 2 ^ (n + 1) = 2 * 2 ^ n
T(y) = O(y)
Now what if we optimized the whole function?
T'(0) = 1
T'(y) = y is even: T(y / 2) + 1
y is odd: T(y - 1) + 1
Worst case:
T'(2^n - 1) = T(2^n - 2) + 1 = T(2^(n - 1) - 1) + 1 + 1 = ... =
= T(1) + 1 + 1 + 1 + ... =
= 2 + 1 + 1 + 1 + ... =
= 1 + ln(2^n) / ln(2) * 2 =
= 1 + 2 * n
T'(y) = O(log y)
Best case:
T'(2 ^ n) = T(1) + 1 + 1 + ... =
= 2 + 1 + 1 + ... =
= 2 + ln(2^n) / ln(2)
= n + 2
T'(y) = O(log y)
So the optimized version is definitely faster (linear vs logarithmic complexity).
Sometime code analysis focuses on the theory and not the actual.
Code has a bug.
if(y%2!= 0)
return poow(x,y-1)*x;
// should be
if(y%2!= 0)
return poow(x,y-y%2)*x;
// better alternative, use
unsigned poow(unsigned x, unsigned y)
Without the fix, calling poow(1,-1) causes infinite recursion and likely stack-overflow. So O(∞) regardless in the last line is return poow(x,y/2)*poow(x,y/2); or int p=poow(x,y/2);
return p*p;.

Given 3 sorted arrays A, B, C and number S, find i, j, k such that A[i] + B[j] + C[k] = S

I've just found out how to solve this in O(n^2 log n) time (assuming each array has the same length):
for each A[i]:
for each B[j]:
if A[i] + B[j] + C.binarySearch(S - A[i] - B[j]) == S:
return (i, j, k)
Is there any way to solve this in O(n^2) time or to improve the above algorithm?
The algorithm you have ain't bad. Relative to n^2, log(n) grows so slowly that it can practically be considered a constant. For example, for n = 1000000, n^2 = 1000000000000 and log(n) = 20. Once n becomes large enough for log(n) to have any significant influence, n^2 will already be so big that the result cannot be computed anyway.
A solution, inspired by #YvesDaoust, though I'm not sure if it's exactly the same:
For every A[i], calculate the remainder R = S - A[i] which should be a combination of some B[j] and C[k];
Let j = 0 and k = |C|-1 (the last index in C);
If B[j] + C[k] < R, increase j;
If B[j] + C[k] > R, decrease k;
Repeat the two previous steps until B[j] + C[k] = R or j >= |B| or k < 0
I suggest not to complicate the algorithm too much with micro-optimizations. For any reasonably small set of numbers and it will be fast enough. If the arrays become too large for this approach, your problem would make a good candidate for Machine Learning approaches such as Hill Climbing.
if the arrays are of non-negatives
* you can trim all 3 arrays to at S => A[n] > S
* similarly, dont bother checking array C if A[aIdx] + B[bIdx] > S
prepare:
sort each array ascending +O(N.log(n))
implement binary search on each array ?O(log(N))
compute:
i=bin_search(smallest i that A[i]+B[0]+C[0]>=S); for (;i<Na;i++) { if (A[i]+B[0]+C[0]>S) break;
j=bin_search(smallest j that A[i]+B[j]+C[0]>=S); for (;j<Nb;j++) { if (A[i]+B[j]+C[0]>S) break;
ss=S-A[i]-B[j];
if (ss<0) break;
k=bin_search(ss);
if (k found) return; // found solution is: i,j,k
}
}
if I see it right and: N=max(Na,Nb,Nc), M=max(valid intervals A,B,C) ... M<=N
it is (3*N.log(N)+log(N)+M*log(N)*M*log(N)) -> O((M^2)*log(N))
the j binary search can be called just once and then iterate +1 if needed
the complexity is the same but the N has changed
for average conditions is this much much faster because M<<N
The O(N²) solution is very simple.
First consider the case of two arrays, finding A[i] + B[j] = S'.
This can be rewritten as A[i] = S' - B[j] = B'[j]: you need to find equal values in two sorted arrays. This is readily done in linear time with a merging process. (You can explicitly compute the array B' but this is unnecessary, just do it on the fly: instead of fetching B'[j], get S' - B[NB-1-j]).
Having established this procedure, it suffices to use it for all elements of C, in search of S - C[k].
Here is Python code that does that and reports all solutions. (It has been rewritten to be compact and symmetric.)
for k in range(NC):
# Find S - C[k] in top-to-tail merged A and B
i, j= 0, NB - 1
while i < NA and 0 <= j:
if A[i] + B[j] + C[k] < S:
# Move forward A
i+= 1
elif A[i] + B[j] + C[k] > S:
# Move back B
j-= 1
else:
# Found
print A[i] + B[j] + C[k], "=", A[i], "+", B[j], "+", C[k]
i+= 1; j-= 1
Execution with
A= [1, 2, 3, 4, 5, 6, 7]; NA= len(A)
B= [2, 3, 5, 7, 11]; NB= len(B)
C= [1, 1, 2, 3, 5, 7]; NC= len(C)
S= 15
gives
15 = 3 + 11 + 1
15 = 7 + 7 + 1
15 = 3 + 11 + 1
15 = 7 + 7 + 1
15 = 2 + 11 + 2
15 = 6 + 7 + 2
15 = 1 + 11 + 3
15 = 5 + 7 + 3
15 = 7 + 5 + 3
15 = 3 + 7 + 5
15 = 5 + 5 + 5
15 = 7 + 3 + 5
15 = 1 + 7 + 7
15 = 3 + 5 + 7
15 = 5 + 3 + 7
15 = 6 + 2 + 7

What is the running time complexity of the following piece of code?

void foo(int n){
int i = 1, sum = 1;
while (sum <= n) {
i++;
sum+=i;
}
}
What I feel is, the loop will terminate only if the sum becomes greater than the argument n.
And, Sum at jth iteration is: S(j) = S(j-1) + j
S(1) = S(0) + 1
S(2) = S(1) + 2
S(3) = S(2) + 3
...
S(n) = S(n-1) + n
How should I go further? I am stuck at this recurrence relation.
The loop will terminate when 1 + 2 + 3 + ... + j times becomes greater than n. But I am not convinced, if it is okay.
The classic way to prove this is to write the series out twice, in both orders ie:
S(n) = 1 + 2 + 3 + ...+ n-2 + n-1 + n
S(n) = n + (n-1) + (n-2) + ...+ 3 + 2 + 1
If you sum those term by term you get
2S(n)= n+1 + n+1 + n+1 + ... + n+1
With n terms
therefore
S(n) = n*(n+1)/2
(A result allegedly discovered by Gauss as a schoolboy)
From this it follows that it takes O(sqrt(n)) iterations.
You have almost done it. The last thing you have to note is that 1 + 2 + 3 + ... + j is (1 + j) * j / 2, or O(j^2). This is a well-known formula from math (arithmetic progression).
It will break after k iterations when
1 + 2 + .... + k > n
k*(k+1)/2 > n
k*k + k - 2*n >0
k will come to k = (-1 + sqrt(1+8n))/2 (discarding negative value)
Hence, Time complexity is sqrt(n).

Nested loops result

I really don't know how to find out the result of nested loops.
For example in the following pseudo-code, I can't sort out what will be given at the end of execution. I'll be so glad if anyone gives me a simple solution.
r <- 0
for i <- 1 to n do
for j <- 1 to i do
for k <- j to i+j do
r <- r + 1
return r
Question is:
What is the result of the code and give the result r in terms of n?
I write it but every time I get confused.
In your pseudo-code, Inner most loop, k <- j to i+j can be written as k <- 0 to i (this is by removing j). Hence your code can be simplified as follows:
r <- 0
for i <- 1 to n do
for j <- 1 to i do
for k <- 0 to i do // notice here `j` removed
r <- r + 1
return r
Based on this pseudo-code, I have written a C program(as below) to generate sequence for N = 1 to 10. (you originally tagged question as java but I am writing c code because what you wants is independent of language constraints)
#include<stdio.h>
int main(){
int i =0, k =0, j =0, n =0;
int N =0;
int r =0;
N =10;
for (n=1; n <= N; n++){
// unindented code here
r =0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=0; k<=i; k++)
r++;
printf("\n N=%d result = %d",n, r);
}
printf("\n");
}
Output of this program is something like:
$ ./a.out
N=1 result = 2
N=2 result = 8
N=3 result = 20
N=4 result = 40
N=5 result = 70
N=6 result = 112
N=7 result = 168
N=8 result = 240
N=9 result = 330
N=10 result = 440
Then, Tried to explore, How does it work? with some diagrams:
Execution Tree For N=1:
1<=i<=1, (i=1)
|
1<=j<=i, (j=1)
/ \
0<=k<=i, (K=0) (K=1)
| |
r=0 r++ r++ => r = 2
( 1 + 1 )
That is (1*2) = 2
Tree For N=2:
1<=i<=2, (i=1)-----------------------(i=2)
| |---------|------|
1<=j<=i, (j=1) (j=1) (j=2)
/ \ / | \ / | \
0<=k<=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2)
| | | | | | | |
r=0 r++ r++ r++ r++ r++ r++ r++ r++ => 8
-------------- ---------------------------------
( 1 + 1) ( 3 + 3 )
That is (1 + 1) + (3 + 3) = 8
Similarly I drawn a tree for N=3:
1<=i<=3, (i=1)-----------------------(i=2)--------------------------------------------(i=3)
| |---------|------| |----------------------|----------------------|
1<=j<=3, (j=1) (j=1) (j=2) ( j=1 ) ( j=2 ) ( j=3 )
/ \ / | \ / | \ / | | \ / | | \ / | | \
0<=k<=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2) / | | \ / | | \ / | | \
| | | | | | | | (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3)
r=0 r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++
That is (1 + 1) + (3 + 3) + (4 + 4+ 4)= 20
N = 1, (1 + 1) = 2
N = 2, (1 + 1) + (3 + 3) = 8
N = 3, (1 + 1) + (3 + 3) + (4 + 4 + 4)= 20
N = 4, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) = 40
N = 5, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) = 70
N = 6, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) + (7 + 7 + 7 + 7 + 7 + 7)= 112
For N=6 we can also be write above sequence as:
(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + (6*7)
Finally, I could understand that sum of N in three loop is:
(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + ... + (N * (N+1))
With help from math.stackexchange.com, I could simplify this equation:
I asked here: How to simplify summation equation in terms of N?
So as I commented to your question, Result in term of N is ( ((N) * (N+1) * (N+2)) / 3 ).
And, I think its correct. I cross checked it as follows:
N = 1, (1 * 2 * 3)/3 = 2
N = 2, (2 * 3 * 4)/3 = 8
N = 3, (3 * 4 * 5)/3 = 20
N = 4, (4 * 5 * 6)/3 = 40
N = 5, (5 * 6 * 7)/3 = 70
Try using some code like this to work it out... i.e. code up what it is and what you think it should be and test it.
EDIT: updated based on comment above.
public class CountLoop{
public static void main(String[] args){
for(int i=1;i<=10;i++)
System.out.println("It's "+run(i)+" and I think "+guess(i));;
}
public static int run(int n){
int r = 0;
for(int i=1;i<=n;i++)
for(int j=1; j <= i;j++)
for(int k=j; k <= i+j; k++)
r += 1;
return r;
}
public static int guess(int n){
// taken from the comments
int r = ((n * (n+1) * (n+2)) /3);
return r;
}
}
Running this gets
It's 2 and I think 2
It's 8 and I think 8
It's 20 and I think 20
It's 40 and I think 40
It's 70 and I think 70
It's 112 and I think 112
It's 168 and I think 168
It's 240 and I think 240
It's 330 and I think 330
It's 440 and I think 440
so we're happy.
I am getting it something like this :
n = 1: r = 2
n = 2: r = 8
n = 3: r = 20
n = 4: r = 40
n = 5: r = 70
n = 6: r = 112
n = 7: r = 168
n = 8: r = 240
n = 9: r = 330
n = 10: r = 440
lets say for n = 10,
r = 2 + 6 + 12 + 20 + 30 + 42 + 56 + 72 + 90 + 110 = 440
=> r = 2(1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55)
Intuitively, I think
n = sum(n-1) + n * (n + 1).
where
sum(n-1) = value of r for n-1

Resources