📌  相关文章
📜  最长子数组的长度,使得相邻元素之间的差异为 K

📅  最后修改于: 2022-05-13 01:57:51.707000             🧑  作者: Mango

最长子数组的长度,使得相邻元素之间的差异为 K

给定一个大小为N的数组arr[]和整数K 。任务是找到最长子数组的长度,其中相邻元素之间的差异为K

例子:

方法:从数组的第一个元素开始,找到第一个有效的子数组并存储它的长度,然后从下一个元素(第一个不包含在第一个子数组中的元素)开始,找到另一个有效的子数组大批。重复该过程,直到找到所有有效子数组,然后打印最长子数组的长度。

下面是上述方法的实现:

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 K
int getMaxLength(int arr[], int N, int K)
{
    int l = N;
    int i = 0, maxlen = 0;
    while (i < l) {
        int j = i;
        while (i + 1 < l
               && (abs(arr[i] -
                       arr[i + 1]) == K)) {
            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++;
    }
 
    // Return the maximum possible length
    return maxlen;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 4, 6 };
    int K = 1;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getMaxLength(arr, N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
// Function to return the maximum length
// of the sub-array such that the
// absolute difference between every two
// consecutive elements is K
static int getMaxLength(int arr[], int N, int K)
{
    int l = N;
    int i = 0, maxlen = 0;
    while (i < l) {
        int j = i;
        while (i + 1 < l
               && (Math.abs(arr[i] -
                       arr[i + 1]) == K)) {
            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++;
    }
 
    // Return the maximum possible length
    return maxlen;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, 2, 4, 6 };
    int K = 1;
    int N =  arr.length; 
    System.out.print(getMaxLength(arr, N, K));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python 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 K
def getMaxLength (arr, N, K):
    l = N;
    i = 0
    maxlen = 0;
    while (i < l):
        j = i;
        while (i + 1 < l and (abs(arr[i] - arr[i + 1]) == K)):
            i += 1
 
        # Length of the valid sub-array
        # currently under consideration
        currLen = i - j + 1;
 
        # Update the maximum length
        if (maxlen < currLen):
            maxlen = currLen;
 
        if (j == i):
            i += 1
 
    # Return the maximum possible length
    return maxlen;
 
# Driver code
arr = [2, 2, 4, 6];
K = 1;
N = len(arr)
print(getMaxLength(arr, N, K));
 
# This code is contributed by gfgking


C#
// C# program for the above 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 K
  static int getMaxLength(int []arr, int N, int K)
  {
    int l = N;
    int i = 0, maxlen = 0;
    while (i < l) {
      int j = i;
      while (i + 1 < l
             && (Math.Abs(arr[i] -
                          arr[i + 1]) == K)) {
        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++;
    }
 
    // Return the maximum possible length
    return maxlen;
  }
 
  // Driver Code
  public static void Main()
  {
    int []arr = { 2, 2, 4, 6 };
    int K = 1;
    int N =  arr.Length;
    Console.Write(getMaxLength(arr, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1

时间复杂度: O(N)
辅助空间: O(1)