Divisible Sum Pairs Hackerrank solution in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <limits.h> | |
#include <stdbool.h> | |
int divisibleSumPairs(int n, int k, int ar_size, int* ar) { | |
// Complete this function | |
int i,j; | |
int counter=0; | |
for(i=0;i<n;i++){ | |
for(j=0;j<n;j++){ | |
if(i<j && (ar[i]+ar[j])%k==0){ | |
counter++; | |
} | |
} | |
} | |
return counter; | |
} | |
int main() { | |
int n; | |
int k; | |
scanf("%i %i", &n, &k); | |
int *ar = malloc(sizeof(int) * n); | |
for(int ar_i = 0; ar_i < n; ar_i++){ | |
scanf("%i",&ar[ar_i]); | |
} | |
int result = divisibleSumPairs(n, k, n, ar); | |
printf("%d\n", result); | |
return 0; | |
} |
Comments
Post a Comment