Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Minimum Subarray With Required Sum
MEDIUM
18 mins
43 upvotes
Binary Search
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Binary Search
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Minimum Subarray With Required Sum

Contributed by
Anish De
Medium
yellow-spark
0/80
Avg time to solve 18 mins
Success Rate 85 %
Share
43 upvotes

Problem Statement

You have been given an array(ARR) of positive integers and an integer X. You have to find the minimum length subarray such that the sum of all of its elements is strictly greater than the given integer X.

Note:
A subarray is a contiguous block of elements that can be formed by deleting some (possibly zero) elements from the beginning or the end of the original array. 
For example :
If the given array is [1, 2, 3, 4, 5], then [2, 3, 4], [1, 2], [5] are some subarrays while [1, 3], [2, 3, 5] are not.

If there are multiple subarrays with minimum length, find one which appears earlier in the array (i.e. subarray that starts with lower index).

If there is no such subarray, print an empty line.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
4 13
13 7 6 12
Sample Output 1:
13 7
Explanation For Sample Input 1:
Out of all the subarrays, we have [13, 7] and [6, 12] with minimum length of 2 and sum of their elements greater than X = 13. As the starting index of [13, 7] is lower, we print it as the output.
Sample Input 2:
5 6
1 2 3 4 5
Sample Output 2:
3 4
Reset Code
Full screen
Auto
copy-code
Console