给定大小为N的数组arr [] ,任务是查找数组中所有算术序列的计数。
例子:
Input: arr = [1, 2, 3, 4]
Output: 3
Explanation:
The arithmetic sequences in arr are [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
Input: arr = [1, 3, 5, 6, 7, 8]
Output: 4
Explanation:
The arithmetic sequences in arr are [1, 3, 5], [5, 6, 7], [5, 6, 7, 8] and [6, 7, 8].
天真的方法:
- 运行两个循环,并检查每个长度序列至少为3。
- 如果序列是算术序列,则将答案增加1。
- 最后,返回大小至少为3的所有算术子数组的计数。
时间复杂度:O(N 2 )
高效的方法:我们将使用动态编程方法来保持所有算术序列的计数直到任何位置。
- 初始化变量res为0 。它将存储序列数。
- 初始化变量,以0计数。它将存储序列的大小减去2。
- 如果当前元素形成算术序列,则增加count的值,否则将其设置为零。
- 如果当前元素L [i]与L [i-1]和L [i-2]构成算术序列,则直到第i次迭代的算术序列数为:
res = res + count
- 最后,返回res变量。
下面是上述方法的实现:
C++
// C++ program to find all arithmetic
// sequences of size atleast 3
#include
using namespace std;
// Function to find all arithmetic
// sequences of size atleast 3
int numberOfArithmeticSequences(int L[], int N)
{
// If array size is less than 3
if (N <= 2)
return 0;
// Finding arithmetic subarray length
int count = 0;
// To store all arithmetic subarray
// of length at least 3
int res = 0;
for (int i = 2; i < N; ++i) {
// Check if current element makes
// atithmetic sequence with
// previous two elements
if (L[i] - L[i - 1] == L[i - 1] - L[i - 2]) {
++count;
}
// Begin with a new element for
// new arithmetic sequences
else {
count = 0;
}
// Accumulate result in till i.
res += count;
}
// Return final count
return res;
}
// Driver code
int main()
{
int L[] = { 1, 3, 5, 6, 7, 8 };
int N = sizeof(L) / sizeof(L[0]);
// Function to find arithematic sequences
cout << numberOfArithmeticSequences(L, N);
return 0;
}
Java
// Java program to find all arithmetic
// sequences of size atleast 3
class GFG{
// Function to find all arithmetic
// sequences of size atleast 3
static int numberOfArithmeticSequences(int L[], int N)
{
// If array size is less than 3
if (N <= 2)
return 0;
// Finding arithmetic subarray length
int count = 0;
// To store all arithmetic subarray
// of length at least 3
int res = 0;
for (int i = 2; i < N; ++i) {
// Check if current element makes
// atithmetic sequence with
// previous two elements
if (L[i] - L[i - 1] == L[i - 1] - L[i - 2]) {
++count;
}
// Begin with a new element for
// new arithmetic sequences
else {
count = 0;
}
// Accumulate result in till i.
res += count;
}
// Return final count
return res;
}
// Driver code
public static void main(String[] args)
{
int L[] = { 1, 3, 5, 6, 7, 8 };
int N = L.length;
// Function to find arithmetic sequences
System.out.print(numberOfArithmeticSequences(L, N));
}
}
// This code contributed by sapnasingh4991
Python3
# Python3 program to find all arithmetic
# sequences of size atleast 3
# Function to find all arithmetic
# sequences of size atleast 3
def numberOfArithmeticSequences(L, N) :
# If array size is less than 3
if (N <= 2) :
return 0
# Finding arithmetic subarray length
count = 0
# To store all arithmetic subarray
# of length at least 3
res = 0
for i in range(2,N):
# Check if current element makes
# atithmetic sequence with
# previous two elements
if ( (L[i] - L[i - 1]) == (L[i - 1] - L[i - 2])) :
count += 1
# Begin with a new element for
# new arithmetic sequences
else :
count = 0
# Accumulate result in till i.
res += count
# Return final count
return res
# Driver code
L = [ 1, 3, 5, 6, 7, 8 ]
N = len(L)
# Function to find arithematic sequences
print(numberOfArithmeticSequences(L, N))
# This code is contributed by Sanjit_Prasad
C#
// C# program to find all arithmetic
// sequences of size atleast 3
using System;
class GFG{
// Function to find all arithmetic
// sequences of size atleast 3
static int numberOfArithmeticSequences(int []L,
int N)
{
// If array size is less than 3
if (N <= 2)
return 0;
// Finding arithmetic subarray length
int count = 0;
// To store all arithmetic subarray
// of length at least 3
int res = 0;
for(int i = 2; i < N; ++i)
{
// Check if current element makes
// atithmetic sequence with
// previous two elements
if (L[i] - L[i - 1] ==
L[i - 1] - L[i - 2])
{
++count;
}
// Begin with a new element for
// new arithmetic sequences
else
{
count = 0;
}
// Accumulate result in till i.
res += count;
}
// Return readonly count
return res;
}
// Driver code
public static void Main(String[] args)
{
int []L = { 1, 3, 5, 6, 7, 8 };
int N = L.Length;
// Function to find arithmetic sequences
Console.Write(numberOfArithmeticSequences(L, N));
}
}
// This code is contributed by amal kumar choubey
输出:
4
时间复杂度: O(N)