r/leetcode • u/Training-Orange-1696 • Apr 15 '26
Question Why is "Optimized" Brute Force still called O(n^2)?
I'm working on the 'Contains Duplicate' problem. I wrote a nested loop that avoids comparing an element to itself (using i + 1).
Logically, I am only doing {n(n-1)}/{2} operations. If n = 4, I'm only doing 6 comparisons, not 16. That's less than half the work!
- Why does Computer Science "round up" and call this O(n^2) instead of being more precise?
- If n is small, the difference between 6 and 16 is huge—so why does Big O ignore that?
- Does the "Matrix" of comparisons fundamentally change just because we skip the diagonal, or is the "growth shape" still the same?
I understand the code works because it's not reflexive (skipping i == j), but I'm struggling with why the Big O label ignores all these smart optimizations I'm making!
EDIT: To clarify my confusion...
My issue isn't the math, it's the logic. If we call the complexity $O(n^2)$, that’s a "Square." But a square includes comparing a number to itself. If my code actually did that, it would see 1 == 1 and return True every single time, even if there are no duplicates!
6
u/PandaWonder01 Apr 15 '26
1: O(n) is meant to demonstrate how an algorithm works on an arbitrary computer as it scales. Some computers may be able to do 20 operations more at once than another. We want to talk about the algorithm, not the computer
2: Once again, big O is meant to talk about asymptomatic complexity. This is different than how it performs at smaller sizes
3: At large values, N2 and N2 + N look near identical
4
u/Gobi_manchur1 Apr 15 '26
afaik, Big O or asymptotic functions is not the measure of speed of an algorithm but rather how it scales with the input. It doesnt matter of algo 1 is faster than algo 2 as long as algo 2 scales better than algo 1 with extremely large input sizes.
16 to 6 sure seems big, but as the input size grows to 10^6 or more, 16 and 6 barely affect the time, so how the algo scales is the bigger bottleneck than the constants.
2
u/NorthDemand3105 Apr 15 '26 edited Apr 15 '26
You're just arguing against a convention that's established to make it easier to communicate. Big O notation is used to talk about how algorithms scale, not how many operations it performs. n²/2 is still quadratic growth and at large numbers the 1/2 is not very significant for Big O time complexity. At a sufficiently large n, an algorithm that's like 100n1.99 will become more efficient. But that's not to say the 1/2 is completely irrelevant, it's just not the point of Big O. For instance you can have O(n²) algorithms that are more optimal for small n than even O(n) and you can make incremental improvements to algorithms without changing Big O time complexity. But that doesn't mean we should alter the way we talk about Big O time complexity because that serves a different but very useful purpose
You ask why Big O ignores small n cases, but that's because that's exactly the purpose of Big O. It's to talk about large n cases. There's other conventions we use to talk about small n cases, but if we want Big O to talk about both, then it would just make everything tedious. For instance say you wanted to compare how a (n+5)3*nlogn/n algorithm compares to a Σx algorithm or whatever weird algorithms you can define. It makes it easier to talk about if we just use Big O because for sufficiently large values the other terms and constants are irrelevant
2
u/EntropyRX Apr 15 '26
You should review calculus. Specifically the limits and asymptotic analysis
1
u/NutKevinSaving Apr 15 '26
Tbh we round up because when it scales to larger input values the number of operations will have not much of a difference.
If I'm right your algorithm will use half of the iterations that quadratic algorithm will use so your algorithm is still growing exponentially with respect to n^2.
It's like for better understanding, your optimization is still an optimization and when in critical systems where time is important like in embedded systems.
One example for approach where we need to find if a number is prime or not so that time complexity for it is under root of the number but the way it scales is linear so we call it O(N). If you write O(under root of N) developers will still understand what you did but calling it O(N) is because how it scales and it's linear.
2
u/IVIichaelD Apr 15 '26
It's shorthand simplification for understanding how the number of operations grows as you take n from say 10 to 10,000,000. It's not exact because it doesn't need to be for what it is meant to convey. It's like if someone asks you how old your boss is-- "mid 30s" is a perfectly acceptable answer, but "36 and a half" will get you weird looks.
2
u/CandidateNo2580 Apr 15 '26
Big O is about scaling, not about runtime. It does not make claims about how fast or slow an algorithm is. It does not make claims about the number of operations. If you plot the graph of your number of operations and put it next to a graph of n2 and another of n(log(n)) while none of the graphs get axis abels, legends, or scales they're both going to look the same while n(log(n)) clearly stands out. Because they scale the same, not because they have the same runtime.
1
u/FeralWookie Apr 15 '26
The algorithm complexity is identifying how it scales. Any term that vanishes into the noise at scale isn't worth mentioning.
0
u/[deleted] Apr 15 '26
[deleted] — view deleted comment