fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7. // int pid[] = {1,2,3,4};
  8. int arr[] = {2,1,3,2,4}; // k-3 ans=3
  9. int n = 5;
  10. int k=3;
  11.  
  12. int pid[n+1];
  13. int p[n];
  14.  
  15. for(int i=1; i<=n; i++){
  16. pid[i]=arr[i-1];
  17. p[i]=p[i-1]+arr[i];
  18. }
  19.  
  20.  
  21.  
  22. unordered_map<int, int> mp;
  23.  
  24.  
  25. int count=0;
  26. mp[0] = 1;
  27.  
  28.  
  29.  
  30. for(int j=1; j<n; j++){
  31.  
  32.  
  33.  
  34. int t=(p[j]%k -j%k + k)%k;
  35.  
  36.  
  37. cout<<"chosen for position- " << j << " target: "<< t << endl;
  38.  
  39. cout<<"mp[t]: " << mp[t]<< endl;
  40. if(mp.find(t)!=mp.end()){
  41. count += mp[t];
  42. }
  43.  
  44.  
  45. cout<< "count is now: " << count<<endl;
  46.  
  47. mp[t]++;
  48.  
  49.  
  50. }
  51.  
  52. cout<<count;
  53. return 0;
  54. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
chosen for position- 1 target: 1
mp[t]: 0
count is now: 0
chosen for position- 2 target: 0
mp[t]: 1
count is now: 1
chosen for position- 3 target: 1
mp[t]: 1
count is now: 2
chosen for position- 4 target: 1
mp[t]: 2
count is now: 4
4