给定一个由N个整数和数字K组成的数组,任务是查找所有元素均大于K的最长子数组的长度。
例子:
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}.
这个想法是开始使用计数器遍历数组。如果当前元素大于给定值X,则增加计数器,否则将先前长度替换为先前长度和当前计数器的最大值,然后重置计数器。
下面是上述方法的实现。
C++
// C++ program to print the length of the longest
// subarray with all elements greater than X
#include
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 then 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 then 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
# then 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 then 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
$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
?>
输出:
4
时间复杂度:O(N)
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。