Nov. 22, 2021 hackerrank-problems-and-solutions Neeraj Shankar

Dvisible Sum Pair Problem

First, we will create a class "Divisible_Sum_Pair_Logic" which  have static function "find_the_pair" that contains the logic to find all such pairs.

class Divisible_Sum_Pair_Logic{
static void find_the_pair(int n, int k, int arr[]){
int count =0;
// iterate each array
for(int i =0;i<n-1;i++){
for(int j=i+1;j<n;j++){
int sumOfPair = arr[i] +arr[j];
if(sumOfPair % k == 0){
count ++;

// print the count of such pairs
System.out.println("The number of such pairs in the provided list of numbers are: " +count);


In our main method, we have created Scanner class object to take the input from the user

public class Divisible_Sum_Pair {
public static void main(String[] args) {
Scanner userInput = new Scanner(;
// Take the count of all numbers to be entered
System.out.println("Please enter the count of numbers: ");
int n = userInput.nextInt();


Finally, we will pass the inputs using the reference variable "userInput" for n = count of numbers to be evaluated, k= number which we will use to divide the taken pair  of numers and arr =  store all the numbers taken in form of array.

  // Take the number which will divide the pairs
System.out.println("[Please enter the number to divide them: ");
int k = userInput.nextInt();

// Take all the numbers
int[] arr = new int[n];
System.out.println("Please enter all the numbers: ");
for(int i =0;i<n;i++){
arr[i] = userInput.nextInt();

// Calling the function that contains the logic
Divisible_Sum_Pair_Logic.find_the_pair(n, k, arr);


Neeraj Shankar

A result-oriented professional with over 4 years’ experience under different job roles which includes over 2 year of experience with Software Engineer (as also a freelancer)


Leave a comment