SmallestDifference


Smallest Difference[Medium]

  • Write a function that takes in two non-empty arrays of integers,finds the pair of numbers (one from each array)whose absolute difference is closest to zero, and returns an array containing these two numbers, with the number from the first array in the first position.

  • Note that the absolute difference of two integers is the distance between them on the real number line. For example, the absolute difference of -5 and 5 is 10, and the absolute difference of -5 and -4 is 1.
  • You can assume that there will only be one pair of numbers with the smallest difference.

Sample Input

arrayOne = [-1, 5, 10, 20, 28, 3]
arrayTwo = [26, 134, 135, 15, 17]

Sample Output

[28,26]

Hints

Hint 1
Instead of generating all possible pairs of numbers,try somehow only looking at pairs that you know could actually have the smallest difference.How can you accomplish this?


Hint 2
Would it help if the two arrays were sorted?If the arrays were sorted and you were looking at a given pair of numbers,could you efficiently find the next pair of numbers to look at?What are the runtime implications of sorting the arrays?


Hint 3
Start by sorting both arrays,as per Hint #2.Put a pointer at the beginning of both arrays and evaluate the absolute difference of the pointer-numbers.If the difference is equal to zero, then you've found the closest pair;otherwise,increment the pointer of the smaller of the two numbers to find a potentially better pair.Continue until you get a pair with a difference of zero or until one of the pointers gets out of range of its array.


Methon 1

【O(nm)time∣O(1)space】
import java.util.*;

class Program {
  public static int[] smallestDifference(int[] arrayOne, int[] arrayTwo) {
    // Write your code here.
    //Arrays.sort(arrayOne);
    //Arrays.sort(arrayTwo);
    int[] nums = new int[2];
    
    int temp = Integer.MAX_VALUE;
    for(int i = 0; i <  arrayOne.length; i++){
      for(int j = 0; j <  arrayTwo.length; j++){
          int sum =  Math.abs(arrayOne[i] - arrayTwo[j]);
          if(temp >= sum){
             temp = sum;
             nums = new int[]{arrayOne[i],arrayTwo[j]};
          }
      }
   }
    return nums;
  }
}

Methon 2

【O(nlog(n)+mlog(n)time∣O(1)space】
import java.util.*;

class Program {
  public static int[] smallestDifference(int[] arrayOne, int[] arrayTwo) {
    Arrays.sort(arrayOne);
    Arrays.sort(arrayTwo);

    int[] nums = new int[2];

    int temp = Integer.MAX_VALUE;
    int one = 0;
    int two = 0;

    while(one < arrayOne.length && two < arrayTwo.length){
      int sum =  Math.abs(arrayOne[one] - arrayTwo[two]);

      if(temp >= sum){
        temp = sum;
        nums = new int[]{arrayOne[one],arrayTwo[two]};   
      }

      if(arrayOne[one] < arrayTwo[two]){
        one++;
      }else{
        two++;
      }
      
    }
    return nums;
  }
}



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 2379. Minimum Recolors to Get K Consecutive Black Blocks
  • 2471. Minimum Number of Operations to Sort a Binary Tree by Level
  • 1387. Sort Integers by The Power Value
  • 2090. K Radius Subarray Averages
  • 2545. Sort the Students by Their Kth Score