package org.wesome.dsalgo;
import java.util.Arrays;
public class InterleaveArray {
public static int[] interleaveArray(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int arr1Indx = 0, arr2Indx = 0, resultIndx = 0;
while (arr1Indx < arr1.length && arr1Indx < arr2.length) {
result[resultIndx] = arr1[arr1Indx];
arr1Indx++;
resultIndx++;
result[resultIndx] = arr2[arr2Indx];
arr2Indx++;
resultIndx++;
}
while (arr1Indx < arr1.length) {
result[resultIndx] = arr1[arr1Indx];
resultIndx++;
}
while (arr2Indx < arr2.length) {
result[resultIndx] = arr2[arr2Indx];
resultIndx++;
}
System.out.println("result = " + Arrays.toString(result));
return result;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.wesome.dsalgo.InterleaveArray.interleaveArray;
public class InterleaveArrayTest {
@Test
void InterleaveArrayTest1() {
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {2, 4, 6, 8, 10};
int result[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Assertions.assertArrayEquals(result, interleaveArray(arr1, arr2));
}
@Test
void InterleaveArrayTest2() {
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {90, 92, 94, 96, 98};
int result[] = {1, 90, 3, 92, 5, 94, 7, 96, 9, 98};
Assertions.assertArrayEquals(result, interleaveArray(arr1, arr2));
}
}
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()
}