Monday, January 15, 2018

JavaScript - Linear Search

Problem Statement
Given an array of N elements and an integer K, return the position of the first occurrence of K in given array.  The position of the first element is considered as 1.

Output -1 if the number is not found in an array.
InputThe first line contains 'T' denoting the number of test cases. Then follows the description of test cases. Each case begins with a two space separated integer N and K denoting the size of the array and the value of K respectively. The next line contains the N-space separated integers denoting the elements of the array.

OutputFor each test case, print the index of the first occurrence of given number K. 
Print -1 if the number is not found in the array. 

Constraints:
1<=T<=100
1<=N<=1000
1<=K<=100000
1<=A[i]<=100000

Example:
Input :
2
5 16
9 7 2 16 4
98
1 22 57 47 34 18 66
Output
4
-1