给定一个整数的数组arr [] 。任务是返回最大大小子数组的长度,以便满足以下任一条件:
- ARR [K]>的常用3 [K + 1]当k是奇数且常用3 [K]
当k是偶数。 - ARR [K]>的常用3 [K + 1]当k是偶数和的常用3 [k]的当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 [] = {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
?>
输出:
5
时间复杂度: O(N),其中N是数组的长度
空间复杂度: O(1)