@@ -22,6 +22,93 @@ Contributions are always welcome!
2222- Provide link for further reading (optional)
2323- Send a Pull Request (PR) against main branch and mention issue number in the request
2424
25+ # Example program
26+
27+ ``` go
28+ /*
29+ Write a function that takes in two non-empty arrays of integers, finds the pair of numbers (one from each array)
30+ whose absolute difference is closest to zero, and returns an array containing these two numbers, with the number from
31+ the first array in the first position.
32+
33+ Note that the absolute difference of two integers is the distance between them on the real number line.
34+ For example, the absolute difference of -5 and 5 is 10, and the absolute difference of -5 and -4 is 1.
35+
36+ You can assume that there will only be one pair of numbers with the smallest difference.
37+
38+ Sample Input Array1 = [-1, 5, 10, 20, 28, 3]
39+ Sample Input Array2 = [26, 134, 135, 15, 17]
40+
41+ Sample Output = [28, 26]
42+
43+ Explanation :
44+
45+ This code implements the Smallest Difference problem which takes two arrays of integers as input and returns a pair of integers,
46+ one from each array, with the smallest absolute difference between them.
47+
48+ The function first initializes two variables current and smallest to the maximum integer value. It then sorts both input arrays
49+ in ascending order using the sort.Ints function from the sort package.
50+
51+ The function then iterates through both arrays using two pointers, idx1 and idx2, initialized to 0. Inside the loop, it compares
52+ the elements at the current indices of the two arrays, first and second, and calculates the absolute difference between
53+ them in the current variable.
54+
55+ If current is smaller than the smallest variable, it updates smallest to current and assigns the current pair of integers
56+ to the result variable.
57+
58+ The function returns the result variable, which contains the pair of integers with the smallest absolute difference.
59+
60+ If there are identical integers in the two input arrays, the function will return them immediately, without any further comparisons.
61+
62+ O(nlog(n) + mlog(m)) time | O(1) space - where n is the length of the first input array and m is the length of the second input array
63+ */
64+
65+ package main
66+
67+ import (
68+ " math"
69+ " sort"
70+ )
71+
72+ // SmallestDifference takes two integer slices as input and returns a slice with two integers.
73+ // The two integers in the returned slice have the smallest absolute difference among all pairs
74+ // of integers from the two input slices.
75+ func SmallestDifference (array1 , array2 []int ) []int {
76+ // Initialize variables for the smallest difference and the current difference being calculated
77+ current , smallest := math.MaxInt32 , math.MaxInt32
78+ // Sort the input slices
79+ sort.Ints (array1)
80+ sort.Ints (array2)
81+ // Initialize variables for the indices for the two slices
82+ idx1 , idx2 := 0 , 0
83+ // Initialize an empty slice for the result
84+ result := []int {}
85+ // Loop through the two slices until we reach the end of one of the slices
86+ for idx1 < len (array1) && idx2 < len (array2) {
87+ // Get the values at the current indices for the two slices
88+ first , second := array1[idx1], array2[idx2]
89+ // Calculate the current difference between the two values
90+ if first < second {
91+ current = second - first
92+ idx1++
93+ } else if second < first {
94+ current = first - second
95+ idx2++
96+ } else {
97+ // If the two values are equal, we can return the pair
98+ return []int {first, second}
99+ }
100+ // Update the smallest difference and result slice if the current difference is smaller
101+ if smallest > current {
102+ smallest = current
103+ result = []int {first, second}
104+ }
105+ }
106+ // Return the pair with the smallest absolute difference
107+ return result
108+ }
109+
110+ ```
111+
25112## What if the problem you want to add is not present in the issue?
26113
27114- You can suggest an idea for this project under issues tab (add list of questions you think it is important, it will be assigned to you)
0 commit comments