Header Ad

Hackerrank Intro to Tutorials Challenges problem solution

In this Hackerrank Intro to Tutorial Challenges problem we have given a sorted array and a number, we need to print the index location of the integer in the array.

Hackerrank Intro to Tutorials Challenges problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'introTutorial' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER V
#  2. INTEGER_ARRAY arr
#

def introTutorial(V, arr):
    return arr.index(V)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    V = int(input().strip())

    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    result = introTutorial(V, arr)

    fptr.write(str(result) + '\n')

    fptr.close()


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        int value = sc.nextInt();
        int cas = sc.nextInt();
        ArrayList<Integer> ar = new ArrayList<Integer>();
        for(int i = 0;i<cas; i++)
        {
            ar.add(sc.nextInt());
        }
        
        for(int n = 0; n < ar.size();n++)
        {
            if(ar.get(n) == value)
                System.out.println(n);
        }
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int V, n, temp;
    cin >> V;
    cin >> n;
    
    int array[n];
    
    for(int i = 0; i < n; i++) {
        cin >> temp;
        array[i] = temp;  
    }
    for(int i = 0; i < n; i++) {
        if(array[i] == V) {
            cout << i << endl;;
        }
        
    }
  
    
    
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
int main()
{
	int V,N,a;
	scanf("%d%d",&V,&N);
	int i;
	for(i = 0; i < N; i ++)
	{
		scanf("%d",&a);
		if(a == V)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var inputArray = input.split('\n');
    var V = parseInt(inputArray[0], 10);
    var valueArray = inputArray[2].split(" ");
    for(var i =0; i<valueArray.length; i++){
        if(parseInt(valueArray[i],10) === V){
            console.log(i);
            break;
        }
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments