Complexity of Loops
The time complexity of a function is O(1) if does not contain a loop, recursive function, or any other non-constant time function. The time complexity of a loop is also O(1) if it has a constant no of iterations Recursive function time complexity is also O(1) if it runs a constant no of times. Linear Loops: The time complexity of a loop is O(n) which is linear. If the loop variable is incremented or decremented by constant Nested Loops: 1. The time complexity of nested loops is equal to the number of times the innermost statement is executed. For example, the following sample loop has O(n 2 ) time complexity: for (int i = 1; i <=n; i += c) { for (int j = 1; j <=n; j += c) { // some O(1) expressions } } 2. When one of the loops is divided and multiplied by constant and other loop is increm...