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++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, int arr[])
{
int i, ans = 0;
for (i = 0; i < n; i++) {
int left = max(i - 1, 0);
int right = min(n - 1, i + 1);
while (left >= 0) {
if (arr[left] > arr[i]) {
left++;
break ;
}
left--;
}
if (left < 0)
left++;
while (right < n) {
if (arr[right] > arr[i]) {
right--;
break ;
}
right++;
}
if (right >= n)
right--;
ans = 1 + right - left;
cout << ans << " " ;
}
}
int main()
{
int arr[] = { 4, 2, 1 };
int n = sizeof arr / sizeof arr[0];
solve(n, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int n, int arr[])
{
int i, ans = 0 ;
for (i = 0 ; i < n; i++)
{
int left = Math.max(i - 1 , 0 );
int right = Math.min(n - 1 , i + 1 );
while (left >= 0 )
{
if (arr[left] > arr[i])
{
left++;
break ;
}
left--;
}
if (left < 0 )
left++;
while (right < n)
{
if (arr[right] > arr[i])
{
right--;
break ;
}
right++;
}
if (right >= n)
right--;
ans = 1 + right - left;
System.out.print(ans + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 1 };
int n = arr.length;
solve(n, arr);
}
}
|
Python3
def solve(n, arr):
ans = 0
for i in range (n):
left = max (i - 1 , 0 )
right = min (n - 1 , i + 1 )
while left > = 0 :
if arr[left] > arr[i]:
left + = 1
break
left - = 1
if left < 0 :
left + = 1
while right < n:
if arr[right] > arr[i]:
right - = 1
break
right + = 1
if right > = n:
right - = 1
ans = 1 + right - left
print (ans, end = " " )
arr = [ 4 , 2 , 1 ]
n = len (arr)
solve(n, arr)
|
C#
using System;
class GFG{
static void solve( int n, int []arr)
{
int i, ans = 0;
for (i = 0; i < n; i++)
{
int left = Math.Max(i - 1, 0);
int right = Math.Min(n - 1, i + 1);
while (left >= 0)
{
if (arr[left] > arr[i])
{
left++;
break ;
}
left--;
}
if (left < 0)
left++;
while (right < n)
{
if (arr[right] > arr[i])
{
right--;
break ;
}
right++;
}
if (right >= n)
right--;
ans = 1 + right - left;
Console.Write(ans + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 4, 2, 1 };
int n = arr.Length;
solve(n, arr);
}
}
|
Javascript
<script>
function solve(n, arr)
{
let i, ans = 0;
for (i = 0; i < n; i++)
{
let left = Math.max(i - 1, 0);
let right = Math.min(n - 1, i + 1);
while (left >= 0)
{
if (arr[left] > arr[i])
{
left++;
break ;
}
left--;
}
if (left < 0)
left++;
while (right < n)
{
if (arr[right] > arr[i])
{
right--;
break ;
}
right++;
}
if (right >= n)
right--;
ans = 1 + right - left;
document.write(ans + " " );
}
}
let arr = [ 4, 2, 1 ];
let n = arr.length;
solve(n, arr);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...