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
1y

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!
Add answer anonymously...
Dunzo Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter