Miscellaneous Problems of Time Complexity
Prerequisite : Asymptotic Notations
Time Complexity :
Time complexity is the time needed by an algorithm expressed as a function of the size of a problem. It can also be defined as the amount of computer time it needs to run a program to completion. When we solve a problem of time complexity then this definition help the most β
βIt is the number of operations an algorithm performs to complete its task with respect to the input size.β
There are following some miscellaneous problems of time complexity which are always frequently asking in different types of quizzes.
1. What is the time complexity of the following code β
C++
void function( int n) { int i = 1, s = 1; while (s < n) { s = s + i; i++; } } // This code is contributed by Shubham Singh |
C
void function( int n) { int i = 1, s = 1; while (s < n) { s = s + i; i++; } } |
Java
public static void function( int n) { int i = 1 , s = 1 ; while (s < n) { s = s + i; i++; } } // This code is contributed by Shubham Singh |
Python3
def function(n): i = 1 s = 1 while (s < n): s = s + i i + = 1 # This code is contributed by Shubham Singh |
C#
public static void function( int n) { int i = 1, s = 1; while (s < n) { s = s + i; i++; } } // This code is contributed by Shubham Singh |
Javascript
<script> function function (int n) { var i = 1; var s = 1; while (s < n) { s = s + i; i++; } } // This code is contributed by Shubham Singh </script> |
Solution β
Time complexity = O(βn).
Explanation β
We can define the βSβ terms according to the relation Si = Si-1 + i. Let k is the total number of iterations taken by the program
i | S |
1 |
1 |
2 |
2 |
3 |
2 + 2 |
4 |
2 + 2 + 3 |
β¦ |
β¦ |
k |
2 + 2 + 3 + 4 + β¦β¦+ k |
When S>=n , then loop will stop at kth iterations,
β S>=n β S=n
β 2 + 2 + 3 + 4 + β¦β¦+ k = n
β 1 + (k * (k + 1))/2 = n
β k2 = n
k = βn
Hence, the time complexity is O(βn).
2. What is the time complexity of the following code :
C++
void fun( int n) { if (n < 5) cout << "w3wiki" ; else { for ( int i = 0; i < n; i++) { cout << i; } } } // This code is contributed by Shubham Singh |
C
void fun( int n) { if (n < 5) printf ( "w3wiki" ); else { for ( int i = 0; i < n; i++) { printf ( "%d" , i); } } } |
Java
public static void fun( int n) { if (n < 5 ) System.out.print( "w3wiki" ); else { for ( int i = 0 ; i < n; i++) { System.out.print(i + " " ); } } } // This code is contributed by Shubham Singh |
Python3
def fun(n): if (n < 5 ): print ( "w3wiki" , end = "") else : for i in range (n): print (i, end = " " ) # This code is contributed by Shubham Singh |
C#
public static void fun( int n) { if (n < 5) Console.Write( "w3wiki" ); else { for ( int i = 0; i < n; i++) { Console.Write(i + " " ); } } } // This code is contributed by Shubham Singh |
Javascript
<script> function fun(n) { if (n < 5) document.write( "w3wiki" ); else { for ( var i = 0; i < n; i++) { document.write(i + " " ); } } } //This code is contributed by Shubham Singh </script> |
Solution β
Time complexity = O(1) in best case and O(n) in worst case.
Explanation β
This program contains if and else condition. Hence, there are 2 possibilities of time complexity. If the value of n is less than 5, then we get only w3wiki as output and its time complexity will be O(1).
But, if n>=5, then for loop will execute and time complexity becomes O(n), it is considered as worst case because it takes more time.
3. What is the time complexity of the following code :
C++
void fun( int a, int b) { // Consider a and b both are positive integers while (a != b) { if (a > b) a = a - b; else b = b - a; } } // This code is contributed by Shubham Singh |
C
void fun( int a, int b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } } |
Java
public static void fun( int a, int b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } } // This code is contributed by Shubham Singh |
Python3
def fun(a, b): while (a ! = b): if (a > b): a = a - b else : b = b - a # This code is contributed by Shubham Singh |
C#
public static void fun( int a, int b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } } // This code is contributed by Shubham Singh |
Javascript
<script> function fun(a, b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } } // This code is contributed by Shubham Singh </script> |
Solution β
Time complexity = O(1) in best case and O(max(a, b)) worst case.
Explanation β
If the value of a and b are the same, then while loop will not be executed. Hence, time complexity will be O(1).
But if a!=b, then the while loop will be executed. Let a=16 and b=5;
no. of iterations | a | b |
1 |
16 |
5 |
2 |
16-5=11 |
5 |
3 |
11-5=6 |
5 |
4 |
6-5=1 |
5 |
5 |
1 |
5-1=4 |
6 |
1 |
4-1=3 |
7 |
1 |
3-1=2 |
8 |
1 |
2-1=1 |
For this case, while loop executed 8 times (a/2β16/2β8).
If a=5 and b=16, then also the loop will be executed 8 times. So we can say that time complexity is O(max(a/2,b/2))βO(max(a, b)), it is considered as worst case because it takes more time.
4. What is the time complexity of the following code :
C++
void fun( int n) { for ( int i=0;i*i<n;i++) cout<< "w3wiki" ; } // This code is contributed by Shubham Singh |
C
void fun( int n) { for ( int i=0;i*i<n;i++) printf ( "%s" , "w3wiki" ); } |
Java
public static void fun( int n) { for ( int i= 0 ;i*i<n;i++) System.out.print( "w3wiki" ); } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n): i = 0 while i * i < n: print ( "w3wiki" ) i + = 1 # This code is contributed by Shubham Singh and edited by Pawan |
C#
public static void fun( int n) { for ( int i=0;i*i<n;i++) Console.Write( "w3wiki" ); } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { for (let i = 0; i*i < n; i++) console.log( "w3wiki" ); } // This code is contributed by Utkarsh. |
Solution β
Time complexity = O(βn).
Explanation β
Let k be the no. of iteration of the loop.
i |
i*i |
1 |
1 |
2 |
2 2 |
3 |
3 2 |
4 |
4 2 |
β¦ |
β¦ |
k |
k 2 |
β The loop will stop when i * i >=n i.e., i*i=n
β i*i=n β k2 = n
βk =βn
Hence, the time complexity is O(βn).
5. What is the time complexity of the following code :
C++
void fun( int n, int x) { for ( int i = 1; i < n; i = i * x) //or for(int i = n; i >=1; i = i / x) cout << "w3wiki" ; } //This code is contributed by Shubham Singh |
C
void fun( int n, int x) { for ( int i = 1; i < n; i = i * x) //or for(int i = n; i >=1; i = i / x) print( "w3wiki" ); } |
Java
public static void fun( int n, int x) { for ( int i = 1 ; i < n; i = i * x) //or for(int i = n; i >=1; i = i / x) System.out.print( "w3wiki" ); } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n, x): for i in range ( 1 , n, i * x): print ( "w3wiki" ) |
C#
public static void fun( int n, int x) { for ( int i = 1; i < n; i = i * x) //or for(int i = n; i >=1; i = i / x) Console.Write( "w3wiki" ); } // This code is contributed by Aman Kumar |
Javascript
function fun(n, x) { for (let i = 1; i < n; i = i * x) //or for(let i = n; i >=1; i = i / x) document.write( "w3wiki" ); } // This code is contributed by Utkarsh |
Solution β
Time complexity = O(logxn).
Explanation β
Let k be the no. of iteration of the loop.
no. of itr | i=i*x |
1 |
1*x=x |
2 |
x*x=x 2 |
3 |
x 2 *x=x 3 |
β¦ |
β¦ |
k |
x k-1 *x= x k |
β The loop will stop when i>=n β xk = n
β xk=n (Take log both sides)
β k=logxn
βHence, time complexity is O(logxn).
6. What is the time complexity of the following code :
C++
#include <iostream> using namespace std; void fun( int n) { for ( int i = 0; i < n / 2; i++) for ( int j = 1; j + n / 2 <= n; j++) for ( int k = 1; k <= n; k = k * 2) cout << "w3wiki" ; } int main() { int n=8; fun(3); } // The code is contributed by Nidhi goel. |
C
void fun( int n) { for ( int i = 0; i < n / 2; i++) for ( int j = 1; j + n / 2 <= n; j++) for ( int k = 1; k <= n; k = k * 2) printf ( "w3wiki" ); } int main() { int n=8; fun(3); } |
Java
public static void fun( int n) { for ( int i = 0 ; i < n / 2 ; i++) for ( int j = 1 ; j + n / 2 <= n; j++) for ( int k = 1 ; k <= n; k = k * 2 ) System.out.print( "w3wiki" ); } // This code is contributed by Pushpesh Raj. |
Python3
import math; def fun(n): for i in range ( 0 ,math.floor(n / 2 )): for j in range ( 1 ,n - math.floor(n / 2 ) + 1 ): k = 1 ; for k in range ( 1 ,n + 1 , 2 * k): print ( "w3wiki" ); |
C#
public static void fun( int n) { for ( int i = 0; i < n / 2; i++) for ( int j = 1; j + n / 2 <= n; j++) for ( int k = 1; k <= n; k = k * 2) Console.Write( "w3wiki" ); } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { for (let i = 0; i < Math.floor(n / 2); i++) for (let j = 1; j + Math.floor(n / 2) <= n; j++) for (let k = 1; k <= n; k = k * 2) console.log( "w3wiki" ); } // This code is contributed by Utkarsh |
Solution β
Time complexity = O(n2log2n).
Explanation β
Time complexity of 1st for loop = O(n/2) β O(n).
Time complexity of 2nd for loop = O(n/2) β O(n).
Time complexity of 3rd for loop = O(log2n). (refer question number β 5)
Hence, the time complexity of function will become O(n2log2n).
7. What is the time complexity of the following code :
C++
void fun( int n) { for ( int i = 1; i <= n; i++) for ( int j = 1; j <= n; j = j + i) cout << "w3wiki" ; } //This Code is contributed by Shubham Singh |
C
void fun( int n) { for ( int i = 1; i <= n; i++) for ( int j = 1; j <= n; j = j + i) print( "w3wiki" ); } |
Java
public static void fun( int n) { for ( int i = 1 ; i <= n; i++) for ( int j = 1 ; j <= n; j = j + i) System.out.print( "w3wiki" ); } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n): for i in range ( 1 ,n + 1 ): for j in range ( 1 ,n + 1 ,i): print ( "w3wiki" ); |
C#
public static void fun( int n) { for ( int i = 1; i <= n; i++) for ( int j = 1; j <= n; j = j + i) Console.Write( "w3wiki" ); } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { for (let i = 1; i <= n; i++) for (let j = 1; j <= n; j = j + i) console.log( "w3wiki" ); } fun(7) // This code is contributed by Utkarsh |
Solution β Time complexity = O(nlogn).
Explanation β
Time complexity of 1st for loop = O(n). 2nd loop executes n/i times for each value of i.
Its time complexity is O(βni=1 n/i) β O(logn).
Hence, time complexity of function = O(nlogn).
8. What is the time complexity of the following code :
C++
void fun( int n) { for ( int i = 0; i <= n / 3; i++) for ( int j = 1; j <= n; j = j + 4) cout << "w3wiki" ; } // The code is contributed by Arushi Jindal. |
C
void fun( int n) { for ( int i = 0; i <= n / 3; i++) for ( int j = 1; j <= n; j = j + 4) cout << "w3wiki" ; } |
Java
public static void fun( int n) { for ( int i = 0 ; i <= n / 3 ; i++) for ( int j = 1 ; j <= n; j = j + 4 ) System.out.print( "w3wiki" ); } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n): for i in range (n / / 3 + 1 ): for j in range ( 1 , n + 1 , 4 ): print ( "w3wiki" ) # the code is contributed by Arushi Jindal. |
C#
public static void fun( int n) { for ( int i = 0; i <= n / 3; i++) for ( int j = 1; j <= n; j = j + 4) Console.Write( "w3wiki" ); } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { for (let i = 0; i <= Math.floor(n / 3); i++) for (let j = 1; j <= n; j = j + 4) document.write( "w3wiki" ); } // This code is contributed by Utkarsh |
Solution β Time complexity = O(n2).
Explanation β
Time complexity of 1st for loop = O(n/3) β O(n).
Time complexity of 2nd for loop = O(n/4) β O(n).
Hence, the time complexity of function will become O(n2).
9. What is the time complexity of the following code :
C++
void fun( int n) { int i = 1; while (i < n) { int j = n; while (j > 0) { j = j / 2; } i = i * 2; } } //This code is contributed by Shubham Singh |
C
void fun( int n) { int i = 1; while (i < n) { int j = n; while (j > 0) { j = j / 2; } i = i * 2; } } |
Java
public static void fun( int n) { int i = 1 ; while (i < n) { int j = n; while (j > 0 ) { j = j / 2 ; } i = i * 2 ; } } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n): i = 1 while (i < n): j = n while (j > 0 ): j = j / / 2 i = i * 2 # This code is contributed by Arushi Jindal. |
C#
public static void fun( int n) { int i = 1; while (i < n) { int j = n; while (j > 0) { j = j / 2; } i = i * 2; } } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { let i = 1; while (i < n) { let j = n; while (j > 0) { j = Math.floor(j / 2); } i = i * 2; } } // This code is contributed by Utkarsh |
Solution β Time complexity = O(log2n).
Explanation β
In each iteration , i become twice (T.C=O(logn)) and j become half (T.C=O(logn)). So, time complexity will become O(log2n).
We can convert this while loop into for loop.
for( int i = 1; i < n; i = i * 2)
for( int j=n ; j > 0; j = j / 2).
Time complexity of above for loop is also O(log2n).
10. Consider the following code, what is the number of comparisons made in the execution of the loop ?
C++
void fun( int n) { int j = 1; while (j <= n) { j = j * 2; } } // The code is contributed by Arushi Jindal. |
C
void fun( int n) { int j = 1; while (j <= n) { j = j * 2; } } |
Java
public static void fun( int n) { int j = 1 ; while (j <= n) { j = j * 2 ; } } // This code is contributed by Pushpesh Raj. |
Python3
def fun(n): j = 1 while (j < = n): j = j * 2 # The code is contributed by Arushi Jindal. |
C#
public static void fun( int n) { int j = 1; while (j <= n) { j = j * 2; } } // This code is contributed by Aman Kumar |
Javascript
function fun(n) { let j = 1; while (j <= n) { j = j * 2; } } // This code is contributed by Utkarsh |
Solution β
ceil(log2n)+1.
Explanation β
Let k be the no. of iteration of the loop. After the kth step, the value of j is 2k. Hence, k=log2n. Here, we use ceil of log2n, because log2n may be in decimal. Since we are doing one more comparison for exiting from the loop.
So, the answer is ceil(log2n)+1.