fork download
  1. class Solution {
  2. public:
  3. int minSubarray(vector<int>& a, int p) {
  4.  
  5. int i,n=a.size();
  6. // cout<<n<<endl;
  7. vector<int>front(n,0),back(n,0);
  8.  
  9.  
  10. front[0]=a[0]%p;
  11. back[n-1]=a[n-1]%p;
  12.  
  13.  
  14. for(i=1;i<n;i++)
  15. front[i]=(front[i-1]+a[i])%p;
  16.  
  17.  
  18. for(i=n-2;i>=0;i--)
  19. back[i]=(back[i+1]+a[i])%p;
  20.  
  21.  
  22.  
  23. unordered_map<int,set<int>>mm;
  24.  
  25.  
  26. for(i=n-1;i>=0;i--)
  27. {
  28. mm[back[i]].insert(i);
  29.  
  30. }
  31.  
  32.  
  33. if(front[n-1]%p==0)
  34. return 0;
  35.  
  36. int ans=n;
  37.  
  38. for(i=0;i<n;i++)
  39. {
  40. int rem1=front[i];
  41.  
  42. if(rem1==0)
  43. ans=min(ans,n-i-1);
  44.  
  45. if(back[i]==0)
  46. ans=min(ans,i);
  47.  
  48. int rem2=(p-front[i])%p;
  49.  
  50. if(mm[rem2].size()!=0)
  51. {
  52. auto it=mm[rem2].upper_bound(i);
  53.  
  54. if(it!=mm[rem2].end())
  55. ans=min(ans,*it-i-1);
  56.  
  57. }
  58. }
  59.  
  60. if(ans==n)
  61. ans=-1;
  62.  
  63. return ans;
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. }
  71. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:21: error: ‘vector’ has not been declared
     int minSubarray(vector<int>& a, int p) {
                     ^~~~~~
prog.cpp:3:27: error: expected ‘,’ or ‘...’ before ‘<’ token
     int minSubarray(vector<int>& a, int p) {
                           ^
prog.cpp: In member function ‘int Solution::minSubarray(int)’:
prog.cpp:5:18: error: ‘a’ was not declared in this scope
          int i,n=a.size();
                  ^
prog.cpp:7:9: error: ‘vector’ was not declared in this scope
         vector<int>front(n,0),back(n,0);
         ^~~~~~
prog.cpp:7:16: error: expected primary-expression before ‘int’
         vector<int>front(n,0),back(n,0);
                ^~~
prog.cpp:10:9: error: ‘front’ was not declared in this scope
         front[0]=a[0]%p;
         ^~~~~
prog.cpp:10:9: note: suggested alternative: ‘float’
         front[0]=a[0]%p;
         ^~~~~
         float
prog.cpp:10:23: error: ‘p’ was not declared in this scope
         front[0]=a[0]%p;
                       ^
prog.cpp:11:9: error: ‘back’ was not declared in this scope
         back[n-1]=a[n-1]%p;
         ^~~~
prog.cpp:23:9: error: ‘unordered_map’ was not declared in this scope
         unordered_map<int,set<int>>mm;
         ^~~~~~~~~~~~~
prog.cpp:23:23: error: expected primary-expression before ‘int’
         unordered_map<int,set<int>>mm;
                       ^~~
prog.cpp:28:13: error: ‘mm’ was not declared in this scope
             mm[back[i]].insert(i);
             ^~
prog.cpp:43:16: error: ‘min’ was not declared in this scope
            ans=min(ans,n-i-1);
                ^~~
prog.cpp:46:17: error: ‘min’ was not declared in this scope
             ans=min(ans,i);
                 ^~~
prog.cpp:50:16: error: ‘mm’ was not declared in this scope
             if(mm[rem2].size()!=0)
                ^~
prog.cpp:55:23: error: ‘min’ was not declared in this scope
                   ans=min(ans,*it-i-1);
                       ^~~
stdout
Standard output is empty