HackerEarth JavaScript solutions 1

HackerEarth JavaScript solutions

function main(input) {
    //Enter your code here
    var data = input.split('\n');
    var firstLine = data[0].split(' ');
    var len = firstLine[0];
    var toFind = firstLine[1];
    var arr = data[1].split(' '); 
    for(var i=len-1;i>=0;i--) {
        if(arr[i] == toFind){

Here is what the above code is Doing:
1. The first line of the input is the length of the array and the number to find.
2. The second line of the input is the array.
3. We split the first line of the input into two variables, len and toFind.
4. We split the second line of the input into an array, arr.
5. We loop through the array from the end to the beginning.
6. If we find the number we’re looking for, we print its index and return.
7. If we don’t find the number, we print -1.

Similar Posts