r/ProgrammerHumor • • Oct 14 '26

Meme everythingIsO1ToGod

Post image
4.5k Upvotes

167 comments sorted by

View all comments

Show parent comment

14

u/Drugbird Oct 14 '26

Otherwise, as they said, you can parallelize the function to run on n2 processor cores such that it technically runs in O(1) time,

Ok, I'll bite. You can have N cores all read 1 value from the array, then each core spawns N-1 other threads / cores that each read 1 other value to compare to. That's all O(1).

But at the end of that you'll need to check the result of all these N-1 cores to check that they're all lower. And that requires O(n) time in naive implementation, or O(log(N)) if you do a reduction strategy.

4

u/_killer1869_ Oct 14 '26

Damn, I should've checked their claim properly. I didn't consider the time complexity of actually joining the results together, only the comparison itself. Thanks for the correction.

-3

u/Mindgapator Oct 14 '26

You just need a better algorithm. We note A the initial array. n its length. Each step is a single cpu cycle Step 1: allocate a buffer of size n, called B initialized with zeros. Step 2: for each i,j in [0, n[ with i != j if A[i] <= A[j] then write 1 in B[i]. This is O(1) because we have enough cpus, and if they write, they all write the same value. Step 3: for each i in [0, n[ if B[i] == 0 return A[i]. This is O(1) because we have enough cpus. A single value is returned by design of the algo.

8

u/oshaboy Oct 14 '26

Yeah I can probably solve any O(N) problem in O(1) time if I had O(N2) cores. That doesn't make the problem O(1) because as the problem increases you either need to add more cores or take more time. Which is the definition of the complexity of the algorithm.

Also you need to initialize the threads anyway. Even if you have each thread initializing threads until you have enough O(N2) threads that would still require O(log(N)) time.

-1

u/Mindgapator Oct 14 '26

Well it moves a time constraint to a resource one. Time is constant but resources needed scale at n2. Thread init is an implementation detail. It obviously won't work on a machine from the shelf, but a dedicated built could do it.

4

u/TheHyperbolicTangent Oct 14 '26

This is still O(n), as the number of ops scale linearly with the size of the input. Big-O is fundamentally a measure of an algorithm’s complexity. Saying it moves a time constraint to a resource constraint doesn’t alleviate the algorithms complexity.

0

u/Mindgapator Oct 14 '26

We usually note O(n) for time complexity lol, but whatever

0

u/[deleted] Oct 14 '26

[deleted] — view deleted comment

-1

u/Mindgapator Oct 14 '26

You don't. You write a predefined memory address. If others write at the same time, it's only UB if a different value tried to be written. Check the original comment answers, someone else described at the same time something similar if you want a different approach.