1
#include <iostream>

using namespace std;

int main()
{
int n;
n=4;
int arr[n]={1,2,3,8};
int sum;
sum=5;
int curr=0;
cin>>sum;
for(int i=0;i<n;i++){
    for(int j=i;j<n;j++){
        curr+=arr[j];
        if(curr==sum){
            cout<<i;
        }
    cout<<curr<<endl;
    }
}
    
}

For the given question I need to find the starting and ending index of such a subarray. I have tried the above code but am not able to get the right output. Please guide me.

Diksha Nasa
  • 135
  • 3
  • 13
  • What you're looking for is what's commonly referred to as [Kaden's algorithm](https://www.google.com/amp/s/www.geeksforgeeks.org/largest-sum-contiguous-subarray/amp/) – Sherif Jul 31 '21 at 17:46
  • For a start, take the [tour] and read [ask]. Concerning your code, I wouldn't want to read it either, because it's not formatted/indented consistently. This is important for understanding! Now, concerning your question, do you know the algorithm? Without that, randomly throwing code together is futile. Further, "not able to get the right output" is not an accurate problem description. Even more so, since your code requires manual input. For an example, replace this with few hard-coded values. – Ulrich Eckhardt Jul 31 '21 at 17:48
  • @Sherif Thank you for your response, I did refer to the article, but I was wondering if there was anyway I could make changes in the current program using O(N2) complexity and get the right output. – Diksha Nasa Jul 31 '21 at 17:54
  • 2
    @UlrichEckhardt I apologize for the disoriented code, I just read this question and applied brute force approach which now seems to be a failure. Yes, I did read about Kaden's algorithm but tried to apply a different approach on this question to better understand about problem solving. I will edit the question and input the values in the code itself as you suggested. Thank you! – Diksha Nasa Jul 31 '21 at 17:57
  • You likely want to reset `curr` to zero on each iteration of the outer loop. – Igor Tandetnik Jul 31 '21 at 23:16
  • If anyone knows of a way to use Kaden's algorithm to solve this problem, please share the code or the link to the solution with us because I honestly don't know how to use Kaden's algorithm to solve this problem. My simple solution below has the time complexity of O(n^2). – Job_September_2020 Aug 01 '21 at 03:30
  • @Job_September_2020 , Sherif had shared the link for using Kaden's algorithm for solving this issue. Sharing the link again : [Kaden's Algorithm](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/) – Diksha Nasa Aug 01 '21 at 05:00

2 Answers2

1

I think your code only needs some minor modifications. You should add some code to handle the case where your running sum is greater than the target sum, and you should also re-initialize your running sum correctly.

There may be some efficient solution that is faster than O(n^2), which I am not aware of yet. If someone knows of a solution with a better time complexity, please share with us.

Below is a simple algorithm that has the time complexity of O(n^2). (It may not have the most efficient time complexity for this problem).

This function prints out the 2 indices of the array. The sum of all elements between these 2 indices inclusively will equal the target sum.

    void Print_Index_of_2_Elements(int array[], int total_element, int target)
    {            
            // Use Brute force .  Time complexity = O(n^2)
            for (int i = 0; i < total_element; i++) 
            {
                int running_sum = array[i];
        
                // Second for loop
                for (int j = (i + 1) ; j < total_element; j++) 
                {
                    if (running_sum == target) 
                    {    
                        cout << "Two indices are: " << i << " and " << j;
                        return;  // Found Answer. Exit.
                    }
                    else if ( running_sum > target )
                        break;
                    else  //  running_sum < target 
                        running_sum += array[j];
                }
            }
        
            cout << " Sorry - no answer was found for the target sum.";             
}
Job_September_2020
  • 858
  • 2
  • 10
  • 25
0

If you are someone that is a beginner in subarrays or arrays for the case. Then this code is for you:

#include <iostream>

using namespace std;

int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
    cin>>arr[i];
}
int sum;
cin>>sum;
int curr=0;
for(int i=0;i<n;i++){
    for(int j=i;j<n;j++){
        if(curr==sum){
            cout<<i+1<<" "<<j;
            return 0;
        }
        else if (curr>sum){
            curr=0;
        }
        else if(curr<sum){
            curr+=arr[j];
        }

    }
}
return 0;
}

If you have any doubts regarding this, feel free to comment and let me know.

Diksha Nasa
  • 135
  • 3
  • 13