Length of longest subarray in which elements greater than K are more than elements not greater than K
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 .
- First, replace the elements which are greater than K by 1 and other elements by -1 and calculate the prefix sum over it. Now if a subarray has a sum greater than 0, it implies that it holds more elements greater than K than elements which are less than K.
- To find the answer use binary search over the answer. In each step of binary search, check every subarray of that length and then decide whether to go for larger length or not.
Below is the implementation of above Approach:
C++
#include <bits/stdc++.h> using namespace std; // C++ implementation of above approach // Function to find the length of a // longest subarray in which elements // greater than K are more than // elements not greater than K int LongestSubarray(int a[], int n, int k) { int pre[n] = { 0 }; // Create a new array in which we store 1 // if a[i] > k otherwise we store -1. for (int i = 0; i < n; i++) { if (a[i] > k) pre[i] = 1; else pre[i] = -1; } // Taking prefix sum over it for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + pre[i]; // len will store maximum // length of subarray int len = 0; int lo = 1, hi = n; while (lo <= hi) { int mid = (lo + hi) / 2; // This indicate there is at least one // subarray of length mid that has sum > 0 bool ok = false; // Check every subarray of length mid if // it has sum > 0 or not if sum > 0 then it // will satisfy our required condition for (int i = mid - 1; i < n; i++) { // x will store the sum of // subarray of length mid int x = pre[i]; if (i - mid >= 0) x -= pre[i - mid]; // Satisfy our given condition if (x > 0) { ok = true; break; } } // Check for higher length as we // get length mid if (ok == true) { len = mid; lo = mid + 1; } // Check for lower length as we // did not get length mid else hi = mid - 1; } return len; } // Driver code 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
// Java implementation of above approach
class GFG
{
// Function to find the length of a
// longest subarray in which elements
// greater than K are more than
// elements not greater than K
static int LongestSubarray(int a[], int n, int k)
{
int []pre = new int[n];
// Create a new array in which we store 1
// if a[i] > k otherwise we store -1.
for (int i = 0; i < n; i++)
{
if (a[i] > k)
pre[i] = 1;
else
pre[i] = -1;
}
// Taking prefix sum over it
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + pre[i];
// len will store maximum
// length of subarray
int len = 0;
int lo = 1, hi = n;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
// This indicate there is at least one
// subarray of length mid that has sum > 0
boolean ok = false;
// Check every subarray of length mid if
// it has sum > 0 or not if sum > 0 then it
// will satisfy our required condition
for (int i = mid – 1; i < n; i++)
{
// x will store the sum of
// subarray of length mid
int x = pre[i];
if (i - mid >= 0)
x -= pre[i – mid];
// Satisfy our given condition
if (x > 0)
{
ok = true;
break;
}
}
// Check for higher length as we
// get length mid
if (ok == true)
{
len = mid;
lo = mid + 1;
}
// Check for lower length as we
// did not get length mid
else
hi = mid – 1;
}
return len;
}
// Driver code
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of above approach # Function to find the Length of a # longest subarray in which elements # greater than K are more than # elements not greater than K def LongestSubarray(a, n, k): pre = [0 for i in range(n)] # Create a new array in which we store 1 # if a[i] > k otherwise we store -1. for i in range(n): if (a[i] > k): pre[i] = 1 else: pre[i] = -1 # Taking prefix sum over it for i in range(1, n): pre[i] = pre[i - 1] + pre[i] # Len will store maximum # Length of subarray Len = 0 lo = 1 hi = n while (lo <= hi): mid = (lo + hi) // 2 # This indicate there is at least one # subarray of Length mid that has sum > 0 ok = False # Check every subarray of Length mid if # it has sum > 0 or not if sum > 0 then it # will satisfy our required condition for i in range(mid - 1, n): # x will store the sum of # subarray of Length mid x = pre[i] if (i - mid >= 0): x -= pre[i - mid] # Satisfy our given condition if (x > 0): ok = True break # Check for higher Length as we # get Length mid if (ok == True): Len = mid lo = mid + 1 # Check for lower Length as we # did not get Length mid else: hi = mid - 1 return Len # Driver code a = [2, 3, 4, 5, 3, 7] k = 3n = len(a) print(LongestSubarray(a, n, k)) # This code is contributed by Mohit Kumar |
Output:
5
Time Complexity: O(N*logN)
Recommended Posts:
- Longest subarray in which all elements are greater than K
- Smallest subarray such that all elements are greater than K
- Longest subsequence with first and last element greater than all other elements
- Length of the longest Subarray with only Even Elements
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray having average greater than or equal to x
- Longest subarray having average greater than or equal to x | Set-2
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Find all elements in array which have at-least two greater elements
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Longest Subarray with first element greater than or equal to Last element
- Number of segments where all elements are greater than X
- Smallest subset with sum greater than all other elements
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.



