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:
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)
Similar Reads
Find all subarrays with sum in the given range Given an unsorted array of size, N. Find subarrays that add to a sum in the given range L-R. Examples: Input: arr[] = {2, 3, 5, 8}, L = 4, R = 13Output: The indexes of subarrays are {0, 1}, {0, 2}, {1, 2}, {2, 2}, {2, 3}, {3, 3} Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: The indexes of subarrays
4 min read
Print all subarrays with 0 sum Given an array arr[] of size n, the task is to print all subarrays in the array which has sum 0.Examples: Input: arr = [6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7]Output:Subarray found from Index 2 to 4Subarray found from Index 2 to 6Subarray found from Index 5 to 6Subarray found from Index 6 to 9Subarra
14 min read
Number of subarrays having sum in a given range Given an array arr[] of integers and a range (L, R). Find the number of subarrays having sum in the range L to R. Examples: Input: arr = { -2, 4, 1, -2}, lower = -4, upper = 1Output: 5Explanation: The pairs that are present here are - (1, 1) = [-2] , sum = -2(1, 4) = [-2, 4, 1, -2] , sum = 1(3, 3) =
15 min read
Subarray of size k with given sum Given an array arr[], an integer K and a Sum. The task is to check if there exists any subarray with K elements whose sum is equal to the given sum. If any of the subarray with size K has the sum equal to the given sum then print YES otherwise print NO. Examples: Input: arr[] = {1, 4, 2, 10, 2, 3, 1
10 min read
Count maximum non-overlapping subarrays with given sum Given an array arr[] consisting of N integers and an integer target, the task is to find the maximum number of non-empty non-overlapping subarrays such that the sum of array elements in each subarray is equal to the target. Examples: Input: arr[] = {2, -1, 4, 3, 6, 4, 5, 1}, target = 6Output: 3Expla
7 min read