Divisible Sum Pairs Hackerrank problem solution

Divisible Sum Pairs Hackerrank

You are given an array of n integers, a0,a1,..,an-1, and a positive integer,k. Find and print the number of (i,j)pairs where ii+aj is evenly divisible by K.

Input Format

The first line contains 2 space-separated integers, n and k, respectively.
The second line contains n space-separated integers describing the respective values of a0,a1,..,an-1.

Constraints
2<=n<=100
1<=k<=100
1<=ai<=100

Output Format

Print the number of (i,j)pairs where ii+aj is evenly divisible by K.

Sample Input

6 3
1 3 2 6 1 2
Sample Output

5

Solution

We can check every pair, one by one, and use (ai+aj) %k==0 to check if the pair is valid.

For Learning More Visit: https://coderinme.com/

```#include <bits/stdc++.h>
using namespace std;

int n, k;
int a[N];

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

int res = 0;
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
if((a[i] + a[j]) % k == 0) res++;
cout << res << endl;
return 0;
}```
```#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 l;
int count = 0;
cin >> n >> l;
vector<int> a(n);
for(int i = 0;i < n;i++){
cin >> a[i];
}
for(int i = 0;i < n;i++)
{
for(int k = i+1; k < n;k++)
{
if((a[i] + a[k]) % l == 0 && i < k)
count++;
}
}
cout << count;
return 0;
}```

python

``````n, k = map(int,raw_input().split())
arr = map(int,raw_input().split())
count = 0
for i in range(0, n):
for j in range(i+1, n):
if (arr[i] + arr[j]) % k == 0:
count += 1
print count``````

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) {
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();
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((a[i] + a[j]) % k == 0) count++;
}
}
System.out.println(count);
}
}
``````

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi