We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First, O(N) is the same as O(N/2). The "O" means "order of", meaning that we ignore the coefficients to the terms (be they polynomial, log, or exponential).
Second, this algorithm is no more efficient than the trivial one. It's also considerably less clear.
While it's true you loop half the number of times, you do two comparisons each time through the loop. So, every character is examined twice (except the ones on the ends).
Those who are timing out are doing something very inefficient, and are not O(N).
Westerngravity has the right idea.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Alternating Characters
You are viewing a single comment's thread. Return to all comments →
First, O(N) is the same as O(N/2). The "O" means "order of", meaning that we ignore the coefficients to the terms (be they polynomial, log, or exponential).
Second, this algorithm is no more efficient than the trivial one. It's also considerably less clear.
While it's true you loop half the number of times, you do two comparisons each time through the loop. So, every character is examined twice (except the ones on the ends).
Those who are timing out are doing something very inefficient, and are not O(N).
Westerngravity has the right idea.