HackerRank Divisible Sum Pairs Problem Solution Yashwant Parihar, April 12, 2023April 12, 2023 In this post, We are going to solve HackerRank Divisible Sum Pairs Problem. Given an array of integers and a positive integer k, determine the number of (I, j) pairs where I < J and ar [I] + is divisible by k. Example ar = [ 1, 2, 3, 4, 5, 6] k = 5 Three pairs meet the criteria: [ 1, 5 ], [ 2, 3 ], and [ 4, 6 ]. Function Description Complete the divisibleSumPairs function in the editor below. divisibleSumPairs has the following parameter(s): int n: the length of the array ar int ar[n]: an array of integers int k: the integer divisor Returns– int: the number of pairs Input Format The first line contains 2 space-separated integers, n, and k.The second line contains n space-separated integers, each a value of arr[I]. Constraints 2 < n < 100 1 < k < 100 1 < arr[I] < 100 Sample Input STDIN Function ----- -------- 6 3 n = 6, k = 3 1 3 2 6 1 2 ar = [1, 3, 2, 6, 1, 2] Sample Output 5 HackerRank Divisible Sum Pairs Problem Solution Divisible Sum Pairs C Solutions #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 count = 0; scanf("%d %d",&n,&k); int *arr = malloc(sizeof(int) * n); for(int i = 0; i < n; i++){ scanf("%d",&arr[i]); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((arr[i] + arr[j]) % k == 0) { count++; } } } printf("%d", count); return 0; } Divisible Sum Pairs C++ Solution // Created by Kaidul Islam on 18/4/16. // Copyright © 2016 Kaidul Islam. All rights reserved. // // #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define rep(i, n) for(__typeof(n) i = 0; i < (n); i++) #define rrep(i, n) for(__typeof(n) i = (n) - 1; i >= 0; --i) #define rep1(i, n) for(__typeof(n) i = 1; i <= (n); i++) #define FOR(i, a, b) for(__typeof(b) i = (a); i <= (b); i++) #define forstl(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i) #define SIZE(v) ((int)v.size()) #define INF (1 << 30) #define PI acos(-1.0) #define bitcnt __builtin_popcount #define pb push_back #define ppb pop_back #define all(x) x.begin(), x.end() #define mem(x, y) memset(x, y, sizeof x) #define eps 1e-9 #define pii pair<int, int> #define couple make_pair #define vi vector<int> #define vpii vector< pii > #define si set<int> #define SDi(x) sf("%d", &x) #define SD2(x, y) sf("%d %d", &x, &y) #define SD3(x, y, z) sf("%d %d %d", &x, &y, &z) #define SDs(x) sf("%s", x) #define pf printf #define print(x) pf("%d ", x) #define println(x) pf("%d\n", x) #define output(x, y); pf("Case %d: %d", ++x, y) #define newLine pf("\n") #define sf scanf #define READ(f) freopen(f, "r", stdin) #define WRITE(f) freopen(f, "w", stdout) #if ( _WIN32 or __WIN32__ ) #define LLD "%I64d" #else #define LLD "%lld" #endif #define SDl(x) sf(LLD, &x) #define MAX5 100000 #define MAX7 10000000 #define MAX9 1000000000 #define MOD7 (MAX7 + 7) #define MOD9 (MAX9 + 9) typedef long long i64; typedef unsigned long long ui64; const i64 INF64 = (i64) 1E18; template<typename T> string toStr(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); } template<typename T> T toInt(string s) { T n = 0; istringstream sin(s); sin >> n; return n; } class TimeTracker { clock_t start, end; public: TimeTracker() { start = clock(); } ~TimeTracker() { end = clock(); fprintf(stderr, "%.3lf s\n", (double)(end - start) / CLOCKS_PER_SEC); } }; struct Point { int x, y; Point(): x(0), y(0) {} Point(int a, int b): x(a), y(b) {} bool operator < (const Point& other) const { return x < other.x; } }; // BitMask int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } int Check(int N, int pos) { return (N & (1 << pos)); } int toggle(int N, int pos) { if( Check(N, pos) ) return N = Reset(N, pos); return N = Set(N, pos); } // direction array //int dx[] = {0, -1, 0, 1}; //int dy[] = {-1, 0, 1, 0}; //int Dx[] = {0, -1, -1, -1, 0, 1, 1, 1}; //int Dy[] = {-1, -1, 0, 1, 1, 1, 0, -1}; //int row, col; //inline bool isValid(int i, int j) { // return i >= 0 and j >= 0 and i < row and j < col; //} /** Implementation **/ #define MAX 105 int arr[MAX]; int main() { int n, k; SD2(n, k); rep(i, n) SDi(arr[i]); int result = 0; FOR(i, 0, n - 1) { FOR(j, i + 1, n - 1) { int sum = arr[i] + arr[j]; result += (sum % k == 0); } } println(result); return 0; } Divisible Sum Pairs C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; 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 pair = 0; for(int i = 0; i<n; i++){ for(int j = i+1; j < n; j++){ if(j>i){ if((a[i] + a[j])%k == 0) pair++; } } } Console.WriteLine(pair); } } Divisible Sum Pairs Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'divisibleSumPairs' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. INTEGER k * 3. INTEGER_ARRAY ar */ public static int divisibleSumPairs(int n, int k, List<Integer> ar) { // Write your code here int pairsCount = 0; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if((ar.get(i) + ar.get(j)) % k == 0) { pairsCount++; } } } return pairsCount; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(firstMultipleInput[0]); int k = Integer.parseInt(firstMultipleInput[1]); List<Integer> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); int result = Result.divisibleSumPairs(n, k, ar); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } } Divisible Sum Pairs JavaScript Solution process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// const evenlyDivides = (x, y) => Number.isInteger(y / x); const sum = (x, y) => x + y; const getDivisibleSumPairs = (baseNum, k, arr) => { return arr.filter((val) => evenlyDivides(k, sum(baseNum, val))); }; const getDivisibleSumPairCount = (arr, k) => { return arr.reduce((prev, curr, idx) => { return prev + getDivisibleSumPairs(curr, k, arr.slice(idx + 1)).length; }, 0); }; function main() { var n_temp = readLine().split(' '); var n = parseInt(n_temp[0]); var k = parseInt(n_temp[1]); a = readLine().split(' '); a = a.map(Number); console.log(getDivisibleSumPairCount(a, k)); } Divisible Sum Pairs Python Solution #! /usr/bin/python3 # https://www.hackerrank.com/contests/w20/challenges/divisible-sum-pairs n, k = map(int, input().strip().split()) a = [int(i) for i in input().strip().split()] ret = 0 for i in range(0, n): for j in range(i + 1, n): if (a[i] + a[j]) % k == 0: ret = ret + 1 print(ret) Other Solutions HackerRank Migratory Birds Problem Solution HackerRank Day of the Programmer Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython