Last active
January 15, 2018 15:34
-
-
Save gauravbansal74/b7f260fb000e828426a511c0bdf12a7b to your computer and use it in GitHub Desktop.
Linear Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = `2 | |
5 16 | |
9 7 2 16 4 | |
7 98 | |
1 22 57 47 34 18 66`; | |
function searchNumber(digit, input){ | |
var output = -1; | |
for(var i=0; i <input.length; i++){ | |
if(digit == input[i]){ | |
output = i+1; | |
break; | |
} | |
} | |
return output; | |
} | |
function LinearSearch(input){ | |
let inputSplitedByLines = input.split("\n"); | |
let totalNoOfCases = inputSplitedByLines[0]; | |
if (inputSplitedByLines.length == (totalNoOfCases*2)+1){ | |
for(var i=1; i <inputSplitedByLines.length; i=i+2){ | |
console.log(searchNumber(inputSplitedByLines[i].split(" ")[1],inputSplitedByLines[i+1].split(" "))); | |
} | |
}else{ | |
console.log("Problem statement doesn't match with input"); | |
} | |
} | |
LinearSearch(input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment