Miscellaneous Problems of Time Complexity - GeeksforGeeks

16 Feb, 2022
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++
C
Java
Python3
C#
Javascript
void function(int n){    int i = 1, s = 1;    while (s < n) {        s = s + i;        i++;    }} // This code is contributed by Shubham Singh
void function(int n){    int i = 1, s = 1;    while (s < n) {        s = s + i;        i++;    }}
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
def function(n):    i = 1    s = 1    while (s < n):        s = s + i        i+=1     # This code is contributed by Shubham Singh
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
<script>function functionn(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 
 
 
 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 = √nHence, the time complexity is O(√n).
 
2. What is the time complexity of the following code : 
 
C++
C
Java
Python3
C#
Javascript
void fun(int n){    if (n < 5)        cout << "GeeksforGeeks";    else {        for (int i = 0; i < n; i++) {            cout << i;        }    }} // This code is contributed by Shubham Singh
void fun(int n){    if (n < 5)        printf("GeeksforGeeks");    else {        for (int i = 0; i < n; i++) {            printf("%d", i);        }    }}
public static void fun(int n){    if (n < 5)        System.out.print("GeeksforGeeks");    else {        for (int i = 0; i < n; i++) {            System.out.print(i + " ");        }    }} // This code is contributed by Shubham Singh
def fun(n):    if (n < 5):        print("GeeksforGeeks", end ="")    else:        for i in range(n):            print(i, end= " ")             # This code is contributed by Shubham Singh
public static void fun(int n){    if (n < 5)        Console.Write("GeeksforGeeks");    else {        for (int i = 0; i < n; i++) {            Console.Write(i + " ");        }    }} // This code is contributed by Shubham Singh
<script>function fun(n){    if (n < 5)        document.write("GeeksforGeeks");    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 GeeksforGeeks 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++
C
Java
Python3
C#
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
void fun(int a, int b){    while (a != b) {        if (a > b)            a = a - b;        else            b = b - a;    }}
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
def fun(a, b):         while (a != b):        if (a > b):            a = a - b        else:            b = b - a             # This code is contributed by Shubham Singh
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
  
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;
 
 
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++
C
void fun(int n){  for(int i=0;i*i<n;i++)    cout<<"GeeksforGeeks";} // This code is contributed by Shubham Singh
void fun(int n){  for(int i=0;i*i<n;i++)    printf("%s","GeeksforGeeks");}
Solution – Time complexity = O(√n).
Explanation –Let k be the no. of iteration of the loop.
⇒ The loop will stop when i * i >=n       i.e.,  i*i=n⇒ i*i=n ⇒ k2 = n⇒k =√nHence, the time complexity is O(√n).
5. What is the time complexity of the following code : 
C++
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 << "GeeksforGeeks";} //This code is contributed by Shubham Singh
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("GeeksforGeeks");}
  
Solution – Time complexity = O(logxn).
 
Explanation – Let k be the no. of iteration of the loop.
 
 
⇒ 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
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 << "GeeksforGeeks";}
  
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 << "GeeksforGeeks";}
  
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 << "GeeksforGeeks";}
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++
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
void fun(int n){    int i = 1;    while (i < n) {        int j = n;        while (j > 0) {            j = j / 2;        }        i = i * 2;    }}
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 C-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;    }}
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.
anuragkumar8
SHUBHAMSINGH10
surinderdawra388
time complexity
Analysis
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
3-coloring is NP Complete
Proof that Dominant Set of a Graph is NP-Complete
Difference between Deterministic and Non-deterministic Algorithms
Set partition is NP complete
Pseudo-polynomial Algorithms
Complexity analysis of various operations of Binary Min Heap
Proof that Independent Set in Graph theory is NP Complete
Applications of Hashing
Time Complexity Analysis | Tower Of Hanoi (Recursion)