给定N个整数的数组arr [] ,任务是找到满足给定条件之一的arr []子数组的最大长度:
- 子数组严格增加。
- 子数组严格减少。
- 子数组首先严格递增,然后严格递减。
例子:
Input: arr[] = {1, 2, 2, 1, 3}
Output: 2
{1, 2}, {2, 1} and {1, 3} are the valid subarrays.
Input: arr[] = {5, 4, 3, 2, 1, 2, 3, 4}
Output: 5
{5, 4, 3, 2, 1} is the required subarray.
方法:创建一个数组incEnding [] ,其中incEnding [i]将存储给定数组的最大递增子数组的长度,该子数组的结尾为索引i 。同样,创建另一个数组decStarting [] ,其中decStarting [i]将存储从索引i开始的给定数组的最大递减子数组的长度。现在开始遍历原始数组,并为每个元素假定它是所需子数组的中间,然后是最大所需子数组的长度,该数组的下标在索引i处将为incEnding [i] + decStarting [i] – 1 。注意,减去1是因为arr [i]对于递增和递减子数组都将被计数两次。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the largest
// required sub-array
int largestSubArr(int arr[], int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int incEnding[n] = { 0 };
incEnding[0] = 1;
for (int i = 1; i < n; i++) {
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int decStarting[n] = { 0 };
decStarting[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (int i = 0; i < n; i++) {
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = max(maxSubArr, incEnding[i]
+ decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 2, 1, 3 };
int n = sizeof(arr) / sizeof(int);
cout << largestSubArr(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the largest
// required sub-array
static int largestSubArr(int arr[], int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int incEnding[] = new int[n];
int i;
for(i = 0; i < n ; i++)
incEnding[i] = 0;
incEnding[0] = 1;
for (i = 1; i < n; i++)
{
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int decStarting[] = new int[n];
for(i = 0; i < n ; i++)
decStarting[i] = 0;
decStarting[n - 1] = 1;
for (i = n - 2; i >= 0; i--)
{
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (i = 0; i < n; i++)
{
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = Math.max(maxSubArr, incEnding[i] +
decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 2, 1, 3 };
int n = arr.length;
System.out.println(largestSubArr(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the largest
# required sub-array
def largestSubArr(arr, n) :
# incEnding[i] will store the length
# of the largest increasing subarray
# ending at arr[i]
incEnding = [0] * n
incEnding[0] = 1
for i in range(1, n) :
# If current element is greater than
# the previous element then it
# can be a part of the previous
# increasing subarray
if (arr[i - 1] < arr[i]) :
incEnding[i] = incEnding[i - 1] + 1
else :
incEnding[i] = 1
# decStarting[i] will store the length
# of the largest decreasing subarray
# starting at arr[i]
decStarting = [0] * n
decStarting[n - 1] = 1
for i in range(n - 2, -1, -1):
# If current element is greater than
# the next element then it can be a part
# of the decreasing subarray
# with the next element
if (arr[i + 1] < arr[i]) :
decStarting[i] = decStarting[i + 1] + 1
else :
decStarting[i] = 1
# To store the length of the
# maximum required subarray
maxSubArr = 0
# Assume every element to be the mid
# point of the required array
for i in range(n):
# 1 has to be subtracted because the
# current element will be counted for
# both the increasing and
# the decreasing subarray
maxSubArr = max(maxSubArr, incEnding[i] +
decStarting[i] - 1)
return maxSubArr
# Driver code
arr = [ 1, 2, 2, 1, 3 ]
n = len(arr)
print(largestSubArr(arr, n))
# This code is contributed by
# divyamohan123
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the largest
// required sub-array
static int largestSubArr(int []arr, int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int []incEnding = new int[n];
int i;
for(i = 0; i < n ; i++)
incEnding[i] = 0;
incEnding[0] = 1;
for (i = 1; i < n; i++)
{
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int []decStarting = new int[n];
for(i = 0; i < n ; i++)
decStarting[i] = 0;
decStarting[n - 1] = 1;
for (i = n - 2; i >= 0; i--)
{
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (i = 0; i < n; i++)
{
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = Math.Max(maxSubArr, incEnding[i] +
decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 1, 2, 2, 1, 3 };
int n = arr.Length;
Console.WriteLine(largestSubArr(arr, n));
}
}
// This code is contributed by Rajput-Ji
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。