# Zigzag Traversal Of Two Arrays

Posted: 20 Mar, 2021

Difficulty: Moderate

#### You are given two lists/arrays ‘ARR1’ and ‘ARR2’ consisting of ‘N’ and ‘M’ integers respectively.

#### Your task is to traverse both ‘ARR1’ and ‘ARR2’ together in Zigzag order and return a list/array representing their Zigzag traversal.

#### In ZigZag traversal, we iterate the arrays/list alternatively starting with ‘ARR1’ i.e. in order ARR1[0], ARR2[0], ARR1[1], ARR2[1], ARR1[2], ARR2[2] and so on. See the example for more clarity.

##### Example:

```
Consider ARR1 = [1, 2] and ARR2 = [3, 4, 5, 6], then their Zigzag traversal will be represented by the list/array [1, 3, 2, 4, 5, 6].
We traverse in order ARR1[0], ARR2[0], ARR1[1], ARR2[1], ARR2[2], ARR2[3]. Note, ARR1 is completely traversed at the 3rd position, and after which we simply iterate over ‘ARR2’.
```

##### Input format:

```
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line of each test case consists of two single space-separated integers ‘N’ and ‘M’ representing the number of integers in ‘ARR1’ and ‘ARR2’ respectively.
The second line of each test case consists of ‘N’ single space-separated integers representing array ‘ARR1’
The third line of each test case consists of ‘M’ single space-separated integers representing array ‘ARR2’
```

##### Output format :

```
For each test case, return an array of size ‘N’ + ‘M’ that contains integers representing Zigzag traversal of ‘ARR1’ and ‘ARR2’.
```

#### Note:

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

##### Constraints:

```
1 <= T <= 50
0 <= N <= 10^4
0 <= M <= 10^4
0 <= ARR1[i] <= 10^9
0 <= ARR2[i] <= 10^9
N + M > 0
Time limit: 1 sec
```

Approach 1

This approach is straightforward, we will keep two pointers **‘p’ **and **‘q’**. Initially we keep both pointers at the 0th index i.e **‘p’ = 0** and **‘q’ = 0**. After that we simply run a loop where **‘i’** ranges from 0 to **‘N’ + ’M’ - 1,** and if **‘i’ **is even then we visit **‘ARR1[p]’** and increment** ‘p’** by ‘1’ otherwise we visit **‘ARR2[q]’ **and increment** ‘q’ **by 1. Step by step algorithm is described below -:

**Algorithm:**

- Create an integer list/arr
**‘RESULT’**of size**‘N’+’M’**. - Initialize two integer variables
**‘p’:= 0**and**‘q’:= 0.** - Run a loop where
**‘i’**ranges from 0 to**‘N’ + ‘M’ - 1**and in each iteration do the following -:- If
**‘p’ = ‘N’**then do**‘RESULT[i]’:= ‘ARR2[q]’**and increment**‘q’**by 1. - If
**‘q’ = ‘M’**then do**‘RESULT[i]’:= ‘ARR2[p]’**and increment**‘p’**by 1. - If both
**‘p’ < ‘N’**and**‘q’ < ‘M’**then when**‘i’**is even, do**‘RESULT[i]’:= ‘ARR2[p]’**and increment**‘p’**by 1, and when**‘i’**is odd, do**‘RESULT[i]’:= ‘ARR2[q]’**and increment**‘q’**by 1.

- If
- Return
**‘RESULT’**.

SIMILAR PROBLEMS

# Connecting Ropes

Posted: 12 Nov, 2021

Difficulty: Hard

# Insertion Sort

Posted: 30 Nov, 2021

Difficulty: Easy

# Subarrays With Zero Sum

Posted: 1 Dec, 2021

Difficulty: Easy

# Find Student

Posted: 1 Dec, 2021

Difficulty: Easy

# Smaller Than Triplet Sum

Posted: 1 Dec, 2021

Difficulty: Moderate