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 .. P Piyush Gupta Improve Article Tags : Articles Analysis of Algorithms 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 Find n-th term in series 1 2 2 3 3 3 4 4 4 4.... Given series 1 2 2 3 3 3 4 4 4 4 ...., find n-th term of the series. The "pattern" is obvious. There is one "1", two "2"s three "3"s, etc. Examples: Input : n = 5 Output : 3Input : n = 7Output : 4A naive approach is to run two loops one from 1 to n and the other from 1 to i, and for every iteration 11 min read Value of k-th index of a series formed by append and insert MEX in middle Given two integers, n and k. Initially we have a sequence consisting of a single number 1. We need to consider series formed after n steps. In each step, we append the sequence to itself and insert the MEX(minimum excluded)(>0) value of the sequence in the middle. Perform (n-1) steps. Finally, fi 6 min read Find the Nth term of the series 1, 2, 6, 21, 88, 445. . . Given a positive integer N. The task is to find Nth term of the series: 1, 2, 6, 21, 88, 445, . . . Examples: Input: N = 3Output: 6 Input: N = 6Output: 445 Approach: The given sequence follows the following pattern- 1, (1 * 1 + 1 = 2), (2 * 2 + 2 = 6), (6 * 3 + 3 = 21), (21 * 4 + 4 = 88), (88 * 5 + 4 min read Steps to return to {1, 2, ..n} with specified movements Given an array moves[] that contains a permutation of first n natural numbers, each element of this array represents a movement that is each element shows an index where the element goes after each step. Now following these steps we need to tell after how many steps array [1..n] returns back to [1.. 10 min read Make all array elements equal to K by repeatedly incrementing subsequences Given an array arr[] consisting of N integers and an integer K, the task is to make all array elements equal to K by repeatedly incrementing all elements of subsequences by 1. Note: The value of K is at least the maximum element of the array. Examples: Input: arr[] = {2, 3, 3, 4}, K = 5Output: 4 Exp 10 min read Like