Grokking Google Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Arithmetic Slices (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an integer array nums, return the number of arithmetic subarrays of nums.

If any integer array consists of at least three elements and if the difference between any two consecutive elements is the same, it is called arithmetic.

A subarray is a contiguous subsequence of the array.

Examples

Example 1:

  • Input: [1, 3, 5, 7, 9, 10, 11]
  • Expected Output: 7
  • Justification: The segments that form arithmetic sequences are [1, 3, 5], [3, 5, 7], [5, 7, 9], [1, 3, 5, 7], [3, 5, 7, 9], [1, 3, 5, 7, 9], and [9, 10, 11]. Each of these segments has a constant difference of 2 between consecutive elements.

Example 2:

  • Input: [7, 7, 7, 7]
  • Expected Output: 3
  • Justification: The qualifying segments are [7, 7, 7], [7, 7, 7, 7] (considering the first three and then all four elements), and the last three elements [7, 7, 7]. Each segment has a constant difference of 0 between its elements.

Example 3:

  • Input: [1, 2, 4, 6, 8]
  • Expected Output: 3
  • Justification: Only three segments [2,4,6], [4, 6, 8] and [2, 4, 6, 8] form arithmetic sequences with a constant difference of 2.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible