First Code:
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0) ans += 1;
}
}
The time complexity of this code is $$$O(n^2)$$$
Second Code:
if ((1 + 1) % 2 == 0) ans += 1;
if ((1 + 2) % 2 == 0) ans += 1;
if ((1 + 3) % 2 == 0) ans += 1;
if ((1 + 4) % 2 == 0) ans += 1;
if ((1 + 5) % 2 == 0) ans += 1;
.........
if ((n + n) % 2 == 0) ans += 1;
Is the time complexity of this code same as the first code? Or is it $$$O(1)$$$ ?.
Thank You.