Find subarray of Length K with Maximum Peak
Last Updated :
08 May, 2023
Given an array arr[] of length n and a positive integer K, we have to find a subarray of length K which has maximum peak inside in it.
Peaks of the segment [l, r] are those indexes such that l < i < r, a[i-1] < a[i] and a[i+1] < a[i].
Note: The boundary indexes l and r for the segment are not peaks. If there are many subarrays with maximum peak, then print that subarray whose has minimum left index.
Examples:
Input :
arr = {3, 1, 4, 1, 5, 9, 2, 6}, k = 7
Output:
Left = 1
Right = 7
Peak = 2
Explanation:
There are two subarray with length 7 i.e [1, 7] and [2, 8]. Both subarray has 2 peak inside it i.e 3 and 6 index are the peak in both the subarray. We have to return the subarray with minimum l and maximum peak i.e l = 1 and peak = 2.
Input:
arr = {3, 2, 3, 2, 1}, k = 3
Output :
Left = 2
Right = 4
Peak = 1
Explanation:
Only one subarray whose length is 3 and number of peak inside it is 1 i.e. l =2 and peak is i = 3.
Approach:
The approach to solve this problem is to use a sliding window, where we slide across window size of K, and find the total count of peaks in every window, whichever windows gives the maximum number of peaks will be the answer. While moving the right index, we check if an index added is a peak, we increase the count, and while moving the left index, we check if the removed index is a peak, if it is, then decrease the count. We have a window of size K always.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSubArray( int * a, int n, int k)
{
int pref[n];
pref[0] = 0;
for ( int i = 1; i < n - 1; ++i) {
pref[i] = pref[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1])
pref[i]++;
}
int peak = 0, left = 0;
for ( int i = 0; i + k - 1 < n; ++i)
if (pref[i + k - 2] - pref[i] > peak) {
peak = pref[i + k - 2] - pref[i];
left = i;
}
cout << "Left = " << left + 1 << endl;
cout << "Right = " << left + k << endl;
cout << "Peak = " << peak << endl;
}
int main()
{
int arr[] = { 3, 2, 3, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 3;
findSubArray(arr, n, k);
return 0;
}
|
Java
class GFG{
static void findSubArray( int []a, int n, int k)
{
int []pref = new int [n];
pref[ 0 ] = 0 ;
for ( int i = 1 ; i < n - 1 ; ++i) {
pref[i] = pref[i - 1 ];
if (a[i] > a[i - 1 ] && a[i] > a[i + 1 ])
pref[i]++;
}
int peak = 0 , left = 0 ;
for ( int i = 0 ; i + k - 1 < n; ++i)
if (pref[i + k - 2 ] - pref[i] > peak) {
peak = pref[i + k - 2 ] - pref[i];
left = i;
}
System.out.print( "Left = " + (left + 1 ) + "\n" );
System.out.print( "Right = " + (left + k) + "\n" );
System.out.print( "Peak = " + peak + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 3 , 2 , 1 };
int n = arr.length;
int k = 3 ;
findSubArray(arr, n, k);
}
}
|
Python3
def findSubArray(a, n, k):
pref = [ 0 for i in range (n)]
pref[ 0 ] = 0
for i in range ( 1 , n - 1 , 1 ):
pref[i] = pref[i - 1 ]
if (a[i] > a[i - 1 ] and a[i] > a[i + 1 ]):
pref[i] + = 1
peak = 0
left = 0
for i in range ( 0 , n - k + 1 , 1 ):
if (pref[i + k - 2 ] - pref[i] > peak):
peak = pref[i + k - 2 ] - pref[i]
left = i
print ( "Left =" ,left + 1 )
print ( "Right =" ,left + k)
print ( "Peak =" ,peak)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 3 , 2 , 1 ]
n = len (arr)
k = 3
findSubArray(arr, n, k)
|
C#
using System;
class GFG{
static void findSubArray( int []a, int n, int k)
{
int []pref = new int [n];
pref[0] = 0;
for ( int i = 1; i < n - 1; ++i)
{
pref[i] = pref[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1])
{
pref[i]++;
}
}
int peak = 0;
int left = 0;
for ( int i = 0; i + k - 1 < n; ++i)
{
if (pref[i + k - 2] - pref[i] > peak)
{
peak = pref[i + k - 2] - pref[i];
left = i;
}
}
Console.Write( "Left = " + (left + 1) + "\n" );
Console.Write( "Right = " + (left + k) + "\n" );
Console.Write( "Peak = " + peak + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 3, 2, 1 };
int n = arr.Length;
int k = 3;
findSubArray(arr, n, k);
}
}
|
Javascript
<script>
function findSubArray(a, n, k)
{
let pref = new Array(n);
pref[0] = 0;
for (let i = 1; i < n - 1; ++i)
{
pref[i] = pref[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1])
pref[i]++;
}
let peak = 0, left = 0;
for (let i = 0; i + k - 1 < n; ++i)
if (pref[i + k - 2] - pref[i] > peak) {
peak = pref[i + k - 2] - pref[i];
left = i;
}
document.write( "Left = " + (left + 1) + "<br>" );
document.write( "Right = " + (left + k) + "<br>" );
document.write( "Peak = " + peak + "<br>" );
}
let arr = [3, 2, 3, 2, 1];
let n = arr.length;
let k = 3;
findSubArray(arr, n, k);
</script>
|
Output:
Left = 2
Right = 4
Peak = 1
Time Complexity: O(N)
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...