Divisible Sum Pairs HackerRank Solution in C, C++, Java, Python

You are given an array of n integers,ar=ar[0],ar[1],…,ar[n-1] , and a positive integer,k . Find and print the number of (i,j) pairs where i<j and ar[i] + ar[j] is divisible by .

For example,ar = [1,2,3,4,5,6]  and k=5. Our three pairs meeting the criteria are [1,4],[2,3] and [4,6].

Function Description

Complete the divisibleSumPairs function in the editor below. It should return the integer count of pairs meeting the criteria.

divisibleSumPairs has the following parameter(s):

  • n: the integer length of array ar
  • ar: an array of integers
  • k: the integer to divide the pair sum by

Input Format

The first line contains 2 space-separated integers,n  and k.

The second line contains n space-separated integers describing the values of ar[ar[0],ar[1],….,ar[n-1]].


  • 2<=n<=100
  • 1<=k<=100
  • 1<=ar[i]<=100

Output Format

Print the number of (i,j) pairs where i<j and  a[i]+a[j]  is evenly divisible by k.

Sample Input

6 3

1 3 2 6 1 2

Sample Output



Divisible Sum Pairs HackerRank Solution in C

#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;
int k;
int ans = 0, i, j, t;
scanf("%d %d",&n,&k);
int *a = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
for (i=0;i<n;i++)
if ((a[i]+a[j])%k==0)
return 0;


Divisible Sum Pairs HackerRank Solution in C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main(){
int n;
int k;
int count = 0;
cin >> n >> k;
vector<int> a(n);
for(int a_i = 0;a_i < n;a_i++){
cin >> a[a_i];

for(int i =0 ; i < n -1 ; i++){
for(int j=i+1 ; j < n ; j++){
if( (a[i]+a[j])%k ==0){

cout << count;
return 0;


Divisible Sum Pairs HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
int cnt=0;
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
for(int a_i=0; a_i < n-1; a_i++){
for(int a_j=a_i+1; a_j< n; a_j++){
if( (a[a_i]+a[a_j])%k==0)


Divisible Sum Pairs HackerRank Solution in Python
import sys
n,k=raw_input().strip().split(' ')
a=map(int,raw_input().strip().split(' '))
for i in range(n):
for i in range(n):
for j in range(i+1,n):
if(a[j]%k!=0 and (a[j]+a[i])%k==0):
print res


Divisible Sum Pairs HackerRank Solution in C#

using System;

class Solution

static void Main(String[] args)
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);

int count = 0;

for (int j = 0; j < n; j++)
for (int i = 0; i < j; i++)
if ((a[i] + a[j]) % k == 0)



Attempt – Divisible Sum Pairs HackerRank Challenge

Link – https://www.hackerrank.com/challenges/divisible-sum-pairs

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/migratory-birds-hackerrank-solution/

Leave a Comment