Recent Posts
Recent Comments
Link
Today
Total
01-09 19:28
관리 메뉴

채린씨의 티스토리

[HackerEarth] Easy - Empty arrays(JavaScript) 본문

코딩테스트 대비

[HackerEarth] Easy - Empty arrays(JavaScript)

채린씨 2022. 4. 6. 21:23

Problem

You are given two arrays each of size n, a and b consisting of the first n positive integers each exactly once, that is, they are permutations. 

Your task is to find the minimum time required to make both the arrays empty. The following two types of operations can be performed any number of times each taking 1 second:

  • In the first operation, you are allowed to rotate the first array clockwise.
  • In the second operation, when the first element of both the arrays is the same, they are removed from both the arrays and the process continues.

 

Input format

  • The first line contains an integer n, denoting the size of the array.
  • The second line contains the elements of array a.
  • The third line contains the elements of array b.

 

Output format

Print the total time taken required to empty both the array.

 

Constraints

1≤n≤100

 

Sample Input/Output

Input Output
3
1 3 2
2 3 1
6
 
Time Limit: 1
Memory Limit: 256
Source Limit:
 

Explanation

Perform operation 1 to make a = 3, 2, 1

Perform operation 1 to make a = 2, 1, 3

Now perform operation 2 to make a = 1, 3 and b = 3, 1

Perform operation 1 to make a = 3, 1

Now perform operation 2 to make a = 1 and b =  1

Now perform operation 2 to make a = {} and b =  {}

 


My Code

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
  stdin_input += input; // Reading input from STDIN
});

process.stdin.on("end", function () {
  main(stdin_input);
});

function main(input) {
  let data = input.split("\n");
  let n = parseInt(data[0]);
  let a = data[1].split(" ");
  let b = data[2].split(" ");
  let result = 0;
  while (a.length) {
    if (a[0] == b[0]) {
      a.shift();
      b.shift();
      result++;
    } else {
      a.push(a.shift());
      result++;
    }
  }
  console.log(result);
}

 

 

 

Comments