Arrays may be reverse.
for example:
Int[] a = {1,2,3,4,5};
Int[] b = {9,8,7,6,5};
Int[] c = {3,0,9};
Int[] output = {1,2,3,4,5,6,7,8,9,0,3};
I can use nested loop to achieve it. But is there an easier Algorithm?
Arrays may be reverse.
for example:
Int[] a = {1,2,3,4,5};
Int[] b = {9,8,7,6,5};
Int[] c = {3,0,9};
Int[] output = {1,2,3,4,5,6,7,8,9,0,3};
I can use nested loop to achieve it. But is there an easier Algorithm?
In java i have tried with
int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
Follow this Link. It may help you
In kotlin you can achieve this in the following way:
val a = arrayOf(1,2,3,4,5)
val b = arrayOf(9,8,7,6,5)
val c = arrayOf(3,0,9)
val combined = ((a + b.reversed()).distinct()) + ((b + c.reversed()).distinct() - b)
Not sure I got what your requirements are, but here are my assumptions:
According to those assumptions, here's a possible implementation:
fun combineArrays(a: IntArray, b: IntArray): IntArray =
when {
a.last() == b.first() -> a + b.takeLast(b.size - 1)
a.last() == b.last() -> a + b.reversed().takeLast(b.size - 1)
else -> intArrayOf()
}
val a = intArrayOf(1, 2, 3, 4, 5)
val b = intArrayOf(9, 8, 7, 6, 5)
val c = intArrayOf(3, 0, 9)
val result = combineArrays(combineArrays(a, b), c)
println(result.joinToString())
This prints 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 3
.