Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Count subarrays with all elements greater than K
Next article icon

Smallest subarray such that all elements are greater than K

Last Updated : 08 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 than 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 than 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 than X or not
        if a[i] > k:
            return 1
    return -1
 
# Driver Code
a = [1, 22, 3]
n = len(a)
k = 13
print(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 than 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 than 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
?>
 
 

Javascript




<script>
// JavaScriptto print the length of the shortest
// subarray with all elements greater than X
 
// Function to return shortest array
function smallestSubarray(a, n, x)
{
    let count = 0, length = 0;
 
    // Iterate in the array
    for (let i = 0; i < n; i++) {
 
        // check if array element
        // greater than X or not
        if (a[i] > x) {
            return 1;
        }
    }
 
    return -1;
}
 
// Driver Code
 
    let a = [1, 22, 3 ]
    let n = a.length
    let k = 13;
 
    document.write(smallestSubarray(a, n, k));
 
// This code is contributed by Surbhi Tyagi.
</script>
 
 
Output
1

Complexity Analysis:

  • Time Complexity: O(N) 
  • Auxiliary Space: O(1)


Next Article
Count subarrays with all elements greater than K

S

swetankmodi
Improve
Article Tags :
  • Arrays
  • Competitive Programming
  • DSA
  • array-traversal-question
  • subarray
Practice Tags :
  • Arrays

Similar Reads

  • Longest subarray in which all elements are greater than K
    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}. Inp
    6 min read
  • Count subarrays with all elements greater than K
    Given an array of n integers and an integer k, the task is to find the number of subarrays such that all elements in each subarray are greater than k. Examples: Input: arr[] = {3, 4, 5, 6, 7, 2, 10, 11}, k= 5 Output: 6 The possible subarrays are {6}, {7}, {6, 7}, {10}, {11} and {10, 11}. Input: arr[
    9 min read
  • Longest subarray in which all elements are smaller than K
    Given an array arr[] consisting of N integers and an integer K, the task is to find the length of the longest subarray in which all the elements are smaller than K. Constraints:0 <= arr[i] <= 10^5 Examples: Input: arr[] = {1, 8, 3, 5, 2, 2, 1, 13}, K = 6Output: 5Explanation:There is one possib
    11 min read
  • Smallest subarray with sum greater than or equal to K
    Given an array A[] consisting of N integers and an integer K, the task is to find the length of the smallest subarray with sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: A[] = {2, -1, 2}, K = 3 Output: 3 Explanation: Sum of the given array is 3. Hence, the sma
    15+ min read
  • Smallest subarray from a given Array with sum greater than or equal to K | Set 2
    Given an array A[] consisting of N positive integers and an integer K, the task is to find the length of the smallest subarray with a sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: arr[] = {3, 1, 7, 1, 2}, K = 11Output: 3Explanation:The smallest subarray with
    15+ min read
  • Smallest subarray of size greater than K with sum greater than a given value
    Given an array, arr[] of size N, two positive integers K and S, the task is to find the length of the smallest subarray of size greater than K, whose sum is greater than S. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 1, S = 8Output: 2Explanation: Smallest subarray with sum greater than S(=8) is {4
    15 min read
  • Number of subarrays with at-least K size and elements not greater than M
    Given an array of N elements, K is the minimum size of the sub-array, and M is the limit of every element in the sub-array, the task is to count the number of sub-arrays with a minimum size of K and all the elements are lesser than or equal to M. Examples: Input: arr[] = {-5, 0, -10}, K = 1, M = 15O
    7 min read
  • Smallest subarray such that its bitwise OR is at least k
    Given an array arr[] of non-negative integers and an integer k. The task is to find the smallest non-empty subarray of arr[] such that the bitwise OR of all of its elements is at least k, or return -1 if no subarray exists. Example: Input: arr = {1,2,3}, k = 2Output: 1Explanation: The subarray {3} h
    10 min read
  • Longest subarray in which all elements are a factor of K
    Given an array A[] of size N and a positive integer K, the task is to find the length of the longest subarray such that all elements of the subarray is a factor of K. Examples: Input: A[] = {2, 8, 3, 10, 6, 7, 4, 9}, K = 60Output: 3Explanation: The longest subarray in which all elements are a factor
    6 min read
  • Smallest subarray having an element with frequency greater than that of other elements
    Given an array arr of positive integers, the task is to find the smallest length subarray of length more than 1 having an element occurring more times than any other element. Examples: Input: arr[] = {2, 3, 2, 4, 5} Output: 2 3 2 Explanation: The subarray {2, 3, 2} has an element 2 which occurs more
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences