Subarrays problems

1)   Number of subarrays of a array whose sum is K

2)  number of sub arrays of a array whose sum is multiple of  K

3) Number of subarrays of a array whose xor is Maximum (Lalit kundu post ,Quora )
http://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems

4) number of distinct subarrays of a array
http://stackoverflow.com/questions/17513359/number-of-distinct-subarrays







2) solution (number of subarrays whose sum is multiple of K)

#include<bits/stdc++.h>

using namespace std;

int B[1000];
int main()
{
  int n,k;
  cin>>n>>k;
  int A[n];

     for(int i=0;i<n;i++)
        cin>>A[i];

int s,i,ans;

B[0]++;
s = 0;

for (i = 0;i<= n-1;i++)
{
  s=(s + A[i])%k;
  B[s]++;
}

ans=0;
for(i = 0 ;i<= k-1;i++)
{
cout<<B[i]<<" ";
  ans+=(B[i]*(B[i]-1))/2;
}

cout<<endl;
  cout<<ans<<endl;
return 0;
}


Comments

Popular posts from this blog

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )