Number of moves required between the arrays to complete the traversal in sorted order
Last Updated :
03 Nov, 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: 1
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++
#include <bits/stdc++.h>
using namespace std;
int numberofMoves( int X[], int Y[], int n, int m)
{
bool present_at_X = true ;
int total_moves = 0;
int i = 0, j = 0;
while (i < n && j < m) {
if (present_at_X == true && X[i] < Y[j]) {
i++;
}
else if (present_at_X == true && Y[j] < X[i]) {
total_moves++;
present_at_X = false ;
j++;
}
else if (present_at_X == false && Y[j] < X[i]) {
j++;
}
else if (present_at_X == false && X[i] < Y[j]) {
total_moves++;
present_at_X = true ;
i++;
}
}
if (present_at_X == true && j < m) {
total_moves++;
present_at_X = false ;
}
if (present_at_X == false && i < n) {
total_moves++;
present_at_X = true ;
}
return total_moves;
}
int main()
{
int X[] = { 1, 3, 4 };
int n = sizeof (X) / sizeof (X[0]);
int Y[] = { 2, 5, 6 };
int m = sizeof (Y) / sizeof (Y[0]);
cout << numberofMoves(X, Y, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int numberofMoves( int [] X, int Y[], int n, int m)
{
boolean present_at_X = true ;
int total_moves = 0 ;
int i = 0 , j = 0 ;
while (i < n && j < m) {
if (present_at_X == true && X[i] < Y[j]) {
i++;
}
else if (present_at_X == true && Y[j] < X[i]) {
total_moves++;
present_at_X = false ;
j++;
}
else if (present_at_X == false && Y[j] < X[i]) {
j++;
}
else if (present_at_X == false && X[i] < Y[j]) {
total_moves++;
present_at_X = true ;
i++;
}
}
if (present_at_X == true && j < m) {
total_moves++;
present_at_X = false ;
}
if (present_at_X == false && i < n) {
total_moves++;
present_at_X = true ;
}
return total_moves;
}
public static void main(String[] args)
{
int X[] = { 1 , 3 , 4 };
int n = X.length;
int Y[] = { 2 , 5 , 6 };
int m = Y.length;
System.out.print(numberofMoves(X, Y, n, m));
}
}
|
Python3
def numberofMoves(X, Y, n, m):
present_at_X = True
total_moves = 0
i, j = 0 , 0
while (i < n and j < m):
if (present_at_X = = True and X[i] < Y[j]):
i + = 1
elif (present_at_X = = True and Y[j] < X[i]):
total_moves + = 1
present_at_X = False
j + = 1
elif (present_at_X = = False and Y[j] < X[i]):
j + = 1
elif (present_at_X = = False and X[i] < Y[j]):
total_moves + = 1
present_at_X = True
i + = 1
if (present_at_X = = True and j < m):
total_moves + = 1
present_at_X = False
if (present_at_X = = False and i < n):
total_moves + = 1
present_at_X = True
return total_moves
if __name__ = = '__main__' :
X = [ 1 , 3 , 4 ]
n = len (X)
Y = [ 2 , 5 , 6 ]
m = len (Y)
print (numberofMoves(X, Y, n, m))
|
C#
using System;
public class GFG{
static int numberofMoves( int [] X, int [] Y, int n, int m)
{
bool present_at_X = true ;
int total_moves = 0;
int i = 0, j = 0;
while (i < n && j < m) {
if (present_at_X == true && X[i] < Y[j]) {
i++;
}
else if (present_at_X == true && Y[j] < X[i]) {
total_moves++;
present_at_X = false ;
j++;
}
else if (present_at_X == false && Y[j] < X[i]) {
j++;
}
else if (present_at_X == false && X[i] < Y[j]) {
total_moves++;
present_at_X = true ;
i++;
}
}
if (present_at_X == true && j < m) {
total_moves++;
present_at_X = false ;
}
if (present_at_X == false && i < n) {
total_moves++;
present_at_X = true ;
}
return total_moves;
}
static public void Main ()
{
int [] X = { 1, 3, 4 };
int n = X.Length;
int [] Y = { 2, 5, 6 };
int m = Y.Length;
Console.WriteLine(numberofMoves(X, Y, n, m));
}
}
|
Javascript
<script>
function numberofMoves(X,Y,n,m)
{
let present_at_X = true ;
let total_moves = 0;
let i = 0, j = 0;
while (i < n && j < m) {
if (present_at_X == true && X[i] < Y[j]) {
i++;
}
else if (present_at_X == true && Y[j] < X[i]) {
total_moves++;
present_at_X = false ;
j++;
}
else if (present_at_X == false && Y[j] < X[i]) {
j++;
}
else if (present_at_X == false && X[i] < Y[j]) {
total_moves++;
present_at_X = true ;
i++;
}
}
if (present_at_X == true && j < m) {
total_moves++;
present_at_X = false ;
}
if (present_at_X == false && i < n) {
total_moves++;
present_at_X = true ;
}
return total_moves;
}
var X = [1, 3, 4 ];
var n =X.length;
var Y = [2, 5, 6 ];
var m = Y.length;
document.write(numberofMoves(X, Y, n, m));
</script>
|
Time Complexity: O(N+M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...