An interesting time complexity question Last Updated : 30 Oct, 2015 Comments Improve Suggest changes Like Article Like Report What is the time complexity of following function fun()? C int fun(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j < 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, the inner loop is executed approximately n/4 times. ............................................................. ............................................................. For i = n, the inner loop is executed approximately n/n times. So the total time complexity of the above algorithm is (n + n/2 + n/3 + ... + n/n) Which becomes n * (1/1 + 1/2 + 1/3 + ... + 1/n) The important thing about series (1/1 + 1/2 + 1/3 + ... + 1/n) is, it is equal to Θ(Logn) (See this for reference). So the time complexity of the above code is Θ(nLogn). As a side note, the sum of infinite harmonic series is counterintuitive as the series diverges. The value of is ∞. This is unlike geometric series as geometric series with ratio less than 1 converges. Reference: http://en.wikipedia.org/wiki/Harmonic_series_%28mathematics%29#Rate_of_divergence http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap03.htm Comment More infoAdvertise with us Next Article An interesting time complexity question R Rahul Improve Article Tags : DSA Analysis of Algorithms Similar Reads A Time Complexity Question 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() { in 2 min read Practice Questions on Time Complexity Analysis Prerequisite: Analysis of Algorithms1. What is the time, and space complexity of the following code: CPPint a = 0, b = 0; for (i = 0; i < N; i++) { a = a + rand(); } for (j = 0; j < M; j++) { b = b + rand(); } Javaint a = 0, b = 0; for (i = 0; i < N; i++) { a = a + Math.random(); } for (j = 7 min read Time and Space Complexity of Linked List A linked list is a fundamental data structure in computer science and programming. It is a collection of nodes where each node contains a data field and a reference (link) to the next node in the sequence. The last node in the list points to null, indicating the end of the list. Knowing the time and 4 min read Step Count Method for Time Complexity Analysis What is Time Complexity? Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm. Time Complexity can be calculated by using Two types of methods. They are: Step Count MethodAsymptotic Notation. Here, we will d 4 min read Time Complexity and Space Complexity Many times there are more than one ways to solve a problem with different algorithms and we need a way to compare multiple ways. Also, there are situations where we would like to know how much time and resources an algorithm might take when implemented. To measure performance of algorithms, we typic 13 min read Time and Space Complexity of Ternary Search The time complexity of Ternary Search is O(log3 N), where N is the size of the array. In terms of space complexity, ternary search requires only O(1) auxiliary space, as it operates directly on the given array without creating any additional data structures. Feature Ternary Search Time Complexity O( 2 min read How to analyse Complexity of Recurrence Relation The analysis of the complexity of a recurrence relation involves finding the asymptotic upper bound on the running time of a recursive algorithm. This is usually done by finding a closed-form expression for the number of operations performed by the algorithm as a function of the input size, and then 7 min read Time and Space Complexity Analysis of Merge Sort The Time Complexity of Merge Sort is O(n log n) in both the average and worst cases. The space complexity of Merge sort is O(n). AspectComplexityTime ComplexityO(n log n)Space ComplexityO(n)Time Complexity Analysis of Merge Sort:Consider the following terminologies: T(k) = time taken to sort k eleme 2 min read Time and Space complexity analysis of Selection Sort The Selection sort algorithm has a time complexity of O(n^2) and a space complexity of O(1) since it does not require any additional memory space apart from a temporary variable used for swapping. Time Complexity Analysis of Selection Sort:Best-case: O(n2), best case occurs when the array is already 2 min read Akra-Bazzi method for finding the time complexities Master's theorem is a popular method to solve time complexity recurrences of the form:T(n) = aT(\frac{n}{b}) + f(n) With constraints over a, b and f(n). The recurrence relation form limits the usability of the Master's theorem. Following are three recurrences that cannot be solved directly using mas 3 min read Like