Maximum subarray problem

Joined
Sep 20, 2022
Messages
271
Reaction score
40
I needed to compute a maximum subarray. It would help me solve a larger problem.

I found an algorithm on wikipedia that was close to what I needed and made some changes to it. My program was ugly. It fell out of the ugly tree, and hit every branch on the way down.

After the problem was solved, and I didn't need the routine anymore, I found Kadane's algorithm.

It does exactly what I needed, with only 6 lines, and one of those was just a return statement.

A sublime piece of code, that I wish I had found first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,339
Messages
2,571,396
Members
48,790
Latest member
artyengy123

Latest Threads

Top