Thursday, August 2, 2018

#codingexercise
Get count of subarrays with sum divisible by m
int GetCountDivByK(List<int> A, int k)
{
var mod = new int[k];
int sum = 0;
for (int i = 0; i < A.Count; i++)

    sum += A[i];
    mod[((sum%k)+k)%k] += 1;
}
int result = 0;
for (int i =0; i < k; i++)
    if (mod[i] > 1)
          result += (mod[i] * (mod[i]-1)) / 2;
result += mod[0];
return result;



Find the number of zeros in the binary representation of a number: 
int GetCountUnsetBits(int n) 
{ 
    int set = 0; 
    int unset = 0; 
    while (n) 
    { 
        if (n & 0x1) { 
             set++; 
        } else { 
             unset++; 
        } 
        n = n >> 1; 
    } 
    return unset; 

} 
}

No comments:

Post a Comment