📜  计算数组中大小至少为 3 的算术序列

📅  最后修改于: 2021-09-22 09:40:05             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到数组中所有算术序列的计数。
例子:

幼稚的方法:

  • 运行两个循环并检查每个长度至少为 3 的序列。
  • 如果序列是等差数列,则将答案加 1。
  • 最后,返回所有大小至少为 3 的算术子数组的计数。

时间复杂度:O(N 2 )
高效的方法:我们将使用动态规划方法来维护所有等差数列的计数,直到任何位置。

  • 0初始化一个变量res 。它将存储序列的计数。
  • 初始化一个变量,用0计数。它将存储序列的大小减去 2。
  • 如果当前元素形成一个等差数列,则增加 count 的值,否则使其为零。
  • 如果当前元素 L[i] 与 L[i-1] 和 L[i-2] 构成一个等差数列,则直到第 i 次迭代的等差数列的数量由下式给出:
  • 最后,返回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


Javascript


输出:
4

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程