Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Print all subarrays with sum in a given range

  • Last Updated : 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:  

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {1, 4, 6}, L = 3, R = 8
Output: {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 = 13
Output:  {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++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find subarrays in given range
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) {
                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 Code
int 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




// Java code for the above approach
import 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

Python3




# Python program for above approach
 
# Function to find subarrays in given range
def 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 Code
arr = [15, 2, 4, 8, 9, 5, 10, 23]
N = len(arr)
L = 10
R = 23
subArraySum(arr, N, L, R)
 
# This code is contributed by Saurabh Jaiswal

C#




// C# code for the above approach
using 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.

Javascript




<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>
Output
{ 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)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!