# Find Pair With Smallest Difference in Java

Finding a pair with the smallest difference within an array is a common algorithmic problem in Java. It involves comparing differences between pairs of elements to identify the pair with minimal separation, and Java provides various solutions to address this challenge.

Example 1:

Input: A[] = {4, 7, 10, 15, 20}

B[] = {25, 8, 17, 12, 5}

Output: 2

Explanation: The pair with the smallest difference is (15, 17).

Example 2:

Input: A[] = {3, 12, 18, 25, 30}

B[] = {35, 50, 10, 5, 22}

Output: 2

Explanation: The pair with the smallest difference is (25, 22).

## Approach: Brute Force

The provided Java code utilizes a brute-force approach to find the smallest difference between elements in two arrays. Brute force involves exhaustively checking all possible combinations of elements, ensuring correctness but potentially leading to inefficiency for larger datasets.

### Algorithm:

Step 1: Initialize a variable minDiff to Integer.MAX_VALUE, representing the smallest difference found so far.

Step 2: Iterate through each element arrayA[i] in the first array (inputArrayA):

• For each element in the second array (inputArrayB), calculate the absolute difference between the current elements: diff = Math.abs(arrayA[i] - arrayB[j]).
• Update minDiff if the calculated difference diff is smaller than the current minimum difference.

Step 3: Return the smallest difference (minDiff) after completing the nested loops.

Step 4: In the main Method.

• Declare two arrays (inputArrayA and inputArrayB) representing the input arrays.
• Calculate the sizes of both arrays (sizeA and sizeB).
• Call the findSmallestDifference Method with the input arrays and their sizes to get the smallest difference.

Step 5: Display the result, indicating the smallest difference between the arrays.

### Implementation:

Filename: SmallestDifferenceFinder.java

Output:

```The smallest difference between the arrays is: 4
```

Time Complexity: The time complexity of the given code is O(N*M) due to nested loops, where N represents the size of arrayA and M represents the size of arrayB, as the algorithm iterates through all pairs of elements from both arrays.

Auxiliary Space: The auxiliary space complexity is O(1) as the code utilizes a constant amount of additional space, with variables like minDiff, i, and j, and the space requirements do not grow with the input array sizes.

## Approach: Two-Pointer Approach

The Two-Pointer Approach involves using two pointers that traverse a data structure such as an array or linked list, offering a space-efficient solution by avoiding the need for additional data structures. The technique is particularly useful for optimizing algorithms that require comparing elements in a sorted or partially ordered manner, as demonstrated in the provided code for finding the smallest difference between elements in two sorted arrays.

### Algorithm:

Step 1: Sort arrayA in ascending order using Arrays.sort().

Step 2: Sort arrayB in ascending order using Arrays.sort().

Step 3: Initialize indexA, indexB, and minDifference.

Step 4: Iterate while indexA < sizeA and indexB < sizeB:

• Calculate the absolute difference between arrayA[indexA] and arrayB[indexB].
• If the calculated difference < minDifference, update minDifference.
• Move the pointer for the smaller value (indexA or indexB).
1. Increment indexA if arrayA[indexA] < arrayB[indexB].
2. Increment indexB if arrayB[indexB] < arrayA[indexA].

Step 5: Return minDifference.

Step 6: Main Method:

• Define input arrays arrayA and arrayB.
• Calculate sizes sizeA and sizeB.
• Call findSmallestDifference with arrayA, arrayB, sizeA, and sizeB.

Step 7: Display the result.

### Implementation:

Filename: SmallestDifference.java

Output:

```The smallest difference between the arrays is: 4
```

Time Complexity: The time complexity of the code is O(n log n + sizeA + sizeB) due to the sorting operation (O(n log n)) and the subsequent linear iteration through the sorted arrays (O(sizeA + sizeB)).

Auxiliary Space: The auxiliary space complexity is O(1) as the algorithm utilizes a constant amount of extra space, irrespective of the input array sizes.