Maximum length of subarray such that all elements are equal in the subarray
Last Updated :
18 May, 2023
Given an array arr[] of N integers, the task is to find the maximum length subarray that contains similar elements.
Examples:
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.
Approach: The idea is to traverse the array store the maximum length and current length of the subarray which has the same elements. Below are the steps:
- Traverse the array and check if the current element is equal to the next element then increase the value of the current length variable.
- Now, compare the current length with max length to update the maximum length of the subarray.
- If the current element is not equal to the next element then reset the length to 1 and continue this for all the elements of the array.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the longest
// subarray with same element
int longest_subarray(int arr[], int d)
{
if(d <= 1) return 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)
{
if (d <= 1)
return 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):
if d <= 1:
return 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)
{
if (d <= 1)
return 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
<script>
// javascript program for the above approach
// Function to find the longest
// subarray with same element
function longest_subarray(arr , d)
{
if(d <= 1) return d;
var 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
// Given array arr
var arr = [ 1, 2, 3, 4 ];
var N = arr.length;
// Function Call
document.write(longest_subarray(arr, N));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum length L such that the sum of all subarrays of length L is less than K Given an array of length N and an integer K. The task is to find the maximum length L such that all the subarrays of length L have sum of its elements less than K.Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 20 Output: 5 The only subarray of length 5 is the complete array and (1 + 2 + 3 + 4 + 5) =
8 min read
Split array into maximum subarrays such that every distinct element lies in a single subarray Given an array, arr[] of size N, the task is to split the array into the maximum number of subarrays such that the first and the last occurrence of all distinct array element lies in a single subarray. Examples: Input: arr[] = {1, 1, 2, 2}Output: 2Explanation:Split the array into subarrays {1, 1} an
6 min read
First subarray having sum at least half the maximum sum of any subarray of size K Given an array arr[] and an integer K, the task is to find the first subarray which has a sum greater than or equal to half of the maximum possible sum from any subarray of size K. Examples: Input: arr[] = {2, 4, 5, 1, 4, 6, 6, 2, 1, 0}, K = 3 Output: 6 2 1 Explanation: The given array has a maximum
9 min read
Maximum subarray size having all subarrays sums less than k Given an array of positive integers arr[] of size n, and an integer k. The task is to find the maximum subarray size such that all subarrays of that size have sum less than or equals to k.Examples : Input : arr[] = [1, 2, 3, 4], k = 8.Output : 2Explanation: Following are the sum of subarray of size
15+ min read
Maximum sum of subarrays having distinct elements of length K Given an array, arr[] and a value k, represent the length of the subarray to be considered. Find the maximum sum that can be obtained from the subarray of length k such that each element of the subarray is unique. If there is no subarray that meets the required condition then return 0. Examples: Inp
13 min read
Maximize length of subarray having equal elements by adding at most K Given an array arr[] consisting of N positive integers and an integer K, which represents the maximum number that can be added to the array elements. The task is to maximize the length of the longest possible subarray of equal elements by adding at most K. Examples: Input: arr[] = {3, 0, 2, 2, 1}, k
9 min read