COunt the number of continous subsequences such that the sum is between
C++
// CPP program to find number of subarrays having
// sum in the range L to R.
#include <bits/stdc++.h>
using namespace std;
// Function to find number of subarrays having
// sum less than or equal to x.
int countSub(int arr[], int n, int x)
{
// Starting index of sliding window.
int st = 0;
// Ending index of sliding window.
int end = 0;
// Sum of elements currently present
// in sliding window.
int sum = 0;
// To store required number of subarrays.
int cnt = 0;
// Increment ending index of sliding
// window one step at a time.
while (end < n) {
// Update sum of sliding window on
// adding a new element.
sum += arr[end];
// Increment starting index of sliding
// window until sum is greater than x.
while (st <= end && sum > x) {
sum -= arr[st];
st++;
}
// Update count of number of subarrays.
cnt += (end - st + 1);
end++;
}
return cnt;
}
// Function to find number of subarrays having
// sum in the range L to R.
int findSubSumLtoR(int arr[], int n, int L, int R)
{
// Number of subarrays having sum less
// than or equal to R.
int Rcnt = countSub(arr, n, R);
// Number of subarrays having sum less
// than or equal to L-1.
int Lcnt = countSub(arr, n, L - 1);
return Rcnt - Lcnt;
}
// Driver code.
int main()
{
int arr[] = { 1, 4, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int L = 3;
int R = 8;
cout << findSubSumLtoR(arr, n, L, R);
return 0;
}
// CPP program to count
// subarrays having sum
// less than k.
#include <bits/stdc++.h>
using namespace std;
// Function to find number
// of subarrays having sum
// less than k.
int countSubarray(int arr[],
int n, int k)
{
int count = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
// If sum is less than k
// then update sum and
// increment count
if (sum + arr[j] < k) {
sum = arr[j] + sum;
count++;
}
else {
break;
}
}
}
return count;
}
// Driver Code
int main()
{
int array[] = { 1, 11, 2, 3, 15 };
int k = 10;
int size = sizeof(array) / sizeof(array[0]);
int count = countSubarray(array, size, k);
cout << count << "\n";
}
Also in C++:
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- c++ clear console
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- how to get the largest number in a c++ array
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- #include
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- sub string of vector c++
- Category
- C++
- Title
- c++ for loops
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- log base 10 c+_+
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++