# Big-O comparison different arrangements of similar code [duplicate]

The following code should have a run time of $O(N)$,

int min = INTEGER.MAX_VALUE;
int max = INTEGER.MIN_VALUE;

for (int x : array) {
if (x < min) min = x;
if (x > min) max = x;
}


but what about the following code?

int min = INTEGER.MAX_VALUE;
int max = INTEGER.MIN_VALUE;

for (int x : array) {
if (x < min) min = x;
}
for (int x : array) {
if (x > min) max = x;
}

• Welcome to Computer Science! We discourage posts that simply state a problem out of context, and expect the community to solve it. Assuming you tried to solve it yourself and got stuck, it may be helpful if you wrote your thoughts and what you could not figure out. It will definitely draw more answers to your post. Until then, the question will be voted to be closed / downvoted. You may also want to check out these hints, or use the search engine of this site to find similar questions that were already answered. – Raphael Sep 28 '16 at 6:09

Its O(N). When there are consecutive loops, we calculate time complexity as sum of time complexities of individual loops.

for (int i = 1; i <=m; i += c)
{
// some O(1) expressions
}
for (int i = 1; i <=n; i += c)
{
// some O(1) expressions
}


Time complexity of above code is O(m) + O(n) which is O(m+n) If m == n, the time complexity becomes O(2n) which is O(n).