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++
#include <bits/stdc++.h>
using namespace std;
int longestSubarray( int a[], int n, int x)
{
int count = 0;
int length = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > x) {
count += 1;
}
else {
length = max(length, count);
count = 0;
}
}
if (count)
length = max(length, count);
return length;
}
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
import java.io.*;
class GFG {
static int longestSubarray( int a[], int n, int x)
{
int count = 0 ;
int length = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] > x) {
count += 1 ;
}
else {
length = Math.max(length, count);
count = 0 ;
}
}
if (count> 0 )
length = Math.max(length, count);
return length;
}
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));
}
}
|
Python3
def longestSubarray(a, n, x):
count = 0
length = 0
for i in range (n):
if (a[i] > x):
count + = 1
else :
length = max (length, count)
count = 0
if (count > 0 ):
length = max (length, count)
return length
if __name__ = = '__main__' :
a = [ 8 , 25 , 10 , 19 , 19 ,
18 , 20 , 11 , 18 ]
n = len (a)
k = 13
print (longestSubarray(a, n, k))
|
C#
using System;
class GFG {
static int longestSubarray( int []a, int n, int x)
{
int count = 0;
int length = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > x) {
count += 1;
}
else {
length = Math.Max(length, count);
count = 0;
}
}
if (count>0)
length = Math.Max(length, count);
return length;
}
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));
}
}
|
PHP
<?php
function longestSubarray( $a , $n , $x )
{
$count = 0;
$length = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $x )
{
$count += 1;
}
else
{
$length = max( $length , $count );
$count = 0;
}
}
if ( $count > 0)
$length = max( $length , $count );
return $length ;
}
$a = array ( 8, 25, 10, 19, 19,
18, 20, 11, 18 );
$n = 9;
$k = 13;
echo longestSubarray( $a , $n , $k );
?>
|
Javascript
<script>
function longestSubarray(a , n , x) {
var count = 0;
var length = 0;
for (i = 0; i < n; i++) {
if (a[i] > x) {
count += 1;
} else {
length = Math.max(length, count);
count = 0;
}
}
if (count > 0)
length = Math.max(length, count);
return length;
}
var a = [ 8, 25, 10, 19, 19, 18, 20, 11, 18 ];
var n = a.length;
var k = 13;
document.write(longestSubarray(a, n, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Sep, 2022
Like Article
Save Article