diff --git a/Longest subarray gt k b/Longest subarray gt k new file mode 100644 index 0000000..6513b23 --- /dev/null +++ b/Longest subarray gt k @@ -0,0 +1,47 @@ +// 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; +}