Length of longest subarray in which elements greater than K are more than elements not greater than K
Last Updated :
14 Mar, 2023
Given an array arr[] of length N . The task is to find the length of the longest subarray in which elements greater than a given number K are more than elements not greater than K .Examples:
Input : N = 5, K = 2, arr[]={ 1, 2, 3, 4, 1 } Output : 3 The subarray [2, 3, 4] or [3, 4, 1] satisfy the given condition, and there is no subarray of length 4 or 5 which will hold the given condition, so the answer is 3.
Input : N = 4, K = 2, arr[]={ 6, 5, 3, 4 } Output : 4
Approach:
Idea is to use the concept of binary search over Partial Sum .
Create a new array pre[] of size n, initialized to 0.
Traverse the input array a[] of size n, and for each element a[i], do the following:
If a[i] > k, set pre[i] to 1.
Else, set pre[i] to -1.
Traverse the pre[] array of size n, and compute its prefix sum in place, i.e., update each pre[i] to hold the sum of all elements from pre[0] to pre[i]. You can use the following code snippet for this:
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
Initialize lo to 1 and hi to n.
Perform binary search over the possible lengths of subarrays, i.e., while lo <= hi, do the following:
Set mid to (lo + hi) / 2.
Set ok to false.
For each subarray of length mid, check if its sum is greater than 0, i.e., if pre[i] – pre[i-mid] > 0, for some index i >= mid-1 and i < n. If such a subarray exists, set ok to true and break out of the loop.
If ok is true, update the length of the longest subarray seen so far to mid, and set lo to mid+1.
Else, set hi to mid-1.
Return the length of the longest subarray seen so far, which is stored in the variable len.
Below is the implementation of above Approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LongestSubarray(int a[], int n, int k)
{
int pre[n] = { 0 };
for (int i = 0; i < n; i++) {
if (a[i] > k)
pre[i] = 1;
else
pre[i] = -1;
}
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
int len = 0;
int lo = 1, hi = n;
while (lo <= hi) {
int mid = (lo + hi) / 2;
bool ok = false;
for (int i = mid - 1; i < n; i++) {
int x = pre[i];
if (i - mid >= 0)
x -= pre[i - mid];
if (x > 0) {
ok = true;
break;
}
}
if (ok == true) {
len = mid;
lo = mid + 1;
}
else
hi = mid - 1;
}
return len;
}
int main()
{
int a[] = { 2, 3, 4, 5, 3, 7 };
int k = 3;
int n = sizeof(a) / sizeof(a[0]);
cout << LongestSubarray(a, n, k);
return 0;
}
Java
class GFG
{
static int LongestSubarray(int a[], int n, int k)
{
int []pre = new int[n];
for (int i = 0; i < n; i++)
{
if (a[i] > k)
pre[i] = 1;
else
pre[i] = -1;
}
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
int len = 0;
int lo = 1, hi = n;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
boolean ok = false;
for (int i = mid - 1; i < n; i++)
{
int x = pre[i];
if (i - mid >= 0)
x -= pre[i - mid];
if (x > 0)
{
ok = true;
break;
}
}
if (ok == true)
{
len = mid;
lo = mid + 1;
}
else
hi = mid - 1;
}
return len;
}
public static void main(String[] args)
{
int a[] = { 2, 3, 4, 5, 3, 7 };
int k = 3;
int n = a.length;
System.out.println(LongestSubarray(a, n, k));
}
}
Python3
def LongestSubarray(a, n, k):
pre = [0 for i in range(n)]
for i in range(n):
if (a[i] > k):
pre[i] = 1
else:
pre[i] = -1
for i in range(1, n):
pre[i] = pre[i - 1] + pre[i]
Len = 0
lo = 1
hi = n
while (lo <= hi):
mid = (lo + hi) // 2
ok = False
for i in range(mid - 1, n):
x = pre[i]
if (i - mid >= 0):
x -= pre[i - mid]
if (x > 0):
ok = True
break
if (ok == True):
Len = mid
lo = mid + 1
else:
hi = mid - 1
return Len
a = [2, 3, 4, 5, 3, 7]
k = 3
n = len(a)
print(LongestSubarray(a, n, k))
C#
using System;
class GFG
{
static int LongestSubarray(int[] a, int n, int k)
{
int []pre = new int[n];
for (int i = 0; i < n; i++)
{
if (a[i] > k)
pre[i] = 1;
else
pre[i] = -1;
}
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
int len = 0;
int lo = 1, hi = n;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
bool ok = false;
for (int i = mid - 1; i < n; i++)
{
int x = pre[i];
if (i - mid >= 0)
x -= pre[i - mid];
if (x > 0)
{
ok = true;
break;
}
}
if (ok == true)
{
len = mid;
lo = mid + 1;
}
else
hi = mid - 1;
}
return len;
}
public static void Main()
{
int[] a = { 2, 3, 4, 5, 3, 7 };
int k = 3;
int n = a.Length;
Console.WriteLine(LongestSubarray(a, n, k));
}
}
Javascript
<script>
function LongestSubarray(a , n , k)
{
var pre = Array.from({length: n}, (_, i) => 0);
for (i = 0; i < n; i++)
{
if (a[i] > k)
pre[i] = 1;
else
pre[i] = -1;
}
for (i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
var len = 0;
var lo = 1, hi = n;
while (lo <= hi)
{
var mid = parseInt((lo + hi) / 2);
var ok = false;
for (i = mid - 1; i < n; i++)
{
var x = pre[i];
if (i - mid >= 0)
x -= pre[i - mid];
if (x > 0)
{
ok = true;
break;
}
}
if (ok == true)
{
len = mid;
lo = mid + 1;
}
else
hi = mid - 1;
}
return len;
}
var a = [ 2, 3, 4, 5, 3, 7 ];
var k = 3;
var n = a.length;
document.write(LongestSubarray(a, n, k));
</script>
Time Complexity: O(N*logN)Auxiliary Space: O(N)
Share your thoughts in the comments
Add Your Comment
Please Login to comment...