Find the integer present after a number the sequence of numbers | JavaScript


Description

You are given N integers, your task is to write a program that finds the integer present after 2 in the sequence of numbers given. In case 2 is not present or  2 is the last element, print -1.

Note: You have to ensure that you use functions to implement the solution. Any other solution will be discarded.

Input

Input Format

First line of the input contains N

Second line of the input contains N space separated integers.

Constraints

N<10000

Output

Output Format

Output number present after 2.

In case there's no number present after 2, print -1

Sample Input 1 

5
3 4 2 0 1

Sample Output 1

0
Answer:

var input = input.split("\n");
  var N = input[0];
  var num = input[1].trim().split(" ").map(Number);

  // console.log(N);

  // console.log(num);
  // var output = []

  for (i = 0i < N-1i++)
    if (2 == num[i]){
  console.log(num[i+1])
  break }
  if (num[i] !== 2 || N == 1 || num[N-1] == 2)
  console.log(-1)

Comments

Popular posts from this blog

Multer file upload and delete - Nodejs, Mongodb, Express

Money Management

Sub array quest IX