-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain (7).cpp
65 lines (56 loc) · 1.06 KB
/
main (7).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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 countSubarrays(int arr[],
int n, int k)
{
int start = 0, end = 0,
count = 0, sum = arr[0];
while (start < n && end < n) {
// If sum is less than k,
// move end by one position.
// Update count and sum
// accordingly.
if (sum < k) {
end++;
if (end >= start)
count += end - start;
// For last element,
// end may become n
if (end < n)
sum += arr[end];
}
// If sum is greater than or
// equal to k, subtract
// arr[start] from sum and
// decrease sliding window by
// moving start by one position
else {
sum -= arr[start];
start++;
}
}
return count;
}
int main()
{
int n;
long long int k ;
cin>>n>>k ;
int i =0 ;
int arr[100000];
while(n--)
{
int a,b ;
cin>>a>>b ;
arr[i]=b;
i++ ;
}
sort(arr,arr+n);
cout << countSubarrays(arr, n, k);
}