 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Is the time complexity of this loop O(n*log(n))?

By : Dan Doca
Date : September 16 2020, 01:00 AM
Hope this helps For the first snippet, the inner loop runs log(1) + log(2) + ... + log(n) times, which is the same as log(1 * 2 * ... * n) = log(n!) which through Sterling's Approximation is n log(n). Your second snippet has the same complexity. Even if it happens to do less assignments we only care about the overall behavior. In this case both are linearithmic. code :

## Running time/time complexity for while loop with square root

By : Saverio
Date : March 29 2020, 07:55 AM
it should still fix some issue This question looks relatively simple, but I can't seem to find the running time in terms of n. , Work backwards to get the number of time units for line 2:
code :
``````                                   time
n              n       log_2(n)    units
1              1        0          0
2              2        1          1
4              4        2          2
16             16       4          3
16^2           256      8          4
(16^2)^2       65536    16         5
((16^2)^2)^2)  ...      32         6
``````

## Reduced time complexity of inner loop: Find count of elements greater than current element in the first loop and store t

By : David Scully
Date : March 29 2020, 07:55 AM
I hope this helps . There is actually a O(n*logn) solution, but you should use a self balancing binary search tree such as red-black tree.
Main idea of the algorithm:
code :
``````public class Test
{
static class Node {
public int value, countOfSmaller, sizeOfSubtree;
public Node left, right;
public Node(int val, int count) {
value = val;
countOfSmaller = count;
sizeOfSubtree = 1; /** You always add a new node as a leaf */
System.out.println("For element " + val + " the number of smaller elements to the right is " + count);
}
}
static Node insert(Node node, int value, int countOfSmaller)
{
if (node == null)
return new Node(value, countOfSmaller);

if (value > node.value)
node.right = insert(node.right, value, countOfSmaller + size(node.left) + 1);
else
node.left = insert(node.left, value, countOfSmaller);

node.sizeOfSubtree = size(node.left) + size(node.right) + 1;

/** Here goes the rebalancing part. In case that you plan to use AVL, you will need an additional variable that will keep the height of the subtree.
In case of red-black tree, you will need an additional variable that will indicate whether the node is red or black */

return node;
}
static int size(Node n)
{
return n == null ? 0 : n.sizeOfSubtree;
}

public static void main(String[] args)
{
int[] array = {13, 8, 4, 7, 1, 11};
Node root = insert(null, array[array.length - 1], 0);
for(int i = array.length - 2; i >= 0; i--)
insert(root, array[i], 0); /** When you introduce rebalancing, this should be root = insert(root, array[i], 0); */
}
}
``````

## Time Complexity - While loop divided by 2 with for loop nested

By : DancingBear
Date : March 29 2020, 07:55 AM
seems to work fine External loop executes log(n) times
Internal loop executes n + n/2 + n/4 +..+ 1 ~ 2*n times (geometric progression sum)

## Time Complexity Nested Loop Inner Loop + Outer Loop

By : WofloW
Date : March 29 2020, 07:55 AM
I hope this helps . The printf in the inner loop is called exactly ceil(n) + ceil(n/2) + ceil(n/3) + ... ceil(n/n) times. To get rid of ceil, we know that ceil(y/n) is bounded above by y/n + 1, so we know that the number of executions is >= n + n/2 + n/3 ... n/n but is < n + 1 + n/2 + 1 + n/3 + 1 + n/4 + 1... + n/n + 1. The former can be factored to n(1 + 1/2 + 1/3 + 1/4 ... 1/n) and the latter can be refactored into to n(1 + 1/2 + 1/3 + 1/4 ... 1/n) + n.
The latter factor is of the first addend to infinity is the the harmonic series, which diverges. The sum of the first k terms from the Wikipedia page is known to be bounded:

## Time Complexity of a loop that integer divides the loop counter by a constant

By : maryam sh
Date : March 29 2020, 07:55 AM
help you fix your problem I find it easier to do it the other way around in cases like this. What is the opposite of what you're doing (even approximately)? Something like: Related Posts : 