Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Subarray with given sum in c++

/*
    Geeks For Geeks
    link problem: https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1/?page=1&difficulty[]=0&status[]=unsolved&sortBy=submissions#
*/

class Solution
{
    public:
    //Function to find a continuous sub-array which adds up to a given number.
    vector<int> subarraySum(int arr[], int n, long long s)
    {
        // Your code here
   
	   vector<int> ans;
	   int sum = 0;
	   for (int i = 0; i < n; i++)
	   {
	      sum = 0;
		  for (int j = i; j < n; j++)
		   {
		      sum += arr[j];
			  if (sum == s)
			  {
				 ans.push_back(i + 1);
				 ans.push_back(j + 1);
				 return ans;
		       }
		       else if (sum > s)
			   {
			      break;
			   }
		       else
		       {
			      continue;
		       }
	    	}
    	}
    	ans.push_back(-1);
    	return ans;
    }
};
Comment

Find Subarray sum using 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: map count function c++ 
Cpp :: c++ how to return an empty vector 
Cpp :: C++ Nested if...else 
Cpp :: convert all strings in vector to lowercase or uppercase c++ 
Cpp :: c++ initialise array 
Cpp :: accumulate vector c++ 
Cpp :: cpp linked list 
Cpp :: dice combinations cses solution 
Cpp :: resize vector c++ 
Cpp :: c++ variable types 
Cpp :: walk filesystem in c++ 
Cpp :: c++ uint8_t header 
Cpp :: Translation codeforces in c++ 
Cpp :: how to access a vector member by its index 
Cpp :: c++ class 
Cpp :: how to reset linerenderer unity 
Cpp :: C++ if...else...else if 
Cpp :: c++ formatting 
Cpp :: recursive factorial of a number 
Cpp :: declare empty array in c++ 
Cpp :: backtrack 
Cpp :: get function in cpp. 
Cpp :: select elements from array C++ 
Cpp :: c++ function pointer as variable 
Cpp :: fractional knapsack problem 
Cpp :: raspberry pi mount external hard drive 
Cpp :: kmp c++ 
Cpp :: check if cin got the wrong type 
Cpp :: string class in c++ 
Cpp :: OpenCV" is considered to be NOT FOUND 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =