Answers for "Find subarray sum in c++"

C++
0

Find subarray sum in c++

#include <iostream>
using namespace std;
int main()
{
    int arrayLen, totalSum;
    int arr[20];

    //input
    cin >> arrayLen >> totalSum;
    for (int i = 0; i < arrayLen; i++)
    {
        cin >> arr[i];
    }
    
    //algo
    // j => Loop iterator
    int i = 0, j = 0, start = -1, end = -1, sum = 0;

    while (j < arrayLen && sum + arr[j] <= totalSum) 
    {
        sum += arr[j];
        j++;
    } // after this loop sum is either greater than or equal to totalSum

    // If sum is equal
    if (sum == totalSum)
    {
        cout << i + 1 << " " << j << endl;
        return 0;
    }
    
    while (j < arrayLen)
    {
        sum += arr[j];
        
        while (sum > totalSum)
        {
            sum -= arr[i];
            i++;
        }

        if (sum == totalSum) //Store values
        {
            start = i + 1;
            end = j + 1;
            break;
        }
        j++;
    }
    cout << start <<" "<< end;

    return 0;
}
Posted by: Guest on September-05-2021

Browse Popular Code Answers by Language