Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Intro to Tutorial Challenges Solution

Yashwant Parihar, April 22, 2023April 28, 2023

In this post, we will solve HackerRank Intro to Tutorial Challenges Problem Solution. About Tutorial Challenges
Many of the challenges on HackerRank are difficult and assume that you already know the relevant algorithms. These tutorial challenges are different. They break down algorithmic concepts into smaller challenges so that you can learn the algorithm by solving them. They are intended for those who already know some programming, however. You could be a student majoring in computer science, a self-taught programmer, or an experienced developer who wants an active algorithms review. Here’s a great place to learn by doing!

About the Challenges
Each challenge will describe a scenario and you will code a solution. As you progress through the challenges, you will learn some important concepts in algorithms. In each challenge, you will receive input on STDIN and you will need to print the correct output to STDOUT.

There may be time limits that will force you to make your code efficient. If you receive a “Terminated due to time out” message when you submit your solution, you’ll need to reconsider your method. If you want to test your code locally, each test case can be downloaded, inputs and expected results, using hackos. You earn hackos as you solve challenges, and you can spend them on these tests.

For many challenges, helper methods (like an array) will be provided for you to process the input into a useful format. You can use these methods to get started with your program, or you can write your own input methods if you want. Your code just needs to print the right output to each test case.

Sample Challenge
This is a simple challenge to get things started. Given a sorted array (arr) and a number (V),
can you print the index location of V in the array?
Example
arr = [1, 2, 3]
V = 3
Return 2 for a zero-based index array.
If you are going to use the provided code for I/O, this next section is for you.

Function Description

Complete the introTutorial function in the editor below. It must return an integer representing the zero-based index of .

introTutorial has the following parameter(s):

  • int arr[n]: a sorted array of integers
  • int V: an integer to search for

Returns

  • int: the index of V in Arr

The next section describes the input format. You can often skip it, if you are using included methods or code stubs.

Input Format
The first line contains an integer, V, a value to search for.
The next line contains an integer, n, the size of arr. The last line contains n space-separated integers, each a value of arr[i] where 0 ≤ i ≤n.
The next section describes the constraints and ranges of the input. You should check this
section to know the range of the input

Sample Input 0

STDIN           Function
-----           --------
4               V = 4
6               arr[] size n = 6 (not passed, see function description parameters)
1 4 5 7 9 12    arr = [1, 4, 5, 7, 9, 12]

Sample Output 0

1

Explanation 0

V = 4. The value 4 is the 2nd element in the array. Its index is 1 since the array indices start from 0 (see array definition under Input Format).

HackerRank Intro to Tutorial Challenges Problem Solution
HackerRank Intro to Tutorial Challenges Problem Solution

Intro to Tutorial Challenges C Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int szukany;
    int rozmiar;
    int i=0;
    int tymczas;
    scanf("%d",&szukany);
    scanf("%d",&rozmiar);
    while (i<rozmiar)
        {scanf("%d",&tymczas);
        
        if (tymczas==szukany)
            {
            tymczas=i;
            i=rozmiar;
            }
        else
            {i+=1;}
        }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    printf("%d",tymczas);
    return 0;
}

Intro to Tutorial Challenges C++ Solution

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


int main() {
    int V, n;
    cin >> V >> n;
    int tmp;
    for(int i=0; i<n; i++) {
        cin >> tmp;
        if(tmp==V) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

Intro to Tutorial Challenges C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) 
    {
        String Value = Console.ReadLine();
        int Array_Size = Convert.ToInt32(Console.ReadLine());
        String Array_S = Console.ReadLine();
        String[] Array_split = Array_S.Split(' ');
        int i;
        for (i=0;i<Array_Size;i++)
        {
            if (Value == Array_split[i])
                Console.WriteLine(i);
        }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
    }
}

Intro to Tutorial Challenges Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * 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
     */

    public static int introTutorial(int V, int[] arr) {
        if(arr[0] == V) return 0;
        if(arr[arr.length - 1] == V) return arr.length - 1;
        
        int i = arr.length / 2;
        while(arr[i] != V) {
            if(V > arr[i]) {
                i = (arr.length + i) / 2;
            }
            else {
                i /= 2;
            }
        }
        return i;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);

        int V = scan.nextInt();
        int n = scan.nextInt();

        int[] arr = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
            if(arr[i] == V) {
                System.out.println(i);
                break;
            }
        }
        
        scan.close();
    }
}

Intro to Tutorial Challenges JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var _input = "";

process.stdin.on('data', function (data) {
    _input += data;
});

process.stdin.on('end', function () {
    _input = _input.split('\n');
    var V = parseInt(_input[0], 10);
    var position = -1;
    _input[2].split(' ').map(function(current, pos) {
        var value = parseInt(current, 10);
        if (value === V) {
            position = pos;
        }
    });
    
    process.stdout.write(position);
    
});

Intro to Tutorial Challenges Python Solution

import sys

value = int(sys.stdin.readline())
numberOfElements = int(sys.stdin.readline())

elements = []
raw = sys.stdin.readline().split()
for index in range(numberOfElements):
    elements.append(int(raw[index]))
    
print(elements.index(value))

Other Solutions

  • HackerRank CamelCase Problem Solution
  • HackerRank Insertion Sort – Part 1 Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes