# Find subarray with given sum - (Nonnegative Numbers) in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] consisting of N positive integers stored in unsorted order. Our task is to find a subarray with a given sum.

Let's take an example to understand the problem,

Input : arr[] = {2, 5, 1, 4, 6, 9, 5} sum = 11
Output : subarray = {1, 4, 6}

Explanation

Subarray sum = 1 + 4 + 6 = 11

## Solution Approach

A simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find the sum of all elements and compare it with the given sum value. If it's equal, print the subarray. If all elements of the array are traversed, print no such array found.

## Algorithm

• Step 1 − Loop through the array, i -> 0 to (n-1).

• Step 1.1 − For each element, find the sum of each subarray for all possible subarrays.

• Step 1.2 − if the sum of current sum array elements is equal to the given subarray, print the subarray.

• Step 2 − If all elements of the array are traversed and no subarray is found. Print "No subarray with the given sum found!".

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
void printSubArray(int arr[], int i, int j){
cout<<"{";
for(; i < j; i++)
cout<<arr[i]<<" ";
cout<<"}";
}
int findSubArrayWithSum(int arr[], int n, int sum) {
int currSum;
for (int i = 0; i < n; i++) {
currSum = arr[i];
for (int j = i + 1; j <= n; j++) {
if (currSum == sum) {
cout<<"Subarray with given sum : ";
printSumArray(arr, i, j); return 1;
}
if (currSum > sum || j == n)
break;
currSum = currSum + arr[j];
}
}
cout<<"No subarray found";
return 0;
}
int main() {
int arr[] = { 2, 5, 1, 4, 6, 9, 3};
int n = sizeof(arr) / sizeof(arr);
int sum = 11;
findSubArrayWithSum(arr, n, sum);
return 0;
}

## Output

Subarray with given sum : { 1 4 6 }

A better approach to solve the problem is using a method similar to sliding window but here we will have a variable window. We will start from the first element of the array, add elements to the window till the sum becomes greater than the given sum. If it becomes greater than remove the elements until it becomes less than sum again. Do this process till the whole array is traversed.

If at any point, the window sum becomes equal to the given sum, print the subarray. And if no window will sum equal to the given sum is found and there are no elements to be traversed, print "No subarray found!".

## Algorithm

Initialise − windowSum = 0, sindex = 0, endindex = 0

• Step 1 − Traverse the array using endindex.

• Step 1.1 −Update the windowSum by adding elements till the windowSum becomes greater than sum i.e. if(windowSum < sum) -> windowSum = windowSum + arr[endIndex].

• Step 1.2 − If the windowSum is greater than sum, reduce window size by removing elements i.e. while(windowSum < sum) -> windowSum = windowSum - arr[endIndex].

• Step 1.3 − if windowSum == sum, print subarray.

• Step 2 − If the array is traversed, print -> 'not possible'.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
void printSubArray(int arr[], int i, int j){
cout<<"{ ";
for(; i < j; i++)
cout<<arr[i]<<" ";
cout<<"}";
}
int findSubArrayWithSum(int arr[], int n, int sum) {
int windowSum = arr, startIndex = 0, endIndex;
for (endIndex = 1; endIndex <= n; endIndex++) {
while (windowSum > sum && startIndex < endIndex - 1) {
windowSum -= arr[startIndex];
startIndex++;
}
if (windowSum == sum) {
cout << "Subarray with given sum : ";
printSumArray(arr, startIndex ,endIndex);
return 1;
}
if (endIndex < n)
windowSum += arr[endIndex];
}
cout << "No subarray found";
return 0;
}
int main() {
int arr[] = { 2, 5, 1, 4, 6, 9, 3};
int n = sizeof(arr) / sizeof(arr);
int sum = 11;
findSubArrayWithSum(arr, n, sum);
return 0;
}

## Output

Subarray with given sum : { 1 4 6 }