Open In App

A Time Complexity Question

Last Updated : 27 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2. 

C++




void fun()
{
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= log(i); j++)
            cout << "GeeksforGeeks";
}
 
// This code is contributed by SHUBHAMSINGH10.


C




void fun()
{
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= log(i); j++)
            printf("GeeksforGeeks");
}


Java




static void fun()
{
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= log(i); j++)
            System.out.printf("GeeksforGeeks");
}
 
// This code is contributed by umadevi9616


Python3




import math
def fun():
    i = 0
    j = 0
    for i in range(1, n + 1):
        for j in range(1,math.log(i) + 1):
            print("GeeksforGeeks")
 
# This code is contributed by SHUBHAMSINGH10.


C#




static void fun()
{
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= log(i); j++)
            Console.Write("GeeksforGeeks");
}
 
// This code is contributed by umadevi9616


Javascript




const fun()
{
    let i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= Math.log(i); j++)
            document.write("GeeksforGeeks");
}
 
// This code is contributed by SHUBHAMSINGH10.


Time Complexity of the above function can be written as ?(log 1) + ?(log 2) + ?(log 3) + . . . . + ?(log n) which is ?(log n!)
Order of growth of ‘log n!’ and ‘n log n’ is same for large values of n, i.e., ?(log n!) = ?(n log n). So time complexity of fun() is ?(n log n).
The expression ?(log n!) = ?(n log n) can be easily derived from following Stirling’s approximation (or Stirling’s formula)

log n! = n*log n - n = O(n*log(n)) 

Sources: 
http://en.wikipedia.org/wiki/Stirling%27s_approximation



Similar Reads

Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
An interesting time complexity question
What is the time complexity of following function fun()? int fun(int n) { for (int i = 1; i &lt;= n; i++) { for (int j = 1; j &lt; n; j += i) { // Some O(1) task } } } For i = 1, the inner loop is executed n times. For i = 2, the inner loop is executed approximately n/2 times. For i = 3, the inner loop is executed approximately n/3 times. For i = 4
2 min read
An Insertion Sort time complexity question
Question : How much time Insertion sort takes to sort an array of size n in below form? arr[] = 2, 1, 4, 3, 6, 5,....i, i-1, .....n, n-1 Answer : At first look, it seems like Insertion Sort would take O(n2) time, but it actually takes O(n) time How? Let us take a closer look at below code. /* Function to sort an array using insertion sort*/ void in
2 min read
Comparison of an Array and Hash table in terms of Storage structure and Access time complexity
Arrays and Hash Tables are two of the most widely used data structures in computer science, both serving as efficient solutions for storing and accessing data in Java. They have different storage structures and time complexities, making them suitable for different use cases. In this article, we will explore the differences between arrays and hash t
3 min read
Time Complexity Analysis | Tower Of Hanoi (Recursion)
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk ca
2 min read
Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
Given a string str containing characters '(', ')', '{', '}', '[' and ']', the task is to determine if brackets are balanced or not. Brackets are balanced if: Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order. Examples: Input: str = "(())[]" Output: Yes Input: str = "))(({}{" Output: No Appro
10 min read
Queries of nCr%p in O(1) time complexity
Given Q queries and P where P is a prime number, each query has two numbers N and R and the task is to calculate nCr mod p. Constraints: N &lt;= 106 R &lt;= 106 p is a prime number Examples: Input: Q = 2 p = 1000000007 1st query: N = 15, R = 4 2nd query: N = 20, R = 3 Output: 1st query: 1365 2nd query: 1140 15!/(4!*(15-4)!)%1000000007 = 1365 20!/(2
9 min read
Python Code for time Complexity plot of Heap Sort
Prerequisite : HeapSort Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element. We implement Heap Sort here, call it for different sized random lists, meas
3 min read
C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
Prerequisite:Comparison among bubble sort, insertion sort and selection sort. Write a C program to plot and analyze the time complexity of Bubble sort, Insertion sort and Selection sort (using Gnuplot). As per the problem we have to plot a time complexity graph by just using C. So we will be making sorting algorithms as functions and all the algori
5 min read
How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
Pre-requisite: Sieve of Eratosthenes What is Sieve of Eratosthenes algorithm? In order to analyze it, let's take a number n and the task is to print the prime numbers less than n. Therefore, by definition of Sieve of Eratosthenes, for every prime number, it has to check the multiples of the prime and mark it as composite. This process continues unt
3 min read
Article Tags :