Print all subarrays with sum in a given range - GeeksforGeeks

02 Dec, 2021
Given an array arr[] of positive integers and two integers L and R defining the range [L, R]. The task is to print the subarrays having sum in the range L to R.
Examples:  
Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: {1, 4}, {4}, {6}.Explanation: All the possible subarrays are the following{1] with sum 1. {1, 4} with sum 5. {1, 4, 6} with sum 11.{4} with sum 4.{4, 6} with sum 10. {6} with sum 6. Therefore, subarrays {1, 4}, {4}, {6} are having sum in range [3, 8].
Input: arr[] = {2, 3, 5, 8}, L = 4, R = 13Output:  {2, 3}, {2, 3, 5}, {3, 5}, {5}, {5, 8}, {8}.
Approach: This problem can be solved by doing brute force and checking for each and every possible subarray using two loops. Below is the implementation of the above approach. 
C++
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to find subarrays in given rangevoid subArraySum(int arr[], int n,                 int leftsum, int rightsum){    int curr_sum, i, j, res = 0;     // Pick a starting point    for (i = 0; i < n; i++) {        curr_sum = arr[i];         // Try all subarrays starting with 'i'        for (j = i + 1; j <= n; j++) {            if (curr_sum > leftsum                && curr_sum < rightsum) {                cout << "{ ";                 for (int k = i; k < j; k++)                    cout << arr[k] << " ";                 cout << "}\n";            }            if (curr_sum > rightsum || j == n)                break;            curr_sum = curr_sum + arr[j];        }    }} // Driver Codeint main(){    int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };    int N = sizeof(arr) / sizeof(arr[0]);     int L = 10, R = 23;     subArraySum(arr, N, L, R);     return 0;}
// Java code for the above approachimport java.io.*; class GFG{       // Function to find subarrays in given range    static void subArraySum(int arr[], int n, int leftsum,                            int rightsum)    {        int curr_sum, i, j, res = 0;         // Pick a starting point        for (i = 0; i < n; i++) {            curr_sum = arr[i];             // Try all subarrays starting with 'i'            for (j = i + 1; j <= n; j++) {                if (curr_sum > leftsum                    && curr_sum < rightsum) {                    System.out.print("{ ");                     for (int k = i; k < j; k++)                        System.out.print(arr[k] + " ");                     System.out.println("}");                }                if (curr_sum > rightsum || j == n)                    break;                curr_sum = curr_sum + arr[j];            }        }    }     // Driver Code    public static void main(String[] args)    {        int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };        int N = arr.length;         int L = 10, R = 23;         subArraySum(arr, N, L, R);    }} // This code is contributed by Potta Lokesh
# Python program for above approach # Function to find subarrays in given rangedef subArraySum (arr, n, leftsum, rightsum):    res = 0     # Pick a starting point    for i in range(n):        curr_sum = arr[i]         # Try all subarrays starting with 'i'        for j in range(i + 1, n + 1):            if (curr_sum > leftsum                and curr_sum < rightsum):                print("{ ", end="")                 for k in range(i, j):                    print(arr[k], end=" ")                 print("}")            if (curr_sum > rightsum or j == n):                break            curr_sum = curr_sum + arr[j]         # Driver Codearr = [15, 2, 4, 8, 9, 5, 10, 23]N = len(arr)L = 10R = 23subArraySum(arr, N, L, R) # This code is contributed by Saurabh Jaiswal
// C# code for the above approachusing System; class GFG{       // Function to find subarrays in given range    static void subArraySum(int []arr, int n, int leftsum,                            int rightsum)    {        int curr_sum, i, j, res = 0;         // Pick a starting point        for (i = 0; i < n; i++) {            curr_sum = arr[i];             // Try all subarrays starting with 'i'            for (j = i + 1; j <= n; j++) {                if (curr_sum > leftsum                    && curr_sum < rightsum) {                    Console.Write("{ ");                     for (int k = i; k < j; k++)                        Console.Write(arr[k] + " ");                     Console.WriteLine("}");                }                if (curr_sum > rightsum || j == n)                    break;                curr_sum = curr_sum + arr[j];            }        }    }     // Driver Code    public static void Main()    {        int []arr = { 15, 2, 4, 8, 9, 5, 10, 23 };        int N = arr.Length;         int L = 10, R = 23;         subArraySum(arr, N, L, R);    }} // This code is contributed by Samim Hossain Mondal.
<script>    // JavaScript program for above approach     // Function to find subarrays in given range    const subArraySum = (arr, n, leftsum, rightsum) => {        let curr_sum, i, j, res = 0;         // Pick a starting point        for (i = 0; i < n; i++) {            curr_sum = arr[i];             // Try all subarrays starting with 'i'            for (j = i + 1; j <= n; j++) {                if (curr_sum > leftsum                    && curr_sum < rightsum) {                    document.write("{ ");                     for (let k = i; k < j; k++)                        document.write(`${arr[k]} `);                     document.write("}<br/>");                }                if (curr_sum > rightsum || j == n)                    break;                curr_sum = curr_sum + arr[j];            }        }    }     // Driver Code    let arr = [15, 2, 4, 8, 9, 5, 10, 23];    let N = arr.length;    let L = 10, R = 23;    subArraySum(arr, N, L, R);     // This code is contributed by rakeshsahni </script>
{ 15 }
{ 15 2 }
{ 15 2 4 }
{ 2 4 8 }
{ 4 8 }
{ 4 8 9 }
{ 8 9 }
{ 8 9 5 }
{ 9 5 }
{ 5 10 }
Time Complexity: O(N^3)
Auxiliary Space: O(1)
rakeshsahni
lokeshpotta20
_saurabh_jaiswal
samim2000
simmytarika5
subarray
subarray-sum
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Remove duplicates from sorted array
Move all negative numbers to beginning and positive to end with constant extra space
Building Heap from Array