fork download
  1. #include <iostream>
  2. using namespace std;
  3. int count(int arr[],int n,int k){
  4. int count;
  5. for(int i=0;i<n;i++){
  6. int sum=0;
  7. for(int j=i;j<n;j++){
  8. sum+=arr[j];
  9. if(sum%k==j-i+1){
  10. count++;
  11. }
  12. }
  13. }
  14. return count;
  15. }
  16.  
  17. int main() {
  18. // your code goes here
  19. int n;
  20. cin>>n;
  21. int k;
  22. cin>>k;
  23. int arr[n];
  24. for(int i=0;i<n;i++){
  25. cin>>arr[i];
  26. }
  27. cout<<"The count of good subarrays is:"<<count(arr,n,k);
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5288KB
stdin
4 4
1 3 2 4
stdout
The count of good subarrays is:2