📌  相关文章
📜  子数组的最大长度,使得子数组中的所有元素都相等

📅  最后修改于: 2021-09-04 13:09:38             🧑  作者: Mango

给定一个由 N 个整数组成的数组arr[] ,任务是找到包含相似元素的最大长度子数组。
例子:

处理方法:思想是遍历数组存储具有相同元素的子数组的最大长度当前长度。以下是步骤:

  1. 遍历数组并检查当前元素是否等于下一个元素,然后增加当前长度变量的值。
  2. 现在,将当前长度与最大长度进行比较以更新子数组的最大长度。
  3. 如果当前元素不等于下一个元素,则将长度重置为1 ,并对数组的所有元素继续此操作。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the longest
// subarray with same element
int longest_subarray(int arr[], int d)
{
 
    int i = 0, j = 1, e = 0;
 
    for (i = 0; i < d - 1; i++) {
 
        // Check if the elements
        // are same then we can
        // increment the length
        if (arr[i] == arr[i + 1]) {
            j = j + 1;
        }
        else {
 
            // Reinitialize j
            j = 1;
        }
 
        // Compare the maximum
        // length e with j
        if (e < j) {
            e = j;
        }
    }
 
    // Return max length
    return e;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << longest_subarray(arr, N);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the longest
// subarray with same element
static int longest_subarray(int arr[], int d)
{
    int i = 0, j = 1, e = 0;
 
    for(i = 0; i < d - 1; i++)
    {
         
       // Check if the elements
       // are same then we can
       // increment the length
       if (arr[i] == arr[i + 1])
       {
           j = j + 1;
       }
       else
       {
            
           // Reinitialize j
           j = 1;
       }
        
       // Compare the maximum
       // length e with j
       if (e < j)
       {
           e = j;
       }
    }
 
    // Return max length
    return e;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4 };
    int N = arr.length;
 
    // Function Call
    System.out.print(longest_subarray(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the longest
# subarray with same element
def longest_subarray(arr, d):
     
    (i, j, e) = (0, 1, 0)
     
    for i in range(d - 1):
         
        # Check if the elements
        # are same then we can
        # increment the length
        if arr[i] == arr[i + 1]:
            j += 1
        else:
             
            # Reinitialize j
            j = 1
         
        # Compare the maximum
        # length e with j
        if e < j:
            e = j
     
    # Return max length
    return e
 
# Driver code
 
# Given list arr[]
arr = [ 1, 2, 3, 4 ]
N = len(arr)
 
# Function call
print(longest_subarray(arr, N))
 
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
 
class GFG{
  
// Function to find the longest
// subarray with same element
static int longest_subarray(int []arr, int d)
{
    int i = 0, j = 1, e = 0;
  
    for(i = 0; i < d - 1; i++)
    {
          
       // Check if the elements
       // are same then we can
       // increment the length
       if (arr[i] == arr[i + 1])
       {
           j = j + 1;
       }
       else
       {
             
           // Reinitialize j
           j = 1;
       }
         
       // Compare the maximum
       // length e with j
       if (e < j)
       {
           e = j;
       }
    }
  
    // Return max length
    return e;
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array []arr
    int []arr = { 1, 2, 3, 4 };
    int N = arr.Length;
  
    // Function Call
    Console.Write(longest_subarray(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live