'What is the time complexity of for loop in which the counter starts from 1?
May i know for the outer loop the time complexity is n+1-2? So its n-1
Example
for(int a=3; a<=n; a++){
System.out.println(a);
}
So i have another question, assume that a < n , so the time complexity would be n-2? Thanks
Solution 1:[1]
Your loop
for(int a=3; a<=n; a++){
System.out.println(a);
}
is running n-2 times, and if you compare it like a < n, the loop stops before 'a' reaches 'n', so n-3 times.
Since per definition for the Big-O-Notation constants don't matter the complexity of your loop is:
O(n-2) = O(n-3) = O(n).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jakob Graf |
