Skip to main content
All Questions

Sum the sums of all subarrays that form a good arithmetic sequence.

Unlock detailed company stats for this questionUpgrade

An arithmetic sequence is a list of numbers where subtracting any number from the previous one always gives a constant difference. A good arithmetic sequence is an arithmetic sequence with a common difference of either 1 or -1. For example, [4, 5, 6] is good, and any sequence with only one element, such as [4], is good.

Given an integer array nums, return the sum of the sums of each subarray that is a good arithmetic sequence.

Example: nums = [7, 4, 5, 6, 5]. The good subarrays are [7], [4], [5], [6], [5], [4, 5], [5, 6], [6, 5] and [4, 5, 6]. Their sums are 7, 4, 5, 6, 5, 9, 11, 11 and 15, so the answer is 73.

Hint from the candidate: find the start and end of each sequence and count how many segments each number appears in.

Format · Google L3 onsite.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.