'I thought answer of these time complexity is O(n) but it is wrong. Can someone explain?

What is the big-O notation of the given code snippet?

void snippet() { 
  for (int i = 0; i < n; i=i+5)    
    a[i] = i;  
}


Solution 1:[1]

The time complexity is O(n/5).

Let's take the simplified example (in javascript) below where n is 10

var n = 10;
for (var i = 0; i < n; i += 5)
    console.log(i);

The loop will run twice.

First i is 0, 
then i is 5, 
then i is 10 and 10 < 10 == false so we stop.

Effectively we are moving i towards n in steps of 5.

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 Jackson Kerr