Find subarray with given sum with negatives allowed in constant space - GeeksforGeeks

04 Feb, 2022
Given an unsorted array of integers, find a subarray that adds to a given number. If there is more than one subarray with the sum of the given number, print any of them.
Examples:
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Output: Sum found between indexes 2 and 4

Input: arr[] = {10, 2, -2, -20, 10}, sum = -10
Output: Sum found between indexes 0 to 3

Input: arr[] = {-10, 0, 2, -2, -20, 10}, sum = 20
Output: No subarray with given sum exists.
We had already discussed an approach to find subarray with a given sum in an array containing both positive and negative elements using Hashing In this article, an approach without using any extra space is discussed.The idea is to modify the array to contain only positive elements: 
Find the smallest negative element in the array and increase every value in the array by the absolute value of the smallest negative element found.
We may notice that after doing the above modification, the sum of every subarray will also increase by: 
(number of elements in the subarray) * (absolute value of min element)
After doing the above modification to the input array, the task is reduced to finding if there is any subarray in the given array of only positive numbers with the new target sum. This can be done using the sliding window technique in O(N) time and constant space. Every time while adding elements to the window, increment the target sum by the absolute value of the minimum element and, similarly, while removing elements from the current window, decrement the target sum by the absolute value of the minimum element so that for every subarray of length, say K, the updated target sum will be:  
targetSum = sum + K*abs(minimum element)
Below is the approach for the same:  
Initialize a variable curr_sum as the first element.
The variable curr_sum indicates the sum of the current subarray.
Start from the second element and add all elements one by one to the curr_sum, and keep incrementing the target sum by abs(minimum element).
If curr_sum becomes equal to the target sum, then print the solution.
If curr_sum exceeds the sum, then remove the trailing elements while decreasing curr_sum is greater than the sum and keep decreasing the target sum by abs(minimum element).
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to check if subarray with sum// exists when negative elements are also present#include <bits/stdc++.h>using namespace std; // Function to check if subarray with sum// exists when negative elements are also presentvoid subArraySum(int arr[], int n, int sum){    int minEle = INT_MAX;     // Find minimum element in the array    for (int i = 0; i < n; i++)        minEle = min(arr[i], minEle);     // Initialize curr_sum as value of first element    // and starting point as 0    int curr_sum = arr[0] + abs(minEle), start = 0, i;     // Starting window length will be 1,    // For generating new target sum,    // add abs(minEle) to sum only 1 time    int targetSum = sum;     // Add elements one by one to curr_sum    // and if the curr_sum exceeds the    // updated sum, then remove starting element    for (i = 1; i <= n; i++) {         // If curr_sum exceeds the sum,        // then remove the starting elements        while (curr_sum - (i - start) * abs(minEle) > targetSum && start < i) {            curr_sum = curr_sum - arr[start] - abs(minEle);            start++;        }         // If curr_sum becomes equal to sum, then return true        if (curr_sum - (i - start) * abs(minEle) == targetSum) {            cout << "Sum found between indexes " << start << " and " << i - 1;            return;        }         // Add this element to curr_sum        if (i < n) {            curr_sum = curr_sum + arr[i] + abs(minEle);        }    }     // If we reach here, then no subarray    cout << "No subarray found";} // Driver Codeint main(){    int arr[] = { 10, -12, 2, -2, -20, 10 };    int n = sizeof(arr) / sizeof(arr[0]);     int sum = -10;     subArraySum(arr, n, sum);     return 0;}
// Java program to check if subarray with sum// exists when negative elements are also presentclass GFG{         // Function to check if subarray with sum    // exists when negative elements are also present    static void subArraySum(int arr[], int n, int sum)    {        int minEle = Integer.MAX_VALUE;             // Find minimum element in the array        for (int i = 0; i < n; i++)            minEle = Math.min(arr[i], minEle);             // Initialize curr_sum as value of        // first element and starting point as 0        int curr_sum = arr[0] + Math.abs(minEle);        int start = 0, i;             // Starting window length will be 1,        // For generating new target sum,        // add abs(minEle) to sum only 1 time        int targetSum = sum;             // Add elements one by one to curr_sum        // and if the curr_sum exceeds the        // updated sum, then remove starting element        for (i = 1; i <= n; i++)        {                 // If curr_sum exceeds the sum,            // then remove the starting elements            while (curr_sum - (i - start) *                   Math.abs(minEle) > targetSum &&                                      start < i)            {                curr_sum = curr_sum - arr[start] -                           Math.abs(minEle);                start++;            }                 // If curr_sum becomes equal to sum,            // then return true            if (curr_sum - (i - start) *                Math.abs(minEle) == targetSum)            {                System.out.println("Sum found between indexes " +                                      start + " and " + (i - 1));                return;            }                 // Add this element to curr_sum            if (i < n)            {                curr_sum = curr_sum + arr[i] +                             Math.abs(minEle);            }        }             // If we reach here, then no subarray        System.out.println("No subarray found");    }         // Driver Code    public static void main (String[] args)    {        int arr[] = { 10, -12, 2, -2, -20, 10 };        int n = arr.length;             int sum = -10;             subArraySum(arr, n, sum);    }} // This code is contributed by AnkitRai01
# Python3 program to check if subarray with summ# exists when negative elements are also present # Function to check if subarray with summ# exists when negative elements are also presentdef subArraySum(arr, n, summ):    minEle = 10**9     # Find minimum element in the array    for i in range(n):        minEle = min(arr[i], minEle)     # Initialize curr_summ as value of    # first element and starting point as 0    curr_summ = arr[0] + abs(minEle)    start = 0     # Starting window length will be 1,    # For generating new target summ,    # add abs(minEle) to summ only 1 time    targetSum = summ     # Add elements one by one to curr_summ    # and if the curr_summ exceeds the    # updated summ, then remove starting element    for i in range(1, n + 1):         # If curr_summ exceeds the summ,        # then remove the starting elements        while (curr_summ - (i - start) * abs(minEle) >                       targetSum and start < i):            curr_summ = curr_summ - arr[start] - abs(minEle)            start += 1         # If curr_summ becomes equal to summ,        # then return true        if (curr_summ - (i - start) *            abs(minEle) == targetSum):            print("Sum found between indexes",                          start, "and", i - 1)            return         # Add this element to curr_summ        if (i < n):            curr_summ = curr_summ + arr[i] + abs(minEle)     # If we reach here, then no subarray    print("No subarray found") # Driver Codearr = [10, -12, 2, -2, -20, 10]n = len(arr) summ = -10 subArraySum(arr, n, summ) # This code is contributed by Mohit Kumar
// C# program to check if subarray// with sum exists when negative// elements are also presentusing System; class GFG{         // Function to check if subarray    // with sum exists when negative    // elements are also present    static void subArraySum(int []arr,                            int n, int sum)    {        int minEle = int.MaxValue;        int i;                 // Find minimum element in the array        for (i = 0; i < n; i++)            minEle = Math.Min(arr[i], minEle);             // Initialize curr_sum as value of        // first element and starting point as 0        int curr_sum = arr[0] + Math.Abs(minEle);        int start = 0;             // Starting window length will be 1,        // For generating new target sum,        // add abs(minEle) to sum only 1 time        int targetSum = sum;             // Add elements one by one to curr_sum        // and if the curr_sum exceeds the        // updated sum, then remove starting element        for (i = 1; i <= n; i++)        {                 // If curr_sum exceeds the sum,            // then remove the starting elements            while (curr_sum - (i - start) *                   Math.Abs(minEle) > targetSum &&                                      start < i)            {                curr_sum = curr_sum - arr[start] -                           Math.Abs(minEle);                start++;            }                 // If curr_sum becomes equal to sum,            // then return true            if (curr_sum - (i - start) *                Math.Abs(minEle) == targetSum)            {                Console.Write("Sum found between indexes " +                                 start + " and " + (i - 1));                return;            }                 // Add this element to curr_sum            if (i < n)            {                curr_sum = curr_sum + arr[i] +                             Math.Abs(minEle);            }        }             // If we reach here, then no subarray        Console.Write("No subarray found");    }         // Driver Code    static public void Main ()    {        int []arr = { 10, -12, 2, -2, -20, 10 };        int n = arr.Length;             int sum = -10;             subArraySum(arr, n, sum);    }} // This code is contributed by ajit
<script>// Java Script program to check if subarray with sum// exists when negative elements are also present      // Function to check if subarray with sum// exists when negative elements are also presentfunction subArraySum(arr,n,sum){    let minEle = Number.MAX_VALUE;         // Find minimum element in the array    for (let i = 0; i < n; i++)        minEle = Math.min(arr[i], minEle);         // Initialize curr_sum as value of    // first element and starting point as 0    let curr_sum = arr[0] + Math.abs(minEle);    let start = 0, i;         // Starting window length will be 1,    // For generating new target sum,    // add abs(minEle) to sum only 1 time    let targetSum = sum;         // Add elements one by one to curr_sum    // and if the curr_sum exceeds the    // updated sum, then remove starting element    for (i = 1; i <= n; i++)    {             // If curr_sum exceeds the sum,        // then remove the starting elements        while (curr_sum - (i - start) *            Math.abs(minEle) > targetSum &&                                    start < i)        {            curr_sum = curr_sum - arr[start] -                    Math.abs(minEle);            start++;        }             // If curr_sum becomes equal to sum,        // then return true        if (curr_sum - (i - start) *                Math.abs(minEle) == targetSum)        {            document.write("Sum found between indexes " +                                    start + " and " + (i - 1));            return;        }             // Add this element to curr_sum        if (i < n)        {            curr_sum = curr_sum + arr[i] +                            Math.abs(minEle);        }    }         // If we reach here, then no subarray    document.write("No subarray found");}     // Driver Code         let arr = [ 10, -12, 2, -2, -20, 10 ];    let n = arr.length;         let sum = -10;         subArraySum(arr, n, sum);  // This code is contributed by sravan kumar</script>
Sum found between indexes 1 and 2
 
mohit kumar 29
ankthon
jit_t
nidhi_biet
sameer200240
sonusaikat09
sravankumar8128
surinderdawra388
sliding-window
subarray
subarray-sum
Arrays
sliding-window
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Python | Using 2D arrays/lists the right way
Linked List vs Array
Queue | Set 1 (Introduction and Array Implementation)
Find the Missing Number
Find Second largest element in an array
Array of Strings in C++ (5 Different Ways to Create)
Program to find largest element in an array
Merge two sorted arrays
Sort an array of 0s, 1s and 2s