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[] = {1, 2, 3}, K = 13
Output: -1
Approach: The task is to find the smallest subarray with all elements greater than K. Since smallest subarray can be of size 1. So, just check if there exists any element in the array greater than K. If yes then print “1” else print “-1”.
Below is the implementation of the above approach:
C++
// C++ program to print the length of the shortest // subarray with all elements greater than X #include <bits/stdc++.h> using namespace std; // Function to return shortest array int smallestSubarray(int a[], int n, int x) { int count = 0, 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) { return 1; } } return -1; } // Driver Code int main() { int a[] = { 1, 22, 3 }; int n = sizeof(a) / sizeof(a[0]); int k = 13; cout << smallestSubarray(a, n, k); return 0; } |
Java
// Java program to print the length of the shortest // subarray with all elements greater than X import java.io.*; class GFG { // Function to return shortest array static int smallestSubarray(int a[], int n, int x) { int count = 0, 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) { return 1; } } return -1; } // Driver Code public static void main (String[] args) { int a[] = { 1, 22, 3 }; int n = a.length; int k = 13; System.out.println(smallestSubarray(a, n, k)); } } // This code has been contributed by anuj_67.. |
Python3
# Python 3 program to print the # length of the shortest subarray # with all elements greater than X # Function to return shortest array def smallestSubarray(a, n, k): # Iterate in the array for i in range(n): # check if array element # greater then X or not if a[i] > k: return 1 return -1 # Driver Code a = [1, 22, 3] n = len(a) k = 13print(smallestSubarray(a, n, k)) # This code is contributed # by Shrikant13 |
C#
using System; class GFG { // Function to return shortest array static int smallestSubarray(int []a, int n, int x) { // Iterate in the array for (int i = 0; i < n; i++) { // check if array element // greater then X or not if (a[i] > x) { return 1; } } return -1; } // Driver Code static public void Main () { int []a = { 1, 22, 3 }; int n = a.Length; int k = 13; Console.WriteLine(smallestSubarray(a, n, k)); } } // This code is contributed by ajit |
PHP
<?php // PHP program to print the length // of the shortest subarray with // all elements greater than X // Function to return shortest array function smallestSubarray($a, $n, $x) { $count = 0; $length = 0; // Iterate in the array for ($i = 0; $i < $n; $i++) { // check if array element // greater then X or not if ($a[$i] > $x) { return 1; } } return -1; } // Driver Code $a = array( 1, 22, 3 ); $n = sizeof($a); $k = 13; echo smallestSubarray($a, $n, $k); // This code is contributed by ajit ?> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Smallest subarray having an element with frequency greater than that of other elements
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Smallest subarray with sum greater than a given value
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Longest subarray in which all elements are greater than K
- Smallest subset with sum greater than all other elements
- Sum of all array elements less than X and greater than Y for Q queries
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Maximum length of subarray such that all elements are equal in the subarray
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Count elements such that there are exactly X elements with values greater than or equal to X
- Number of positions such that adding K to the element is greater than sum of all other elements
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Count the subarray with sum strictly greater than the sum of remaining elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


