📌  相关文章
📜  形成算术级数(AP)的最长子数组

📅  最后修改于: 2021-04-29 12:57:09             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是查找形成算术级数的最长子数组的长度。
例子:

天真的方法:解决问题的最简单方法是生成所有可能的子数组,并针对每个子数组检查相邻元素之间的差异是否始终相同。在满足条件的所有此类子阵列中,存储最长子阵列的长度并将其打印为结果。
时间复杂度: O(N 3 )
辅助空间: O(1)

高效方法:为了优化上述方法,这里的想法是观察到,每当当前相邻元素对之间的差异不等于前一对相邻元素之间的差异时,请将前一个子数组的长度与最大长度进行比较。到目前为止已获得并开始一个新的子阵列,并相应地重复。请按照以下步骤解决问题:

  1. 初始化变量res以存储形成AP的最长子阵列的长度。
  2. 遍历其余数组,并将当前相邻差异与先前的相邻差异进行比较。
  3. 遍历数组,并为每个元素计算当前相邻元素对之间的差,并检查其是否等于上一个相邻元素对。如果发现是真的,则通过将res加1来继续进行中的子数组。
  4. 否则,考虑一个新的子阵列。通过它与先前的子数组的长度比较更新迄今为止获得的最大长度,即资源
  5. 最后,将res作为必需答案返回。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to return the length
// of longest subarray forming an AP
int getMaxLength(int arr[], int N)
{
 
    // Minimum possible length of
    // required subarray is 2
    int res = 2;
 
    // Stores the length of the
    // current subarray
    int dist = 2;
 
    // Stores the common difference
    // of the current AP
    int curradj = (arr[1] - arr[0]);
 
    // Stores the common difference
    // of the previous AP
    int prevadj = (arr[1] - arr[0]);
    for (int i = 2; i < N; i++) {
        curradj = arr[i] - arr[i - 1];
 
        // If the common differences
        // are found to be equal
        if (curradj == prevadj) {
 
            // Continue the previous subarray
            dist++;
        }
 
        // Start a new subarray
        else {
            prevadj = curradj;
 
            // Update the length to
            // store maximum length
            res = max(res, dist);
            dist = 2;
        }
    }
 
    // Update the length to
    // store maximum length
    res = max(res, dist);
 
    // Return the length of
    // the longest subarray
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 7, 4, 6, 8, 10, 11 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getMaxLength(arr, N);
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to return the length
// of longest subarray forming an AP
static int getMaxLength(int arr[], int N)
{
 
    // Minimum possible length of
    // required subarray is 2
    int res = 2;
 
    // Stores the length of the
    // current subarray
    int dist = 2;
 
    // Stores the common difference
    // of the current AP
    int curradj = (arr[1] - arr[0]);
 
    // Stores the common difference
    // of the previous AP
    int prevadj = (arr[1] - arr[0]);
    for (int i = 2; i < N; i++)
    {
        curradj = arr[i] - arr[i - 1];
 
        // If the common differences
        // are found to be equal
        if (curradj == prevadj)
        {
            // Continue the previous subarray
            dist++;
        }
 
        // Start a new subarray
        else
        {
            prevadj = curradj;
 
            // Update the length to
            // store maximum length
            res = Math.max(res, dist);
            dist = 2;
        }
    }
 
    // Update the length to
    // store maximum length
    res = Math.max(res, dist);
 
    // Return the length of
    // the longest subarray
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = {10, 7, 4,
                 6, 8, 10, 11};
    int N = arr.length;
    System.out.print(getMaxLength(arr, N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# python3 Program to implement
# the above approach
 
# Function to return the length
# of longest subarray forming an AP
def getMaxLength(arr,  N):
 
    # Minimum possible length of
    # required subarray is 2
    res = 2
 
    # Stores the length of the
    # current subarray
    dist = 2
 
    # Stores the common difference
    # of the current AP
    curradj = (arr[1] - arr[0])
 
    # Stores the common difference
    # of the previous AP
    prevadj = (arr[1] - arr[0])
    for i in range(2, N):
        curradj = arr[i] - arr[i - 1]
 
        # If the common differences
        # are found to be equal
        if (curradj == prevadj):
 
            # Continue the previous subarray
            dist += 1
 
        # Start a new subarray
        else:
            prevadj = curradj
 
            # Update the length to
            # store maximum length
            res = max(res, dist)
            dist = 2
 
    # Update the length to
    # store maximum length
    res = max(res, dist)
 
    # Return the length of
    # the longest subarray
    return res
 
# Driver Code
if __name__ == "__main__":
    arr = [10, 7, 4, 6, 8, 10, 11]
    N = len(arr)
    print(getMaxLength(arr, N))
 
# This code is contributed by Chitranayal


C#
// C# Program to implement
// the above approach
using System;
 
public class GFG{
 
// Function to return the length
// of longest subarray forming an AP
static int getMaxLength(int []arr,
                        int N)
{
 
    // Minimum possible length of
    // required subarray is 2
    int res = 2;
 
    // Stores the length of the
    // current subarray
    int dist = 2;
 
    // Stores the common difference
    // of the current AP
    int curradj = (arr[1] - arr[0]);
 
    // Stores the common difference
    // of the previous AP
    int prevadj = (arr[1] - arr[0]);
   
    for (int i = 2; i < N; i++)
    {
        curradj = arr[i] - arr[i - 1];
 
        // If the common differences
        // are found to be equal
        if (curradj == prevadj)
        {
            // Continue the previous subarray
            dist++;
        }
 
        // Start a new subarray
        else
        {
            prevadj = curradj;
 
            // Update the length to
            // store maximum length
            res = Math.Max(res, dist);
            dist = 2;
        }
    }
 
    // Update the length to
    // store maximum length
    res = Math.Max(res, dist);
 
    // Return the length of
    // the longest subarray
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = {10, 7, 4,
                 6, 8, 10, 11};
    int N = arr.Length;
    Console.Write(getMaxLength(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)