Smallest Difference Pair of Values Between Two Sorted Arrays

package org.wesome.dsalgo;

import lombok.AllArgsConstructor;
import lombok.Data;

public class SmallestDifference {
    public static TwoNumber SmallestDifference(int[] arr1, int[] arr2) {
        TwoNumber twoNumber = null;
        if (0 == arr1.length) {
            System.out.println("arr1 is null");
            return twoNumber;
        }
        if (0 == arr2.length) {
            System.out.println("arr2 is null");
            return twoNumber;
        }
        int min = Integer.MAX_VALUE, diff = Integer.MAX_VALUE;
        int inx1 = 0, indx2 = 0;
        while (inx1 < arr1.length && indx2 < arr2.length) {
            diff = Math.abs(arr1[inx1] - arr2[indx2]);
            if (0 == diff) {
                System.out.println("element from array 1 = " + arr1[inx1] + " element from array 2 = " + arr2[indx2]);
                return twoNumber;
            }
            if (min > diff) {
                min = Math.abs(Math.min(min, diff));
                twoNumber = new TwoNumber(arr1[inx1], arr2[indx2]);
            }
            if (arr1[inx1] > arr2[indx2]) {
                indx2++;
            } else {
                inx1++;
            }
        }
        while (inx1 < arr1.length) {
            diff = Math.abs(arr1[inx1] - arr2[indx2 - 1]);
            if (0 == diff) {
                System.out.println("element from array 1 = " + arr1[inx1] + " element from array 2 = " + arr2[indx2]);
                twoNumber = new TwoNumber(arr1[inx1], arr2[indx2]);
                return twoNumber;
            }
            if (min > diff) {
                min = Math.abs(Math.min(min, diff));
                twoNumber = new TwoNumber(arr1[inx1], arr2[indx2]);
            }
            inx1++;
        }
        while (indx2 < arr2.length) {
            diff = Math.abs(arr1[inx1 - 1] - arr2[indx2]);
            if (0 == diff) {
                System.out.println("element from array 1 = " + arr1[inx1] + " element from array 2 = " + arr2[indx2]);
                twoNumber = new TwoNumber(arr1[inx1], arr2[indx2]);
                return twoNumber;
            }
            if (min > diff) {
                min = Math.abs(Math.min(min, diff));
                twoNumber = new TwoNumber(arr1[inx1], arr2[indx2]);
            }
            indx2++;
        }
        return twoNumber;
    }

    @Data
    @AllArgsConstructor
    public static class TwoNumber {
        int num1, num2;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class SmallestDifferenceTest {
    @Test
    void smallestDifferenceTest1() {
        int[] arrayOne = new int[]{-1, 3, 5, 10, 20, 28};
        int[] arrayTwo = new int[]{15, 17, 26, 134, 135};
        SmallestDifference.TwoNumber twoNumber = SmallestDifference.SmallestDifference(arrayOne, arrayTwo);
        assertAll(() -> assertNotNull(twoNumber, " object should not be null"), () -> assertEquals(28, twoNumber.getNum1()), () -> assertEquals(26, twoNumber.getNum2()));
    }

    @Test
    void smallestDifferenceTest2() {
        int[] arrayOne = new int[]{15, 17, 26, 134, 135};
        int[] arrayTwo = new int[]{-1, 3, 5, 10, 20, 28};
        SmallestDifference.TwoNumber twoNumber = SmallestDifference.SmallestDifference(arrayOne, arrayTwo);
        assertAll(() -> assertNotNull(twoNumber, " object should not be null"), () -> assertEquals(26, twoNumber.getNum1()), () -> assertEquals(28, twoNumber.getNum2()));
    }

    @Test
    void smallestDifferenceTest3() {
        int[] arrayOne = new int[]{0, -6, 4, 6, 5, -2};
        int[] arrayTwo = new int[]{-4, 8, 2, 3, 10, 9};
        SmallestDifference.TwoNumber twoNumber = SmallestDifference.SmallestDifference(arrayOne, arrayTwo);
        assertAll(() -> assertNotNull(twoNumber, " object should not be null"), () -> assertEquals(6, twoNumber.getNum1()), () -> assertEquals(8, twoNumber.getNum2()));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on