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];
    //process.stdout.write('length:'+len);
    var toFind = firstLine[1];
    //process.stdout.write('toFind:'+toFind);
    //process.stdout.write('\n');
    var arr = data[1].split(' '); 
    //process.stdout.write(arr);
    for(var i=len-1;i>=0;i--) {
        if(arr[i] == toFind){
            process.stdout.write(i+1);
            return;
        }
    }
    process.stdout.write(-1);
}

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