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) { 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 |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum length of subarray such that sum of the subarray is even
- Maximum length L such that the sum of all subarrays of length L is less than K
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximum length subarray with LCM equal to product
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Length of longest Subarray with equal number of odd and even elements
- Maximize length of subarray of equal elements by performing at most K increment operations
- Maximize length of subarray having equal elements by adding at most K
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Length of array pair formed where one contains all distinct elements and other all same elements
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum distinct prime factors of elements in a K-length subarray
- Divide array in two maximum equal length arrays of similar and dissimilar elements
- Maximum number of consecutive 1s after flipping all 0s in a K length subarray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

