fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n,k;
  6. cin>>n>>k;
  7.  
  8. vector<int> numbers(n);
  9.  
  10. for(auto& number: numbers){
  11. cin>>number;
  12. }
  13.  
  14. int smallest = INT_MAX, largest = INT_MIN;
  15. int countSmallest = 0, countLargest = 0;
  16.  
  17. unordered_map<int,int> firstOccr, lastOccr;
  18.  
  19. firstOccr[0] = -1;
  20. lastOccr[0] = -1;
  21.  
  22. int currSum = 0;
  23.  
  24. for(int i=0; i<n; i++){
  25. currSum += numbers[i];
  26.  
  27. if(firstOccr.find(currSum - k) != firstOccr.end()){
  28. int minLen = i - lastOccr[currSum - k];
  29. int maxLen = i - firstOccr[currSum - k];
  30.  
  31. if(maxLen == largest)countLargest++;
  32. if(minLen == smallest)countSmallest++;
  33.  
  34. if(maxLen > largest){
  35. largest = maxLen;
  36. countLargest = 1;
  37. }
  38.  
  39. if(minLen < smallest){
  40. smallest = minLen;
  41. countSmallest = 1;
  42. }
  43.  
  44. }
  45.  
  46. if(firstOccr.find(currSum) == firstOccr.end())firstOccr[currSum] = i;
  47.  
  48. lastOccr[currSum] = i;
  49.  
  50. }
  51.  
  52. cout<<largest<<" "<<countLargest<<endl;
  53. cout<<smallest<<" "<<countSmallest;
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5288KB
stdin
6 8
3 2 3 3 2 8
stdout
3 3
1 1