Shift the elements between two arrays in Z form
Last Updated :
11 Sep, 2023
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:
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++
#include <bits/stdc++.h>
using namespace std;
void zshift( int * arr1, int * arr2, int N)
{
int t = arr1[N - 1];
for ( int i = N - 1; i >= 1; i--) {
arr1[i] = arr2[i];
arr2[i] = arr1[i - 1];
}
arr1[0] = arr2[0];
arr2[0] = t;
}
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);
for ( int i = 0; i < N; i++)
cout << arr1[i] << " " ;
cout << endl;
for ( int i = 0; i < N; i++)
cout << arr2[i] << " " ;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static void zshift( int [] arr1, int [] arr2, int N)
{
int t = arr1[N - 1 ];
for ( int i = N - 1 ; i >= 1 ; i--) {
arr1[i] = arr2[i];
arr2[i] = arr1[i - 1 ];
}
arr1[ 0 ] = arr2[ 0 ];
arr2[ 0 ] = t;
}
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);
for ( int i = 0 ; i < N; i++)
System.out.print(arr1[i] + " " );
System.out.println();
for ( int i = 0 ; i < N; i++)
System.out.print(arr2[i] + " " );
System.out.println();
}
}
|
Python3
def zshift(arr1, arr2, N):
t = arr1[N - 1 ]
for i in range (N - 1 , 0 , - 1 ):
arr1[i] = arr2[i]
arr2[i] = arr1[i - 1 ]
arr1[ 0 ] = arr2[ 0 ]
arr2[ 0 ] = t
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)
for i in range ( 0 , N, 1 ):
print (arr1[i], end = " " )
print ()
for i in range ( 0 , N, 1 ):
print (arr2[i], end = " " )
|
C#
using System;
public class Program {
static void ZShift( int [] arr1, int [] arr2, int N)
{
int t = arr1[N - 1];
for ( int i = N - 1; i >= 1; i--) {
arr1[i] = arr2[i];
arr2[i] = arr1[i - 1];
}
arr1[0] = arr2[0];
arr2[0] = t;
}
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);
foreach ( int num in arr1)
{
Console.Write(num + " " );
}
Console.WriteLine();
foreach ( int num in arr2)
{
Console.Write(num + " " );
}
}
}
|
Javascript
function zShift(arr1, arr2, N) {
const t = arr1[N - 1];
for (let i = N - 1; i >= 1; i--) {
arr1[i] = arr2[i];
arr2[i] = arr1[i - 1];
}
arr1[0] = arr2[0];
arr2[0] = t;
}
const arr1 = [61, 45, 19, 33, 59, 7, 42, 24, 98, 77];
const arr2 = [86, 52, 10, 36, 22, 5, 98, 91, 13, 6];
const N = arr1.length;
zShift(arr1, arr2, N);
console.log(arr1.join( " " ));
console.log(arr2.join( " " ));
|
Output
86 52 10 36 22 5 98 91 13 6
77 61 45 19 33 59 7 42 24 98
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...