Longest Subarrays having each Array element as the maximum
Last Updated :
21 Aug, 2021
Given an array arr[] of length N, the task is to find the longest subarray for each array element arr[i], which contains arr[i] as the maximum.
Examples:
Input: arr[] = {1, 2, 3, 0, 1}
Output: 1 2 5 1 2
Explanation:
The longest subarray having arr[0] as the largest is {1}
The longest subarray having arr[1] as the largest is {1, 2}
The longest subarray having arr[2] as the largest is {1, 2, 3, 0, 1}
The longest subarray having arr[3] as the largest is {0}
The longest subarray having arr[4 as the largest is {0, 1}
Input: arr[] = {3, 3, 3, 1, 6, 2}
Output: 4 4 4 1 6 1
Approach: The idea is to use Two Pointer technique to solve the problem:
- Initialize two pointers, left and right. such that for every element arr[i], the left points to indices [i - 1, 0] to find elements smaller than or equal to arr[i] in a contiguous manner. The moment an element larger than arr[i] is found, the pointer stops.
- Similarly, right points to indices [i + 1, n - 1] to find elements smaller than or equal to arr[i] in a contiguous manner, and stops on finding any element larger than arr[i].
- Therefore, the largest contiguous subarray where arr[i] is largest is of length 1 + right - left.
- Repeat the above steps for every array element.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
void solve(int n, int arr[])
{
int i, ans = 0;
for (i = 0; i < n; i++) {
// Initialize the bounds
int left = max(i - 1, 0);
int right = min(n - 1, i + 1);
// Iterate to find greater
// element on the left
while (left >= 0) {
// If greater element is found
if (arr[left] > arr[i]) {
left++;
break;
}
// Decrement left pointer
left--;
}
// If boundary is exceeded
if (left < 0)
left++;
// Iterate to find greater
// element on the right
while (right < n) {
// If greater element is found
if (arr[right] > arr[i]) {
right--;
break;
}
// Increment right pointer
right++;
}
// If boundary is exceeded
if (right >= n)
right--;
// Length of longest subarray where
// arr[i] is the largest
ans = 1 + right - left;
// Print the answer
cout << ans << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 1 };
int n = sizeof arr / sizeof arr[0];
solve(n, arr);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int arr[])
{
int i, ans = 0;
for (i = 0; i < n; i++)
{
// Initialize the bounds
int left = Math.max(i - 1, 0);
int right = Math.min(n - 1, i + 1);
// Iterate to find greater
// element on the left
while (left >= 0)
{
// If greater element is found
if (arr[left] > arr[i])
{
left++;
break;
}
// Decrement left pointer
left--;
}
// If boundary is exceeded
if (left < 0)
left++;
// Iterate to find greater
// element on the right
while (right < n)
{
// If greater element is found
if (arr[right] > arr[i])
{
right--;
break;
}
// Increment right pointer
right++;
}
// If boundary is exceeded
if (right >= n)
right--;
// Length of longest subarray where
// arr[i] is the largest
ans = 1 + right - left;
// Print the answer
System.out.print(ans + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 2, 1 };
int n = arr.length;
solve(n, arr);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum length of
# Subarrays for each element of the array
# having it as the maximum
def solve(n, arr):
ans = 0
for i in range(n):
# Initialise the bounds
left = max(i - 1, 0)
right = min(n - 1, i + 1)
# Iterate to find greater
# element on the left
while left >= 0:
# If greater element is found
if arr[left] > arr[i]:
left += 1
break
# Decrement left pointer
left -= 1
# If boundary is exceeded
if left < 0:
left += 1
# Iterate to find greater
# element on the right
while right < n:
# If greater element is found
if arr[right] > arr[i]:
right -= 1
break
# Increment right pointer
right += 1
# if boundary is exceeded
if right >= n:
right -= 1
# Length of longest subarray where
# arr[i] is the largest
ans = 1 + right - left
# Print the answer
print(ans, end = " ")
# Driver code
arr = [ 4, 2, 1 ]
n = len(arr)
solve(n, arr)
# This code is contributed by Stuti Pathak
C#
// C# Program to implement
// the above approach
using System;
class GFG{
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int []arr)
{
int i, ans = 0;
for (i = 0; i < n; i++)
{
// Initialize the bounds
int left = Math.Max(i - 1, 0);
int right = Math.Min(n - 1, i + 1);
// Iterate to find greater
// element on the left
while (left >= 0)
{
// If greater element is found
if (arr[left] > arr[i])
{
left++;
break;
}
// Decrement left pointer
left--;
}
// If boundary is exceeded
if (left < 0)
left++;
// Iterate to find greater
// element on the right
while (right < n)
{
// If greater element is found
if (arr[right] > arr[i])
{
right--;
break;
}
// Increment right pointer
right++;
}
// If boundary is exceeded
if (right >= n)
right--;
// Length of longest subarray where
// arr[i] is the largest
ans = 1 + right - left;
// Print the answer
Console.Write(ans + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, 2, 1 };
int n = arr.Length;
solve(n, arr);
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
function solve(n, arr)
{
let i, ans = 0;
for (i = 0; i < n; i++)
{
// Initialize the bounds
let left = Math.max(i - 1, 0);
let right = Math.min(n - 1, i + 1);
// Iterate to find greater
// element on the left
while (left >= 0)
{
// If greater element is found
if (arr[left] > arr[i])
{
left++;
break;
}
// Decrement left pointer
left--;
}
// If boundary is exceeded
if (left < 0)
left++;
// Iterate to find greater
// element on the right
while (right < n)
{
// If greater element is found
if (arr[right] > arr[i])
{
right--;
break;
}
// Increment right pointer
right++;
}
// If boundary is exceeded
if (right >= n)
right--;
// Length of longest subarray where
// arr[i] is the largest
ans = 1 + right - left;
// Print the answer
document.write(ans + " ");
}
}
// Driver Code
let arr = [ 4, 2, 1 ];
let n = arr.length;
solve(n, arr);
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Longest subarray having maximum sum Given an array arr[] containing n integers. The problem is to find the length of the subarray having maximum sum. If there exists two or more subarrays with maximum sum then print the length of the longest subarray.Examples: Input : arr[] = {5, -2, -1, 3, -4}Output : 4There are two subarrays with ma
12 min read
Subarray with largest sum after excluding its maximum element Given an array arr[], the task is to find the starting and ending indices of the subarray with the largest sum after excluding its maximum element.Examples: Input: arr[] = {5, -2, 10, -1, 4} Output: 1 5 Explanation: Subarray[1:5] = {5, -2, 10, -1, 4} Sum of subarray excluding maximum element = 5 + (
9 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
Longest subarray not having more than K distinct elements Given N elements and a number K, find the longest subarray which has not more than K distinct elements.(It can have less than K). Examples: Input : arr[] = {1, 2, 3, 4, 5} k = 6 Output : 1 2 3 4 5 Explanation: The whole array has only 5 distinct elements which is less than k, so we print the array i
8 min read
Longest Sub-array with maximum average value Given an array arr[] of n integers. The task is to find the maximum length of the sub-array which has the maximum average value (average of the elements of the sub-array). Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 1 {6} is the required sub-arrayInput: arr[] = {6, 1, 6, 6, 0} Output: 2 {6} and
6 min read
Longest subarray with all elements same Given an array arr[] of size N, the task is to find the largest subarray which consists of all equal elements.Examples: Input: arr[] = {1, 1, 2, 2, 2, 3, 3}; Output: 3 Explanation: Longest subarray with equal elements is {2, 2, 2}Input: arr[] = {1, 1, 2, 2, 2, 3, 3, 3, 3}; Output: 4 Explanation: Lon
4 min read
Longest subarray in which all elements are smaller than K Given an array arr[] consisting of N integers and an integer K, the task is to find the length of the longest subarray in which all the elements are smaller than K. Constraints:0 <= arr[i] Â <= 10^5 Examples:Â Input: arr[] = {1, 8, 3, 5, 2, 2, 1, 13}, K = 6Output: 5Explanation:There is one poss
11 min read
Maximum length of subarray such that all elements are equal in the subarray 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,
5 min read
Maximum frequencies in each M-length subarray Given an array arr[] consisting of N integers and a positive integer M, the task is to find the maximum frequency for each M-length subarray ( 0 < M ? N). Examples: Input: arr[] = {1, 2, 3, 1, 2, 4, 1, 4, 4}, M = 4Output: 2 2 1 2 2 3Explanation:All the M length sub-arrays with the maximum frequen
8 min read