相邻元素之间差异为 0 或 1 的最大长度子数组
给定一个包含n 个整数的数组。任务是找到子数组的最大长度,使得子数组的所有连续元素之间的绝对差为0或1 。
例子:
Input: arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output: 3
{5, 6} and {7, 6, 5} are the only valid sub-arrays.
Input: arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output: 2
方法:从数组的第一个元素开始,找到第一个有效的子数组并存储它的长度,然后从下一个元素(第一个未包含在第一个子数组中的元素)开始,找到另一个有效的子数组大批。重复该过程,直到找到所有有效子数组,然后打印最大子数组的长度。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum length
// of the sub-array such that the
// absolute difference between every two
// consecutive elements is either 1 or 0
int getMaxLength(int arr[],int n)
{
int l = n;
int i = 0, maxlen = 0;
while (i < l)
{
int j = i;
while (i+1 < l &&
(abs(arr[i] - arr[i + 1]) == 1 ||
abs(arr[i] - arr[i + 1]) == 0))
{
i++;
}
// Length of the valid sub-array currently
// under consideration
int currLen = i - j + 1;
// Update the maximum length
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
// Any valid sub-array cannot be of length 1
//maxlen = (maxlen == 1) ? 0 : maxlen;
// Return the maximum possible length
return maxlen;
}
// Driver code
int main()
{
int arr[] = { 2, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << getMaxLength(arr, n);
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
public class GFG {
// Function to return the maximum length
// of the sub-array such that the
// absolute difference between every two
// consecutive elements is either 1 or 0
public static int getMaxLength(int arr[])
{
int l = arr.length;
int i = 0, maxlen = 0;
while (i < l) {
int j = i;
while (i + 1 < l
&& (Math.abs(arr[i] - arr[i + 1]) == 1
|| Math.abs(arr[i] - arr[i + 1]) == 0)) {
i++;
}
// Length of the valid sub-array currently
// under cosideration
int currLen = i - j + 1;
// Update the maximum length
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
// Any valid sub-array cannot be of length 1
maxlen = (maxlen == 1) ? 0 : maxlen;
// Return the maximum possible length
return maxlen;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 4 };
System.out.print(getMaxLength(arr));
}
}
Python3
# Python3 implementation of the approach
# Function to return the maximum length
# of the sub-array such that the
# absolute difference between every two
# consecutive elements is either 1 or 0
def getMaxLength(arr, n) :
l = n;
i = 0; maxlen = 0;
while (i < l) :
j = i;
while (i + 1 < l and
(abs(arr[i] - arr[i + 1]) == 1 or
abs(arr[i] - arr[i + 1]) == 0)) :
i += 1;
# Length of the valid sub-array
# currently under cosideration
currLen = i - j + 1;
# Update the maximum length
if (maxlen < currLen) :
maxlen = currLen;
if (j == i) :
i += 1;
# Any valid sub-array cannot be of length 1
# maxlen = (maxlen == 1) ? 0 : maxlen;
# Return the maximum possible length
return maxlen;
# Driver code
if __name__ == "__main__" :
arr = [ 2, 4 ];
n = len(arr)
print(getMaxLength(arr, n));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum length
// of the sub-array such that the
// Absolute difference between every two
// consecutive elements is either 1 or 0
public static int getMaxLength(int []arr)
{
int l = arr.Length;
int i = 0, maxlen = 0;
while (i < l)
{
int j = i;
while (i + 1 < l &&
(Math.Abs(arr[i] - arr[i + 1]) == 1 ||
Math.Abs(arr[i] - arr[i + 1]) == 0))
{
i++;
}
// Length of the valid sub-array currently
// under consideration
int currLen = i - j + 1;
// Update the maximum length
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
// Any valid sub-array cannot be of length 1
maxlen = (maxlen == 1) ? 0 : maxlen;
// Return the maximum possible length
return maxlen;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 2, 4 };
Console.Write(getMaxLength(arr));
}
}
// This code is contributed by Arnab Kundu
PHP
Javascript
输出:
1