subarray sum in c++
C++
/* An efficient program to print
subarray with sum as given sum */
#include <iostream>
using namespace std;
/* Returns true if the there is a subarray of
arr[] with a sum equal to 'sum' otherwise
returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
/* Initialize curr_sum as value of
first element and starting point as 0 */
int curr_sum = arr[0], start = 0, i;
/* Add elements one by one to curr_sum and
if the curr_sum exceeds the 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 > sum && start < i - 1) {
curr_sum = curr_sum - arr[start];
start++;
}
// If curr_sum becomes equal to sum,
// then return true
if (curr_sum == sum) {
cout << "Sum found between indexes "
<< start << " and " << i - 1;
return 1;
}
// Add this element to curr_sum
if (i < n)
curr_sum = curr_sum + arr[i];
}
// If we reach here, then no subarray
cout << "No subarray found";
return 0;
}
// Driver Code
int main()
{
int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 23;
subArraySum(arr, n, sum);
return 0;
}
// This code is contributed by SHUBHAMSINGH10
/* A simple program to print subarray
with sum as given sum */
#include <bits/stdc++.h>
using namespace std;
/* Returns true if the there is a subarray
of arr[] with sum equal to 'sum' otherwise
returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
int curr_sum, i, j;
// Pick a starting point
for (i = 0; i < n; i++) {
curr_sum = arr[i];
// try all subarrays starting with 'i'
for (j = i + 1; j <= n; j++) {
if (curr_sum == sum) {
cout << "Sum found between indexes "
<< i << " and " << j - 1;
return 1;
}
if (curr_sum > sum || j == n)
break;
curr_sum = curr_sum + arr[j];
}
}
cout << "No subarray found";
return 0;
}
// Driver Code
int main()
{
int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 23;
subArraySum(arr, n, sum);
return 0;
}
// This code is contributed
// by rathbhupendra
Also in C++:
- Title
- vectors in c++ geeksforgeeks
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- root to leaf path print
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- c++ initialize array
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- c++ clear console
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- *max_element in c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- c++ delete printed characters
- Category
- C++
- Title
- how to put a class in a .h file c++
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- two elements with difference K in c++
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- find all occurrences of a substring in a string c++
- Category
- C++
- Title
- ternary search c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- how are graphics in games made
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- variable sized arrays hackerrank solution in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- factorial in c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++