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 ofarr1
, 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 inarr1
, and the previous element ofarr1
is assigned the current element ofarr2
. 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 ofarr1
, and the original last element ofarr1
(stored int
) is assigned to the first element ofarr2
. This finalizes the shifting process.
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];
for ( int i = N - 1; i >= 1; i--) {
// Shift elements of arr2[i] to arr1[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)
{
// Store the last element of arr1 in t
int t = arr1[N - 1 ];
for ( int i = N - 1 ; i >= 1 ; i--) {
// Shift elements of arr2[i] to arr1[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
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):
# Store the last element of arr1 in t
t = arr1[N - 1 ]
for i in range (N - 1 , 0 , - 1 ):
# Shift elements of arr2[i] to arr1[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 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 |
using System;
public class Program {
// Function to perform Z-form shifting between two
// arrays
static void ZShift( int [] arr1, int [] arr2, int N)
{
// Store the last element of arr1 in t
int t = arr1[N - 1];
for ( int i = N - 1; i >= 1; i--) {
// Shift elements of arr2[i] to arr1[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;
}
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
foreach ( int num in arr1)
{
Console.Write(num + " " );
}
Console.WriteLine();
// Print the elements of arr2
foreach ( int num in arr2)
{
Console.Write(num + " " );
}
}
} |
function zShift(arr1, arr2, N) {
// Store the last element of arr1 in t
const t = arr1[N - 1];
for (let i = N - 1; i >= 1; i--) {
// Shift elements of arr2[i] to arr1[i]
arr1[i] = arr2[i];
// Shift previous elements of arr1 to the 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;
} 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); // Print the elements of arr1 console.log(arr1.join( " " ));
// Print the elements of arr2 console.log(arr2.join( " " ));
|
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)