# Shift the elements between two arrays in Z form

Given two arrays ** arr1[]** and

**both of size**

**arr2[]****, the task is to shift each element of the cell in**

**N****form such that arr2[0] is in arr1[0], arr1[0] is in arr2[1], arr2[1] is in arr1[1] and so on and arr1[N-1] is in arr2[0].**

**Z**** Examples**:

arr1[] = {61, 45, 19, 33, 59, 7, 42, 24, 98, 77}, arr2[] = {86, 52, 10, 36, 22, 5, 98, 91, 13, 6}Input:arr1[] = {86, 52, 10, 36, 22, 5, 98, 91, 13, 6}Output:

arr2[] = {77, 61, 45, 19, 33, 59, 7, 42, 24, 98}Explanation:

arr1[] = {6, 24, 39, 99, 67}, arr2[] = {12, 84, 9, 13, 5}Input:arr1[] = {12, 84, 9, 13, 5}Output:

arr2[] = {67, 6, 24, 39, 99}

** Approach: **Let’s understand how the function

**works step by step:**

**zshift**- The variable
is assigned the last element of**t**, which will be used for swapping later.**arr1** - The loop starts from the last index
**(****N-1**and iterates down to the second index (**)**`1`

). - Inside the loop, the current element of
is assigned to the corresponding index in**arr2**, and the previous element of**arr1**is assigned the current element of**arr1**. This process effectively shifts the elements in a Z-form between the arrays.**arr2** - After the loop, the first element of
is assigned to the first element of**arr2**, and the original last element of**arr1**`arr1`

(stored in) is assigned to the first element of**t**. This finalizes the shifting process.**arr2**

Below is the implementation of the above approach:

```
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to perform Z-form shifting
// between two arrays
void zshift(int* arr1, int* arr2, int N)
{
// Store the last element of arr1 in t
int t = arr1[N - 1];
// Shift elements of arr2[i] to
// arr1[i]
for (int i = N - 1; i >= 1; i--) {
arr1[i] = arr2[i];
// Shift previous elements of
// arr1 to current index of arr2
arr2[i] = arr1[i - 1];
}
// Shift the first element of arr2 to
// arr1
arr1[0] = arr2[0];
// Shift the original last element of
// arr1 to arr2
arr2[0] = t;
}
// Drivers code
int main()
{
int arr1[] = { 61, 45, 19, 33, 59, 7, 42, 24, 98, 77 };
int arr2[] = { 86, 52, 10, 36, 22, 5, 98, 91, 13, 6 };
int N = sizeof(arr1) / sizeof(arr1[0]);
zshift(arr1, arr2, N);
/// Print the elements of arr1
for (int i = 0; i < N; i++)
cout << arr1[i] << " ";
cout << endl;
// Print the elements of arr2
for (int i = 0; i < N; i++)
cout << arr2[i] << " ";
return 0;
}
```

```
//Java program for the above approach
import java.util.Arrays;
class GFG {
// Function to perform Z-form shifting between two arrays
public static void zshift(int[] arr1, int[] arr2, int N)
{
int t = arr1[N - 1]; // Store the last element of arr1 in t
for (int i = N - 1; i >= 1; i--)
{
arr1[i] = arr2[i]; // Shift elements of arr2[i] to arr1[i]
arr2[i] = arr1[i - 1]; // Shift previous elements of arr1 to current index of arr2
}
arr1[0] = arr2[0]; // Shift the first element of arr2 to arr1
arr2[0] = t; // Shift the original last element of arr1 to arr2
}
public static void main(String[] args) {
int arr1[] = {61, 45, 19, 33, 59, 7, 42, 24, 98, 77};
int arr2[] = {86, 52, 10, 36, 22, 5, 98, 91, 13, 6};
int N = arr1.length;
zshift(arr1, arr2, N);
// Print the elements of arr1
for (int i = 0; i < N; i++)
System.out.print(arr1[i] + " ");
System.out.println();
// Print the elements of arr2
for (int i = 0; i < N; i++)
System.out.print(arr2[i] + " ");
System.out.println();
}
}
//This code is contributed by Abhishek Kumar
```

```
def zshift(arr1, arr2, N):
t = arr1[N - 1] # Store the last element of arr1 in t
for i in range(N - 1, 0, -1):
arr1[i] = arr2[i] #Shift elements of arr2[i] to arr1[i]
arr2[i] = arr1[i - 1] #Shift previous elements of arr1 to current index of arr2
arr1[0] = arr2[0] #Shift the first element of arr2 to arr1
arr2[0] = t # Shift the original last element of arr1 to arr2
arr1 = [61, 45, 19, 33, 59, 7, 42, 24, 98, 77]
arr2 = [86, 52, 10, 36, 22, 5, 98, 91, 13, 6]
N = len(arr1)
zshift(arr1, arr2,N)
#Print the elements of arr1
for i in range(0, N, 1):
print(arr1[i], end =" ")
print()
#Print the elements of arr2
for i in range(0, N, 1):
print(arr2[i], end =" ")
#This code is contributed by Abhishek Kumar
```