Arithmetic Subarrays Problem Statement

You are provided with an array A of length N. Your task is to determine the number of arithmetic subarrays present within the array A.

Explanation:

An arithmetic subarray is defined as a subarray that contains 3 or more elements, where the difference between consecutive elements remains consistent. For example, [1, 3, 5, 7] is an arithmetic subarray with a length of 4, and the difference between any two consecutive elements is 2.

Note:
A subarray is a sequence extracted from a contiguous segment of an array and maintains the order of elements.

Input:

The first line contains the integer T representing the number of test cases.
Each test case consists of two lines:
1. The first line includes the integer N, representing the length of the array.
2. The second line contains N space-separated integers representing the elements of the array A.

Output:

Return the number of arithmetic subarrays found for each test case.

Example:

Input:
2
4
1 3 5 7
5
1 2 4 6 8
Output:
3
2

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000
  • 0 ≤ A[i] ≤ 5000
  • Time Limit: 1 second
Note:
Ensure your implementation only returns results, as output is already managed.
AnswerBot
2y

Count the number of arithmetic subarrays in an array.

  • An arithmetic subarray has 3 or more elements with the same difference between consecutive elements.

  • Loop through the array and check for all possib...read more

Help your peers!
Select
Add answer anonymously...
Dunzo Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits