fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. vector<int>arr={1,1,1,-1,1,2,3,4,5};
  9. int k=3;
  10. //Brute force->
  11. int n=arr.size();
  12. int count=0;
  13. for(int i=0;i<n;i++){
  14. int sum=0;
  15. for(int j=i;j<n;j++){
  16.  
  17. sum+=arr[j];
  18. if(sum==k){
  19. count++;
  20. //can't use break in case -ve nos. present in array.
  21. }
  22. }
  23. }
  24. cout<<count;
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
5