Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Longest subarray gt k
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 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;
}