Count Arithmetic Subsequences

Quick Overview

Count arithmetic subsequences of at least three elements while preserving index choices, repeated values, and the full signed integer domain. Includes examples and a written reference.

Count Arithmetic Subsequences

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Count Arithmetic Subsequences Given an integer array `nums`, return the number of subsequences of length at least three whose consecutive values have the same difference. A subsequence keeps the original order but may skip elements. Choices of different indices count separately, even when they produce the same values. The answer is guaranteed to fit in a signed 32-bit integer. ### Function `int numberOfArithmeticSlices(vector<int>& nums)` ### Example 1 ```text Input: nums = [1,3,5,7] Output: 3 ``` The three subsequences are `[1,3,5]`, `[3,5,7]`, and `[1,3,5,7]`. ### Example 2 ```text Input: nums = [4,4,4,4] Output: 5 ``` There are four ways to choose three indices and one way to choose all four. Every such choice has difference zero. ### Constraints - `1 <= nums.length <= 1000`. - `-2147483648 <= nums[i] <= 2147483647`. - The required count fits in a signed 32-bit integer. ### Clarifications Two elements alone do not count. Subsequence elements need not be adjacent. Negative and zero differences are allowed, and equal values at different positions remain distinct index choices. ### What a Strong Answer Covers - Counting every eligible index sequence exactly once. - Repeated values, short arrays, and decreasing subsequences. - Arithmetic across the full signed 32-bit value range. - Correctness and time and space bounds for one thousand elements.

Overview: Count arithmetic subsequences of at least three elements while preserving index choices, repeated values, and the full signed integer domain. Includes examples and a written reference.

Amazon logo
Amazon
hardSoftware EngineerOtherCoding & Algorithms
0
0

Count Arithmetic Subsequences

Given an integer array nums, return the number of subsequences of length at least three whose consecutive values have the same difference.

A subsequence keeps the original order but may skip elements. Choices of different indices count separately, even when they produce the same values. The answer is guaranteed to fit in a signed 32-bit integer.

Function

int numberOfArithmeticSlices(vector<int>& nums)

Example 1

Input: nums = [1,3,5,7]
Output: 3

The three subsequences are [1,3,5], [3,5,7], and [1,3,5,7].

Example 2

Input: nums = [4,4,4,4]
Output: 5

There are four ways to choose three indices and one way to choose all four. Every such choice has difference zero.

Constraints

  • 1 <= nums.length <= 1000 .
  • -2147483648 <= nums[i] <= 2147483647 .
  • The required count fits in a signed 32-bit integer.

Clarifications

Two elements alone do not count. Subsequence elements need not be adjacent. Negative and zero differences are allowed, and equal values at different positions remain distinct index choices.

What a Strong Answer Covers Guidance

  • Counting every eligible index sequence exactly once.
  • Repeated values, short arrays, and decreasing subsequences.
  • Arithmetic across the full signed 32-bit value range.
  • Correctness and time and space bounds for one thousand elements.