Hello **Programmers/Coders,** Today we are going to share ** solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for **Closest Numbers** **in Java-HackerRank Problem**. We are providing the **correct and tested solutions** of coding problems present on **HackerRank**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**Introduction To Algorithm**

The word **Algorithm** means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

**Advantages of Algorithms:**

- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

** Link for the Problem** – Closest Numbers– Hacker Rank Solution

Closest Numbers – Hacker Rank Solution

**Problem:**

Sorting is useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses as well. In this case, it will make it easier to determine which pair or pairs of elements have the smallest absolute difference between them.

**Example**

Sorted, . Several pairs have the minimum difference of : . Return the array .

**Note**

As shown in the example, pairs may overlap.

Given a list of unsorted integers, , find the pair of elements that have the smallest absolute difference between them. If there are multiple pairs, find them all.

**Function Description**

Complete the *closestNumbers* function in the editor below.

closestNumbers has the following parameter(s):

*int arr[n]:*an array of integers

**Returns**

– *int[]:* an array of integers as described

**Input Format**

The first line contains a single integer , the length of .

The second line contains space-separated integers, .

**Constraints**

- All are unique in .

**Output Format**

**Sample Input 0**

10 -20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854

**Sample Output 0**

-20 30

**Explanation 0***(30) – (-20) = 50*, which is the smallest difference.

**Sample Input 1**

12 -20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854 -520 -470

**Sample Output 1**

-520 -470 -20 30

**Explanation 1***(-470) – (-520) = 30 – (-20) = 50*, which is the smallest difference.

**Sample Input 2**

4 5 4 3 2

**Sample Output 2**

2 3 3 4 4 5

**Explanation 2**

Here, the minimum difference is *1*. Valid pairs are *(2, 3)*, *(3, 4)*, and *(4, 5)*.

Closest Numbers – Hacker Rank Solution

import java.util.Arrays; import java.util.Scanner; /** * @author Techno-RJ * */ public class ClosestNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int a[] = new int[t]; for (int i = 0; i < t; i++) { a[i] = sc.nextInt(); } Arrays.sort(a); int minDiff = Integer.MAX_VALUE; int min1 = 0; int min2 = 0, minIndex = -1; String result = ""; for (int i = 1; i < t; i++) { int diff = Math.abs(a[i] - a[i - 1]); if (diff < minDiff) { minDiff = diff; min1 = a[i - 1]; min2 = a[i]; minIndex = i; result = min1 + " " + min2; } } for (int i = minIndex + 1; i < t; i++) { if (minDiff == Math.abs(a[i] - a[i - 1])) { result = result + " " + a[i - 1] + " " + a[i]; } } System.out.println(result); sc.close(); } }