给定N个整数的数组arr [] ,任务是查找包含相似元素的最大长度子数组。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 5, 5, 5, 5, 2, 2, 1, 1}
Output: 5
Explanation:
The subarray {5, 5, 5, 5, 5} has maximum length 5 with identical elements.
Input: arr[] = {1, 2, 3, 4}
Output: 1
Explanation:
All identical element subarray are {1}, {2}, {3}, and {4} which is of length 1.
方法:想法是遍历数组,存储具有相同元素的子数组的最大长度和当前长度。步骤如下:
- 遍历数组并检查当前元素是否等于下一个元素,然后增加当前length变量的值。
- 现在,将当前长度与最大长度进行比较,以更新子数组的最大长度。
- 如果当前元素不等于下一个元素,则将长度重置为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)