TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

This problem (reportedly) took Don Knuth, 24 hours to solve

1 pointsby sohamover 9 years ago

1 comment

informatimagoover 9 years ago
I don&#x27;t know what was Knuth&#x27;s solution, but it&#x27;s trivial:<p><pre><code> (- (reduce &#x27;+ vector) (let ((n (- (length vector) 2))) (&#x2F; (* n (+ n 1)) 2))) </code></pre> reduce is O(n) time and O(1) space. length is O(1) time and space.<p>Notice also that if there are no duplicates, this formula will give the maximum element (- (length vector) 1).