Time Complexity where loop variable is incremented by 1, 2, 3, 4 .. Last Updated : 30 Nov, 2023 Comments Improve Suggest changes Like Article Like Report What is the time complexity of below code? C++ #include <iostream> using namespace std; void fun(int n) { int j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } C void fun(int n) { int j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } Java // Method with a parameter 'n' static void fun(int n) { // Initializing variables int j = 1, i = 0; // While loop with condition while (i < n) { // Some O(1) task i = i + j; j++; } } Python3 def fun(n): j, i = 1, 0 while i < n: # Some O(1) task i = i + j j += 1 C# using System; static void fun(int n) { int j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } JavaScript function fun(n) { let j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } The loop variable 'i' is incremented by 1, 2, 3, 4, ... until i becomes greater than or equal to n. The value of i is x(x+1)/2 after x iterations. So if loop runs x times, then x(x+1)/2 < n. Therefore time complexity can be written as ?(?n). Comment More infoAdvertise with us Next Article Time Complexity where loop variable is incremented by 1, 2, 3, 4 .. K kartik Improve Article Tags : Analysis of Algorithms Articles DSA Similar Reads Miscellaneous Problems of Time Complexity 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 thi 15 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 Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 Given an integer X, the task is to convert 1 into X by using the below-given operations: Multiply the number by 2.Multiply the number by 3.Add 1 to the number. The task is to print the minimum number of operations needed to convert 1 into X using these three operations and also print the sequence of 10 min read Count ways to reach each index by taking steps that is multiple of incremented K Given N and K, the task is to form an array where each element represents the number of ways to reach each index i (1 ? i ? N) by taking only the steps where step length is divisible by incremented K i.e., first step length should be divisible by K. Next, step length should be divisible by K + 1 and 9 min read Find Nth term of the series 4, 2, 2, 3, 6, ... Given a number N, the task is to find the N-th term in series 4, 2, 2, 3, 6, ...Example: Input: N = 2 Output: 2 Input: N = 5 Output: 6 Approach: Nth number of the series is obtained by Multiplying the previous number with the position of the previous number itself.Divide the obtained number by 2.Sin 4 min read Like