One common approach to finding common elements in three sorted arrays is to use a “merge” strategy, where you compare the elements of the three arrays and find the ones that are common. Here’s a step-by-step algorithm to accomplish this:
- Initialize three pointers, one for each of the three arrays, starting at the beginning of each array.
- While all three pointers are still within their respective arrays, do the following:
- Compare the elements pointed to by the three pointers. If they are all equal, you have found a common element. Add it to a result array or print it out, and advance all three pointers to the next element in each array.
- If the element pointed to by the first pointer is less than either of the other two elements, advance the first pointer to the next element.
- If the element pointed to by the second pointer is less than either of the other two elements, advance the second pointer to the next element.
- If the element pointed to by the third pointer is less than either of the other two elements, advance the third pointer to the next element.
- When any one of the pointers reaches the end of its respective array, the algorithm is finished. All common elements have been found.
Here’s an example Kotlin code for finding common elements in three sorted arrays:
fun main() {
val arr1 = intArrayOf(1, 4, 6, 8, 10)
val arr2 = intArrayOf(2, 4, 5, 6, 10)
val arr3 = intArrayOf(3, 4, 6, 9, 10)
val common = findCommonElements(arr1, arr2, arr3)
if (common.isNotEmpty()) {
println("Common elements: ${common.contentToString()}")
} else {
println("No common elements found.")
}
}
fun findCommonElements(arr1: IntArray, arr2: IntArray, arr3: IntArray): IntArray {
var i = 0
var j = 0
var k = 0
val result = mutableListOf<Int>()
while (i < arr1.size && j < arr2.size && k < arr3.size) {
when {
arr1[i] == arr2[j] && arr2[j] == arr3[k] -> {
result.add(arr1[i])
i++
j++
k++
}
arr1[i] < arr2[j] -> i++
arr2[j] < arr3[k] -> j++
else -> k++
}
}
return result.toIntArray()
}
- The function takes three input parameters,
arr1
,arr2
, andarr3
, which are arrays of integers. It returns an array of integers that contains the common elements in the three input arrays. - The function initializes three variables,
i
,j
, andk
, to 0. These variables will be used as pointers to iterate through the three arrays. - The function creates an empty mutable list called
result
, which will be used to store the common elements found in the input arrays. - The function enters a
while
loop that runs as long asi
,j
, andk
are all within the bounds of their respective arrays. Inside the loop, the function performs the following steps:a. It checks if the elements pointed to byi
,j
, andk
are all equal. If they are, it means that the current element is a common element, so it adds the element to theresult
list and increments all three pointers.b. If the element pointed to byi
is less than the element pointed to byj
, the function incrementsi
to move to the next element inarr1
. This is because, since the arrays are sorted, ifarr1[i]
is less thanarr2[j]
,arr1[i]
cannot be a common element witharr2[j]
or any subsequent elements inarr2
.c. If the element pointed to byj
is less than the element pointed to byk
, the function incrementsj
to move to the next element inarr2
. This is because, since the arrays are sorted, ifarr2[j]
is less thanarr3[k]
,arr2[j]
cannot be a common element witharr3[k]
or any subsequent elements inarr3
.d. If none of the above conditions are true, the function incrementsk
to move to the next element inarr3
. This is because, since the arrays are sorted, ifarr3[k]
is less thanarr1[i]
orarr2[j]
,arr3[k]
cannot be a common element with any subsequent elements inarr1
orarr2
. - After the
while
loop exits, the function returns theresult
list as an integer array by calling thetoIntArray()
method on the list.
The time complexity of the findCommonElements
function is O(n), where n is the total number of elements in the input arrays. This is because the function only iterates through each of the input arrays once, and the running time of each iteration is constant.
The space complexity of the function is also O(n), because the result
list can potentially contain all n elements of the input arrays. However, in the worst case scenario where there are no common elements, the result
list will be empty, so the space complexity will be O(1).
Overall, this algorithm has a linear time complexity, which is optimal for finding common elements in three sorted arrays.