Friday 29 December 2017

Divisible Pairs Sum Hacker Rank Solution in C

Problem link :https://www.hackerrank.com/challenges/linkedin-practice-divisible-sum-pairs/problem

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,j,i;
    int k,c[100]={0},res=0;
    scanf("%d %d",&n,&k);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
        c[a[a_i]%k]++;
    }
    res=c[0]*(c[0]-1)/2;
    for(i=1,j=k-1;i<j;i++,j--)
        res=res+(c[i]*c[j]);
    if(k%2 == 0)
        res=res+c[k/2]*(c[k/2]-1)/2;
    printf("%d",res);
    return 0;
}

No comments:

Post a Comment