给定一个整数数组arr[] 。任务是返回满足任一条件的最大大小子数组的长度:
- arr[k] > arr[k + 1]当k 为奇数时, arr[k] < arr[k + 1]当k 为偶数时。
- arr[k] > arr[k + 1]当k 是偶数时arr[k] < arr[k + 1]当k 是奇数时。
例子:
Input: arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9}
Output: 5
The required sub-array is {4, 2, 10, 7, 8} which satisfies the first condition.
Input: arr[] = {4, 8, 12, 16}
Output: 2
方法:因为只需要相邻元素之间的比较。因此,如果比较要由-1, 0, 1表示,那么我们需要最长的交替1, -1, 1, …, -1序列(从 1 或 -1 开始),其中:
- 0 -> arr[i] = arr[i + 1]
- 1 -> arr[i] > arr[i + 1]
- -1 -> arr[i] < arr[i + 1]
例如,如果我们有一个数组arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9}那么比较将是{1, 1, -1, 1, -1, 0 , -1, 1}和所有可能的子数组是{1} , {1, -1, 1, -1} , {0} , {-1, 1} 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that compares a and b
int cmp(int a, int b)
{
return (a > b) - (a < b);
}
// Function to return length of longest subarray
// that satisfies one of the given conditions
int maxSubarraySize(int arr[], int n)
{
int ans = 1;
int anchor = 0;
for (int i = 1; i < n; i++)
{
int c = cmp(arr[i - 1], arr[i]);
if (c == 0)
anchor = i;
else if (i == n - 1 ||
c * cmp(arr[i], arr[i + 1]) != -1)
{
ans = max(ans, i - anchor + 1);
anchor = i;
}
}
return ans;
}
// Driver Code
int main()
{
int arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = sizeof(arr) / sizeof(arr[0]);
// Print the required answer
cout << maxSubarraySize(arr, n);
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
class GFG
{
// Function that compares a and b
static int cmp(int a, int b)
{
if(a > b)
return 1;
else if(a == b)
return 0;
else
return -1;
}
// Function to return length of longest subarray
// that satisfies one of the given conditions
static int maxSubarraySize(int []arr, int n)
{
int ans = 1;
int anchor = 0;
for (int i = 1; i < n; i++)
{
int c = cmp(arr[i - 1], arr[i]);
if (c == 0)
anchor = i;
else if (i == n - 1 ||
c * cmp(arr[i], arr[i + 1]) != -1)
{
ans = Math.max(ans, i - anchor + 1);
anchor = i;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = arr.length;
// Print the required answer
System.out.println(maxSubarraySize(arr, n));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function that compares a and b
def cmp(a, b):
return (a > b) - (a < b)
# Function to return length of longest subarray
# that satisfies one of the given conditions
def maxSubarraySize(arr):
N = len(arr)
ans = 1
anchor = 0
for i in range(1, N):
c = cmp(arr[i - 1], arr[i])
if c == 0:
anchor = i
elif i == N - 1 or c * cmp(arr[i], arr[i + 1]) != -1:
ans = max(ans, i - anchor + 1)
anchor = i
return ans
# Driver program
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
# Print the required answer
print(maxSubarraySize(arr))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that compares a and b
static int cmp(int a, int b)
{
if(a > b)
return 1;
else if(a == b)
return 0;
else
return -1;
}
// Function to return length of longest subarray
// that satisfies one of the given conditions
static int maxSubarraySize(int []arr, int n)
{
int ans = 1;
int anchor = 0;
for (int i = 1; i < n; i++)
{
int c = cmp(arr[i - 1], arr[i]);
if (c == 0)
anchor = i;
else if (i == n - 1 ||
c * cmp(arr[i], arr[i + 1]) != -1)
{
ans = Math.Max(ans, i - anchor + 1);
anchor = i;
}
}
return ans;
}
// Driver Code
static void Main()
{
int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = arr.Length;
// Print the required answer
Console.WriteLine(maxSubarraySize(arr, n));
}
}
// This code is contributed by mits
PHP
$b) - ($a < $b);
}
// Function to return length of longest subarray
// that satisfies one of the given conditions
function maxSubarraySize($arr)
{
$N = sizeof($arr);
$ans = 1;
$anchor = 0;
for ($i = 1; $i < $N; $i++)
{
$c = cmp($arr[$i - 1], $arr[$i]);
if ($c == 0)
$anchor = $i;
else if ($i == $N - 1 or
$c * cmp($arr[$i],
$arr[$i + 1]) != -1)
{
$ans = max($ans, $i - $anchor + 1);
$anchor = $i;
}
}
return $ans;
}
// Driver Code
$arr = array(9, 4, 2, 10, 7, 8, 8, 1, 9);
// Print the required answer
echo maxSubarraySize($arr);
// This code is contributed by Ryuga
?>
Javascript
输出:
5
时间复杂度: O(N),其中 N 是数组的长度
空间复杂度: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。