Given an array of integers find the largest consecutive sum of 3 integers. In the array [1,5,8,3,2,7,4], the largest sum of 3 consecutive is 5+8+3. When you find the largest sum print out the 3 integers. Can you solve this in O(n)?
Given an array of integers find the largest consecutive sum of 3 integers. In the array [1,5,8,3,2,7,4], the largest sum of 3 consecutive is 5+8+3. When you find the largest sum print out the 3 integers. Can you solve this in O(n)?
Permalink: http://problemotd.com/problem/3s-sum/
Content curated by @MaxBurstein
Comments:
Anon - 10 years, 1 month ago
Java; Sorry if this is not O(n). I'm still new to big O notation.
reply permalink
Anonymous - 10 years, 1 month ago
Sorting the array changes the order so you can not ensure the three largest ints were consecutive before the sort.
An example: {0,1,0,0,3,0,0,5} should return 5 but your function would return 9.
Your implementation's runtime is bounded by Arrays.sort() run time which is O(n log n).
reply permalink
Anonymous - 10 years, 1 month ago
Javascript - how's this?
reply permalink
Anonymous - 10 years, 1 month ago
O(n), python, beautifully simple
reply permalink
asheehan - 10 years, 1 month ago
in ruby:
not the cleanest, but it works and is pretty close to O(n)
reply permalink
asheehan - 10 years, 1 month ago
rewrote this a little to be O(n)
reply permalink