给定一个大小为N的数组arr[] ,任务是找到形成算术级数的最长子数组的长度。
例子:
Input: arr[] = {3, 4, 5}
Output: 3
Explanation:The longest subarray forming an AP is {3, 4, 5} with common difference 1.
Input: {10, 7, 4, 6, 8, 10, 11}
Output: 4
Explanation:The longest possible subarray forming an AP is {4, 6, 8, 10} with common difference(= 2).
朴素方法:解决问题的最简单方法是生成所有可能的子数组,并为每个子数组检查相邻元素之间的差异是否始终保持不变。在所有满足条件的子数组中,存储最长子数组的长度并打印为结果。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:为了优化上述方法,这里的想法是观察,每当当前一对相邻元素之间的差异不等于前一对相邻元素之间的差异时,将前一个子数组的长度与最大值进行比较到目前为止获得并开始一个新的子数组并相应地重复。请按照以下步骤解决问题:
- 初始化变量res以存储形成 AP 的最长子阵列的长度。
- 迭代剩余的数组并将当前相邻差异与前一个相邻差异进行比较。
- 迭代数组,对于每个元素,计算当前相邻元素对之间的差值,并检查它是否等于前一对相邻元素。如果发现为真,则通过将res增加 1 来继续进行中的子数组。
- 否则,考虑一个新的子数组。通过与前一个子数组的长度进行比较来更新到目前为止获得的最大长度,即res 。
- 最后,返回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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live