I came across this one problem where I am given two different arrays (can be in any order and any positive/negative integer is allowed) and I must find the smallest difference between the two arrays.

So if the difference between the first array and second array are 0, that would be the smallest difference since that means they are equal to each other.

To make things simpler, we will assume that there is only one pair that has the smallest difference. When you find it, you then output the pair in an array with those 2 values.

The solution below is my solution and I was wondering if there was a more efficient or better way to do this.

Worst case scenario, my solution is O(n

^{2}) time complexity and O(1) space

What do you all think?

using System; public class Program { public static int[] SmallestDifference(int[] arrayOne, int[] arrayTwo) { // Write your code here. int smallest = Int32.MaxValue; int[] smallestDiff = new int[] { 0, 0 }; for(int i = 0; i < arrayOne.Length; i++) { for(int j = 0; j < arrayTwo.Length; j++) { if(Math.Abs(arrayOne[i] - arrayTwo[j]) < smallest) { smallest = Math.Abs(arrayOne[i] - arrayTwo[j]); smallestDiff[0] = arrayOne[i]; smallestDiff[1] = arrayTwo[j]; } } } return smallestDiff; } }