4/03/2017

Hackerrank solution for Divisible Sum Pairs in C++

Problem:- divisible pairs sum solution or Divisible Sum Pairs solution in C++ or Divisible Sum Pairs hacker rank solution in Java or Divisible Sum Pairs hacker rank solution c++ or Divisible Sum Pairs hacker rank solution c++ or Divisible Sum Pairs program in c or Divisible Sum Pairs solution in c or Divisible Sum Pairs hacker rank solution in python or Divisible Sum Pairs in java or Divisible Sum Pairs hacker rank solution in c or Hacker Rank Solution For Divisible Sum Pairs in C++ or Divisible Sum Pairs in C++ or Hacker Rank solution for Divisible Sum Pairs in C++.

Check This-
Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.

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

Explanation:- Let's take an example and with the help of an example try to understand the problem divisible pairs sum. Taking an input from the user and check if the number has divisible pairs sum or not.

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

6 3
1 3 2 6 1 2


So check the following conditions with all condition, here condition 1 is that 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 above 5 conditions is true for given input and if we divide the sum pair than no reminder is occurring. Hence we get our answer, the answer is 5.

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here

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


Solution:-

#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;

No comments:

Post a Comment