Related Articles

# Number of moves required between the arrays to complete the traversal in sorted order

• Difficulty Level : Medium
• Last Updated : 05 Jul, 2021

Given two sorted arrays, X[] of size N and Y[] of size M having unique values. The task is to count the total number of moves required between the arrays to traverse all the elements in both the arrays in ascending order if initially, the traversal starts from the X[] array.

Examples:

Input: X[] = {1}, Y[] = {2, 3, 4}
Output:
Explanation: Only 1 move is required after traversing the X array and then move to the 0 index of the Y array and traverse its rest of the values.

Input: X[] = {1, 3, 4}, Y[] = {2, 5, 6}
Output: 3

Approach: The given problem can be solved using the two-pointer technique. Follow the below steps to solve the problem:

• Initialize two pointers, say i as 0 and j as 0 pointing to the X[] and Y[] array respectively.
• Initialize another variable total_moves as 0 to store the number of moves required.
• Since traversal always starts from the X[] array, so first compare the values at the current index. There arise two cases:
• If currently present at X[] array:
• If X[i] < Y[j], just increment index i.
• If X[i] > Y[j], increment total_moves and index j.
• If currently present at Y[] array:
• If Y[j] < X[i], increment index j.
• If Y[j] > X[i], increment total_moves and index i.
• Repeat the above steps until any one of the array traversals is finished.
• Once the above loop finishes, total_moves would be incremented in the following two conditions:
• If traversal finishes at X array and j < M, then increment total_moves by 1.
• If traversal finishes at Y array and i < N, then increment total_moves by 1.
• Print the value of total_moves as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the number of moves``// required between the arrays to complete``// the traversal in sorted order``int` `numberofMoves(``int` `X[], ``int` `Y[], ``int` `n, ``int` `m)``{` `    ``// Variable to check if present``    ``// at X array or not``    ``bool` `present_at_X = ``true``;` `    ``// Store total number of moves``    ``int` `total_moves = 0;` `    ``// Initialize i and j pointing to X and``    ``// Y array respectively``    ``int` `i = 0, j = 0;` `    ``// Loop until one array is``    ``// completely traversed``    ``while` `(i < n && j < m) {` `        ``// If currently present at X array,``        ``// and X[i]

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to find the number of moves``// required between the arrays to complete``// the traversal in sorted order``static` `int` `numberofMoves(``int``[] X, ``int` `Y[], ``int` `n, ``int` `m)``{` `    ``// Variable to check if present``    ``// at X array or not``    ``boolean` `present_at_X = ``true``;` `    ``// Store total number of moves``    ``int` `total_moves = ``0``;` `    ``// Initialize i and j pointing to X and``    ``// Y array respectively``    ``int` `i = ``0``, j = ``0``;` `    ``// Loop until one array is``    ``// completely traversed``    ``while` `(i < n && j < m) {` `        ``// If currently present at X array,``        ``// and X[i]

## Python3

 `# Python3 program for the above approach` `# Function to find the number of moves``# required between the arrays to complete``# the traversal in sorted order``def` `numberofMoves(X, Y, n, m):` `    ``# Variable to check if present``    ``# at X array or not``    ``present_at_X ``=` `True` `    ``# Store total number of moves``    ``total_moves ``=` `0` `    ``# Initialize i and j pointing to X and``    ``# Y array respectively``    ``i, j ``=` `0``, ``0` `    ``# Loop until one array is``    ``# completely traversed``    ``while` `(i < n ``and` `j < m):` `        ``# If currently present at X array,``        ``# and X[i]

## C#

 `// C# program for the above approach` `using` `System;` `public` `class` `GFG{``    ` `    ``// Function to find the number of moves``// required between the arrays to complete``// the traversal in sorted order``static` `int` `numberofMoves(``int``[] X, ``int``[] Y, ``int` `n, ``int` `m)``{`` ` `    ``// Variable to check if present``    ``// at X array or not``    ``bool` `present_at_X = ``true``;`` ` `    ``// Store total number of moves``    ``int` `total_moves = 0;`` ` `    ``// Initialize i and j pointing to X and``    ``// Y array respectively``    ``int` `i = 0, j = 0;`` ` `    ``// Loop until one array is``    ``// completely traversed``    ``while` `(i < n && j < m) {`` ` `        ``// If currently present at X array,``        ``// and X[i]

## Javascript

 ``
Output

`3`

Time Complexity: O(N+M)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up