Longest subarray in which all elements are greater than K
Last Updated :
07 Sep, 2022
Given an array of N integers and a number K, the task is to find the length of the longest subarray in which all the elements are greater than K.
Examples:
Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5
Output: 2
There are two possible longest subarrays of length 2.
They are {6, 7} and {10, 11}.
Input: a[] = {8, 25, 10, 19, 19, 18, 20, 11, 18}, K = 13
Output: 4
The longest subarray is {19, 19, 18, 20}.
The idea is to start traversing the array using a counter. If the current element is greater than the given value X, increment the counter otherwise replace the previous length with the maximum of the previous length and current counter and reset the counter.
Below is the implementation of the above approach.
C++
// C++ program to print the length of the longest
// subarray with all elements greater than X
#include <bits/stdc++.h>
using namespace std;
// Function to count number of segments
int longestSubarray(int a[], int n, int x)
{
int count = 0;
int length = 0;
// Iterate in the array
for (int i = 0; i < n; i++) {
// check if array element
// greater than X or not
if (a[i] > x) {
count += 1;
}
else {
length = max(length, count);
count = 0;
}
}
// After iteration complete
// check for the last segment
if (count)
length = max(length, count);
return length;
}
// Driver Code
int main()
{
int a[] = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };
int n = sizeof(a) / sizeof(a[0]);
int k = 13;
cout << longestSubarray(a, n, k);
return 0;
}
Java
// Java program to print the length of the longest
// subarray with all elements greater than X
import java.io.*;
class GFG {
// Function to count number of segments
static int longestSubarray(int a[], int n, int x)
{
int count = 0;
int length = 0;
// Iterate in the array
for (int i = 0; i < n; i++) {
// check if array element
// greater than X or not
if (a[i] > x) {
count += 1;
}
else {
length = Math.max(length, count);
count = 0;
}
}
// After iteration complete
// check for the last segment
if (count>0)
length = Math.max(length, count);
return length;
}
// Driver Code
public static void main (String[] args) {
int []a = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };
int n = a.length;
int k = 13;
System.out.println(longestSubarray(a, n, k));
}
}
// This Code is contributed
// by shs
Python3
# Python3 program to print the length of
# the longest subarray with all elements
# greater than X
# Function to count number of segments
def longestSubarray(a, n, x):
count = 0
length = 0
# Iterate in the array
for i in range(n):
# check if array element greater
# than X or not
if (a[i] > x):
count += 1
else:
length = max(length, count)
count = 0
# After iteration complete
# check for the last segment
if (count > 0):
length = max(length, count)
return length
# Driver Code
if __name__ == '__main__':
a = [ 8, 25, 10, 19, 19,
18, 20, 11, 18 ]
n = len(a)
k = 13
print(longestSubarray(a, n, k))
# This code is contributed by 29AjayKumar
C#
// C# program to print the length of the longest
// subarray with all elements greater than X
using System;
class GFG {
// Function to count number of segments
static int longestSubarray(int []a, int n, int x)
{
int count = 0;
int length = 0;
// Iterate in the array
for (int i = 0; i < n; i++) {
// check if array element
// greater than X or not
if (a[i] > x) {
count += 1;
}
else {
length = Math.Max(length, count);
count = 0;
}
}
// After iteration complete
// check for the last segment
if (count>0)
length = Math.Max(length, count);
return length;
}
// Driver Code
public static void Main () {
int []a = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };
int n = a.Length;
int k = 13;
Console.WriteLine(longestSubarray(a, n, k));
}
}
// This Code is contributed
// by shs
PHP
<?php
// PHP program to print the length
// of the longest subarray with all
// elements greater than X
// Function to count number of segments
function longestSubarray($a, $n, $x)
{
$count = 0;
$length = 0;
// Iterate in the array
for ($i = 0; $i < $n; $i++)
{
// check if array element
// greater than X or not
if ($a[$i] > $x)
{
$count += 1;
}
else
{
$length = max($length, $count);
$count = 0;
}
}
// After iteration complete
// check for the last segment
if ($count > 0)
$length = max($length, $count);
return $length;
}
// Driver Code
$a = array( 8, 25, 10, 19, 19,
18, 20, 11, 18 );
$n = 9;
$k = 13;
echo longestSubarray($a, $n, $k);
// This code is contributed
// by Arnab Kundu
?>
JavaScript
<script>
// javascript program to print the length of the longest
// subarray with all elements greater than X
// Function to count number of segments
function longestSubarray(a , n , x) {
var count = 0;
var length = 0;
// Iterate in the array
for (i = 0; i < n; i++) {
// check if array element
// greater than X or not
if (a[i] > x) {
count += 1;
} else {
length = Math.max(length, count);
count = 0;
}
}
// After iteration complete
// check for the last segment
if (count > 0)
length = Math.max(length, count);
return length;
}
// Driver Code
var a = [ 8, 25, 10, 19, 19, 18, 20, 11, 18 ];
var n = a.length;
var k = 13;
document.write(longestSubarray(a, n, k));
// This code is contributed by todaysgaurav
</script>
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Similar Reads
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
Longest subarray in which all elements are a factor of K Given an array A[] of size N and a positive integer K, the task is to find the length of the longest subarray such that all elements of the subarray is a factor of K. Examples: Input: A[] = {2, 8, 3, 10, 6, 7, 4, 9}, K = 60Output: 3Explanation: The longest subarray in which all elements are a factor
6 min read
Smallest subarray such that all elements are greater than K Given an array of N integers and a number K, the task is to find the length of the smallest subarray in which all the elements are greater than K. If there is no such subarray possible, then print -1. Examples: Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5 Output: 1 The subarray is {10} Input: a[]
4 min read
Longest Subarray having Majority Elements Greater Than K Given an array arr[] and an integer k, the task is to find the length of longest subarray in which the count of elements greater than k is more than the count of elements less than or equal to k.Examples:Input: arr[]= [1, 2, 3, 4, 1], k = 2Output: 3 Explanation: The subarray [2, 3, 4] or [3, 4, 1] s
13 min read
Longest subarray with GCD greater than 1 Given an array arr[] consisting of N integers, the task is to find the maximum length of subarray having the Greatest Common Divisor (GCD) of all the elements greater than 1. Examples: Input: arr[] = {4, 3, 2, 2}Output: 2Explanation:Consider the subarray {2, 2} having GCD as 2(> 1) which is of ma
15+ min read