08/03/2023

Divisible Sum Pairs Hackerrank Solution in C++ | Hackerrank

Divisible Sum Pairs Hackerrank Solution C++. Given an array of integers and a positive integer k, determine the number of (i,j) pairs where i < j and ar[i] + ar[j] is divisible by k. Let's take an example and with the help of an example try to understand the problem of divisible pairs sum. Take input from the user and check if the number has divisible pairs sum or not.

Divisible Sum Pairs Hackerrank Solution in C++

Example

ar = [1, 2, 3, 4, 5, 6]
k = 5

Three pairs meet the criteria: [1,4] [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 <= ar[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

Explanation

Here are the 5 valid pairs when k = 3:

(0,2) -> ar[0] + ar[2] = 1 + 2 = 3
(0,5) -> ar[0] + ar[5] = 1 + 2 = 3
(1,3) -> ar[1] + ar[3] = 3 + 6 = 9
(2,4) -> ar[2] + ar[4] = 2 + 1 = 3
(4,5) -> ar[3] + ar[5] = 1 + 2 = 3

Divisible Sum Pairs Hackerrank Solution in C++



#
include <bits/stdc++.h>
using namespace std; string ltrim(const string &); string rtrim(const string &); vector<string> split(const string &); /* *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 */ /*Code Start Here*/ int divisibleSumPairs(int n, int k, vector<int> ar) { int iPairs = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (((ar[i] + ar[j]) % k) == 0) iPairs++; } } return iPairs; } /*Code End Here*/ int main() { ofstream fout(getenv("OUTPUT_PATH")); string first_multiple_input_temp; getline(cin, first_multiple_input_temp); vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp)); int n = stoi(first_multiple_input[0]); int k = stoi(first_multiple_input[1]); string ar_temp_temp; getline(cin, ar_temp_temp); vector<string> ar_temp = split(rtrim(ar_temp_temp)); vector<int> ar(n); for (int i = 0; i < n; i++) { int ar_item = stoi(ar_temp[i]); ar[i] = ar_item; } int result = divisibleSumPairs(n, k, ar); fout << result << "\n"; fout.close(); return 0; } string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int> (isspace))) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int> (isspace))).base(), s.end() ); return s; } vector<string> split(const string &str) { vector<string> tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }

Divisible Sum Pairs Explanation


Logic is very simple we have to take a number and compare if the first number is less than the second number then the first number + the second number is divided by the number input from the user If the sum is divisible by the number then increases the count or else for condition do not increase a count variable. See the explanation section for an example.

User Input: 6 is a size of an array and the number 3 is for dividing the pair sum.

6 3
1 3 2 6 1 2

So check the following conditions with all conditions, here condition 1 is that the first number should be less than the second number.

1 + 3 = 3
1 + 2 = 3
3 + 6 = 9
2 + 1 = 3
1 + 2 = 3

Only all the above 5 conditions are true for the given input and if we divide the sum pair then no reminder is occurring. Hence we get our answer, and the answer is 5.

Submit your solution here: Click here

Tip: Before copying the solution I recommended please read this full article, this will help you to build your own logic.

Hackerrank Divisible Sum Pairs Solution in C++


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

int main()
{
	int i, j, k, n, count = 0;

	cin >> n >> k;

	vector<int> array(n);

	for (i = 0; i < n; i++)
	{
		cin >> array[i];
	}

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (i < j)
			{
				if ((array[i] + array[j]) % k == 0)
					count++;
			}
		}
	}

	cout << count;

	return 0;

}

The Output of Divisible Sum Pairs Hackerrank Solution


The Output of Divisible Sum Pairs Hackerrank Solution

Similar to Divisible Sum Pairs

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: