Techno Blender
Digitally Yours.

Shift the elements between two arrays in Z form

0 50


Given two arrays arr1[] and arr2[] both of size N, the task is to shift each element of the cell in Z 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].

Examples:

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

Z shift of array elements

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

Approach: Let’s understand how the function zshift works step by step:

  • The variable t is assigned the last element of arr1, which will be used for swapping later.
  • The loop starts from the last index (N-1) and iterates down to the second index (1).
  • Inside the loop, the current element of arr2 is assigned to the corresponding index in arr1, and the previous element of arr1 is assigned the current element of arr2. This process effectively shifts the elements in a Z-form between the arrays.
  • After the loop, the first element of arr2 is assigned to the first element of arr1, and the original last element of arr1 (stored in t) is assigned to the first element of arr2. This finalizes the shifting process.

Below is the implementation of the above approach:

C++

// 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

//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

Python3

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


Given two arrays arr1[] and arr2[] both of size N, the task is to shift each element of the cell in Z 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].

Examples:

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

Z-shift-GFG
Z shift of array elements

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

Approach: Let’s understand how the function zshift works step by step:

  • The variable t is assigned the last element of arr1, which will be used for swapping later.
  • The loop starts from the last index (N-1) and iterates down to the second index (1).
  • Inside the loop, the current element of arr2 is assigned to the corresponding index in arr1, and the previous element of arr1 is assigned the current element of arr2. This process effectively shifts the elements in a Z-form between the arrays.
  • After the loop, the first element of arr2 is assigned to the first element of arr1, and the original last element of arr1 (stored in t) is assigned to the first element of arr2. This finalizes the shifting process.

Below is the implementation of the above approach:

C++

// 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

//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

Python3

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

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment