形成具有给定公差的算术级数 (AP) 的最长子阵列
给定一个由N个整数组成的数组arr[]和一个整数K ,任务是找到形成具有共同差K的算术级数的最长子数组的长度。
例子:
Input: arr[] = {3, 4, 5}, K = 1
Output: 3
Explanation: The longest subarray forming an AP with common difference 1 is {3, 4, 5}.
Input: arr[] = {10, 7, 4, 6, 8, 10, 11}, K = 2
Output: 4
Explanation: The longest possible subarray forming an AP with common difference as 2 is {4, 6, 8, 10} .
方法:给定的问题是一个基于实现的问题,可以使用滑动窗口技术来解决。请按照以下步骤解决问题:
- 遍历给定数组并维护一个变量,该变量存储当前窗口中变量的数量。
- 如果当前元素与数组中前一个元素的差值为K ,则增加当前窗口的大小,否则,将窗口的大小重置为1 。
- 打印最大差异作为答案。
下面是上述方法的实现:
C++14
// C++ program of the above approach
#include
using namespace std;
// Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
int maxlenAP(int arr[], int& n, int& d)
{
// Stores the length of
// the current window
int count = 1;
// Stores final answer
int maxLen = INT_MIN;
// Loop to traverse array
for (int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = max(maxLen, count);
}
// Return Answer
return maxLen;
}
// Driver Code
int main()
{
int arr[] = { 10, 7, 4, 6, 8, 10, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
cout << maxlenAP(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
static int maxlenAP(int []arr, int n, int d)
{
// Stores the length of
// the current window
int count = 1;
// Stores final answer
int maxLen = Integer.MIN_VALUE;
// Loop to traverse array
for (int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = Math.max(maxLen, count);
}
// Return Answer
return maxLen;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 10, 7, 4, 6, 8, 10, 11 };
int N = arr.length;
int K = 2;
System.out.println(maxlenAP(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to find longest subarray
# forming an Arithmetic Progression
# with the given common difference
def maxlenAP(arr, n, d):
# Stores the length of
# the current window
count = 1
# Stores final answer
maxLen = 10 ** -9
# Loop to traverse array
for i in range(1, n):
if (arr[i] - arr[i - 1] == d):
# Increment window size
count += 1
else:
# Reset window size
count = 1
# Update answer
maxLen = max(maxLen, count)
# Return Answer
return maxLen
# Driver Code
arr = [10, 7, 4, 6, 8, 10, 11]
N = len(arr)
K = 2
print(maxlenAP(arr, N, K))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG {
// Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
static int maxlenAP(int []arr, int n, int d)
{
// Stores the length of
// the current window
int count = 1;
// Stores final answer
int maxLen = Int32.MinValue;
// Loop to traverse array
for (int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = Math.Max(maxLen, count);
}
// Return Answer
return maxLen;
}
// Driver Code
public static void Main()
{
int []arr = { 10, 7, 4, 6, 8, 10, 11 };
int N = arr.Length;
int K = 2;
Console.Write(maxlenAP(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(1)