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!

The first series of challenges covers sorting. They are listed below:

Tutorial Challenges - Sorting  

Insertion Sort challenges

Insertion Sort 1 - Inserting
Insertion Sort 2 - Sorting
Correctness and loop invariant
Running Time of Algorithms

Quicksort challenges

Quicksort 1 - Partition
Quicksort 2 - Sorting
Quicksort In-place (advanced)
Running time of Quicksort

Counting sort challenges

Counting Sort 1 - Counting
Counting Sort 2 - Simple sort
Counting Sort 3 - Preparing
Full Counting Sort (advanced)

There will also be some challenges where you'll get to apply what you've learned using the completed algorithms.

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 $V$.  

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\leq 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.

Constraints  

$1\leq n\leq1000$   
$-1000\leq V\leq1000,V\in arr$ 
$V$ will occur in $arr$ exactly once.

This "sample" shows the first input test case. It is often useful to go through the sample to understand a challenge.

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 $2^{nd}$ element in the array.  Its index is $1$ since the array indices start from $0$ (see array definition under Input Format).

Source: hackerrank | Difficulty: EASY
Tags: Sorting


Solutions:
V = input()
N = int(input())
A = input()
A = A.split()
print(A.index(V))


val = int(input())
size = int(input())
arr = [int(i) for i in input().split()]
x = 0
for j in arr:
	if val == j:
		break
	x = x + 1
print(x)


import sys
s = sys.stdin.read().split()
s = [int(x) for x in s]
v = s.pop(0)
n = s.pop(0)
i = 0
while s[i] != v:
	i += 1
print(i)


V = input()
l = input()
V = int(V)
l = int(l)
numbers = input()
numbers = numbers.split(' ')
numbers = [int(i) for i in numbers]
for i in range(l):
	if numbers[i] == V:
		print(i)


V = int(input())
n = int(input())
ar = [int(x) for x in input().strip().split(' ')]
print(ar.index(V))


V = input()
n = input()
a = input()
ar = a.split(' ')

def insertionSort(V, n, ar):
	for i in range(n):
		if V == int(ar[i]):
			return i
print(insertionSort(int(V), int(n), ar))


import sys
import bisect
v = int(sys.stdin.readline())
length = int(sys.stdin.readline())
array = list(map(int, sys.stdin.readline().split()))
index = bisect.bisect_left(array, v)
print(index)


v = int(input())
n = int(input())
ar = [int(i) for i in input().split()]
print(ar.index(v))


val = input()
n = int(input())
a = input().split(' ')
print(a.index(val))


V = int(input())
l = int(input())
ar = [int(x, 10) for x in input().split()]
for i in range(l):
	if ar[i] == V:
		print(i)
		break


v = input()
n = int(input())
ar = input().split()
print(ar.index(v))


V = int(input())
n = int(input())
ar = input().split(' ')
for i in range(n):
	if int(ar[i]) == V:
		print(i)
		break


import sys
V = int(input())
n = int(input())
i = 0
for cur in input().split():
	if V == int(cur):
		print(i)
	i += 1


V = int(input())
n = int(input())
ar = [int(word) for word in input().split(' ')]
count = 0
for num in ar:
	if num == V:
		print(count)
	count += 1


V = int(input())
n = int(input())
ar = input()
ar = ar.split()
for x in range(0, n):
	if V == int(ar[x]):
		print(x)
		break


V = int(input())
n = int(input())
ar = input().split()
for i in range(n):
	if str(V) == ar[i]:
		print(i)
		break


def search(array, v, low, high):
	if low > high:
		return -1
	else:
		mid = int((low + high) / 2)
		t = array[mid]
		if v == t:
			return mid
		elif v > t:
			return search(array, v, mid + 1, high)
		else:
			return search(array, v, low, mid - 1)
v = int(input())
n = int(input())
num_line = input()
nums = [int(s) for s in num_line.split()]
print(search(nums, v, 0, len(nums) - 1))


elem = input()
size = input()
l = input().split()
print(l.index(elem))


import sys
V = int(sys.stdin.readline())
n = int(sys.stdin.readline())
ar = [int(x) for x in sys.stdin.readline().split()]
print(ar.index(V))


to_find = input()
ignore = input()
values = input().split(' ')
print(values.index(to_find))


user_find = input()
array_length = int(input())
my_array = input()
my_array = my_array.split(' ')
i = 0
ans = 0
while i < array_length:
	temp = my_array[i]
	if my_array[i] == user_find:
		ans = i
	i += 1
print(ans)


import sys
lines = sys.stdin.readlines()
key = int(lines[0])
nums = lines[2].split(' ')
nums = [int(n) for n in nums if n]
for (i, n) in enumerate(nums):
	if n == key:
		print(i)
		break


v = input()
n = input()
ar = input()
j = ar.split(' ')
print(j.index(v))


v = input()
input()
ar = input().split()
print(ar.index(v))


v = int(input())
n = int(input())
tmp = input()
ar = []
for i in tmp.split():
	ar.append(int(i))
print(ar.index(v))


m = int(input())
n = int(input())
arr = [int(x) for x in input().split()]
for i in range(n):
	if arr[i] == m:
		print(i)
		break


V = int(input())
N = int(input())
ar = input().split(' ')
for i in range(N):
	if int(ar[i]) == V:
		print(i)
		break


i = input()
input()
print(input().split().index(i))


v = int(input())
n = int(input())
num = list(map(int, input().split()))
print(num.index(v))


V = input()
input()
ar = []
for x in input().split():
	ar.append(x)
print(ar.index(V))


def removeSpaces(val):
	newVal = []
	for i in val:
		if i != ' ':
			newVal += [int(i)]
	return newVal

def problem1():
	V = int(input())
	n = int(input())
	values = input().split(' ')
	for x in range(0, len(values)):
		if int(values[x]) == V:
			print(x)
problem1()


x = input()
y = input()
y = input()
y = y.split()
counter = 0
for i in y:
	if x == i:
		print(counter)
		break
	counter += 1


V = input()
n = int(input())
ar = input().split(' ')
for i in range(0, n):
	if ar[i] == V:
		print(i)


import sys
args = sys.stdin.readlines()
x = int(args[0])
array = [int(x) for x in args[2].split()]
for (i, v) in enumerate(array):
	if v == x:
		print(i)


def binary_search(arr, low, high, value):
	if low > high:
		return -1
	else:
		middle = int((high + low) / 2)
		if arr[middle] < value:
			return binary_search(arr, middle + 1, high, value)
		elif arr[middle] > value:
			return binary_search(arr, low, middle, value)
		else:
			return middle

def main():
	value = int(input())
	size = input()
	arr_str = input()
	arr = list(map(int, arr_str.split()))
	ret_val = binary_search(arr, 0, len(arr), value)
	print(ret_val)
main()


V = input()
n = int(input())
ar = input()
ar = ar.split()
print(ar.index(V))


V = int(input())
n = int(input())
ar = input().split(sep=' ')
print(ar.index(str(V)))


import sys
alist = []
for line in sys.stdin:
	alist.append(line.rstrip('\n'))
i = 0
for a in alist:
	i += 1
	if i % 3 == 1:
		value = a
	if i % 3 == 0:
		b = a.split()
		j = 0
		for c in b:
			if int(c) == int(value):
				print(j)
			j += 1


V = int(input())
n = int(input())
ar = [int(i) for i in input().strip().split(' ')]
print(ar.index(V))


def findV(v, n, ar):
	for i in range(n):
		if ar[i] == v:
			print(i)
			return

def main():
	v = int(input(''))
	n = int(input(''))
	ar = list(map(int, input('').split(' ')))
	findV(v, n, ar)
main()


import sys

def main():
	V = int(sys.stdin.readline())
	n = int(sys.stdin.readline())
	ar = [int(x) for x in sys.stdin.readline().split()]
	for i in range(n):
		if ar[i] == V:
			print(i)
			break
main()


V = int(input())
n = int(input())
(a, *ar) = map(int, ['1'] + input().split())
del a
for i in range(n):
	if ar[i] == V:
		break
print(i)


ArraySpot = int(input())
ArrayAmount = int(input())
Array = [x for x in input().split()]
print(Array.index(str(ArraySpot)))


V = int(input())
n = int(input())
arr = [int(x) for x in input().split(' ')]
for i in range(n):
	if arr[i] == V:
		print(i)
		exit()


V = int(input())
size = int(input())
array = [int(x) for x in input().strip().split()]

def binary_search(array, V):
	pivot = array[len(array) // 2]
	left = array[:len(array) // 2]
	right = array[len(array) // 2 + 1:]
	if pivot > V:
		index = binary_search(left, V)
	elif pivot < V:
		index = binary_search(right, V)
		index += len(left) + 1
	else:
		index = len(left)
	return index
index = binary_search(array, V)
print(index)


def intro(k, arr):
	return arr.index(k)
a = input().strip()
b = input().strip()
c = input().strip()
c = list(map(int, c.split(' ')))
print(intro(int(a), c))


def locate_index(a, v):
	return a.index(v)
value = int(input().strip())
array_length = int(input().strip())
array = list(map(int, input().strip().split(' ')))
print(locate_index(array, value))


V = int(input())
input()
vals = [int(i) for i in input().strip().split()]
print(vals.index(V))


a = input()
b = input()
c = input()
l = []
for x in c.split():
	l.append(int(x))
l.sort()
print(l.index(int(a)))


V = input()
n = int(input())
ar = input().split(' ')
print(ar.index(V))


V = int(input())
n = int(input())
ar = input().split()
for i in range(n):
	ar[i] = int(ar[i])
for (i, e) in enumerate(ar):
	if e == V:
		print(i)


x = int(input())
l = int(input())
a = list(map(int, input().split()))
for i in range(0, l):
	if x == a[i]:
		print(i)
		break


input_row1 = input('')
input_row2 = input('')
input_row3 = input('')
target_value = int(input_row1)
array_size = int(input_row2)
array_of_strings = input_row3.split(' ')
for i in range(array_size):
	one_value = int(array_of_strings.pop(0))
	if one_value == target_value:
		print(i)
		break


find = input()
s = input()
print(input().split().index(find))


def tutorial():
	value = int(input())
	num = int(input())
	input_str = str(input()).strip()
	input_list = [int(x) for x in input_str.split(' ')]
	count = 0
	for x in input_list:
		if x == value:
			return count
		else:
			count = count + 1

def main():
	print(tutorial())
main()


import sys
V = int(str(sys.stdin.readline()))
n = int(str(sys.stdin.readline()))
array_string = str(sys.stdin.readline())
lst = [int(x) for x in array_string.split()]
print(lst.index(V))


import fileinput
lines = fileinput.input()
v = lines[0].rstrip()
n = lines[1]
ar = lines[2].rstrip().split(' ')
print(ar.index(v))


import sys
V = int(sys.stdin.readline())
n = int(sys.stdin.readline())
ar = list(map(int, sys.stdin.readline().split()))
print(ar.index(V))


v = int(input())
input()
print(list(map(int, input().split(' '))).index(v))


import fileinput
import sys
tofind = 0
length_list = 0
thelist = []
for line in fileinput.input():
	if not tofind:
		tofind = int(line)
	elif not length_list:
		length_list = int(line)
	else:
		thelist = [int(x) for x in line.split()]
sys.stdout.write(str(thelist.index(tofind)))


def main():
	V = input()
	n = input()
	ar = input().rstrip().split()
	print(ar.index(V))
	return 0
main()


v = int(input())
n = int(input())
vec = list(map(int, input().split()))
for i in range(0, n):
	if vec[i] == v:
		print(i)


V = int(input())
N = int(input())
array = list(map(int, input().split()))
print(array.index(V))


lookup = int(input())
size = int(input())
array = list(map(int, input().strip().split(' ')))
for i in range(0, size):
	if array[i] == lookup:
		print(i)
		break


lookup = input()
length = input()
myarray = input().split(' ')
print(myarray.index(lookup))


v = input('')
n = input('')
ar = input('')
ls = ar.split(' ')
for i in range(0, int(n)):
	if ls[i] == v:
		print(i)


lookupValue = input()
lookupValue = int(lookupValue)
arraySize = input()
arraySize = int(arraySize)
array = input()
array = list(map(int, array.split()))
for i in range(0, arraySize):
	if array[i] == lookupValue:
		print(i)


v = int(input())
input()
ar = [int(i) for i in input().split()]
print(ar.index(v))


x = input('')
y = input('')
z = input('')
z = z.split()
z = [int(each_element) for each_element in z]
for i in range(0, int(y)):
	if int(x) == z[i]:
		print(i)


v = input()
n = input()
s = input().split(' ')
k = 0
while s[k] != v:
	k += 1
print(k)


def bsearch(L, v):
	(start, stop) = (0, len(L) - 1)
	while True:
		if start > stop or (start == stop and L[start] != v):
			return None
		m = int((start + stop) // 2)
		if v < L[m]:
			stop = m - 1
		elif v > L[m]:
			start = m + 1
		else:
			return m
v = int(input())
n = int(input())
L = [int(s) for s in input().split()]
print(bsearch(L, v))


valueLookingFor = int(input())
n = int(input())
arrStr = input().split(' ')
arrVals = []
index = 0
for s in arrStr:
	arrVals.append(int(s))
	if arrVals[index] == valueLookingFor:
		break
	index += 1
print(index)


num_to_find = int(input())
size_of_array = int(input())
my_list = [int(x) for x in input().split()]
print(my_list.index(num_to_find))


V = int(input())
n = int(input())
ar = input().strip().split()
for i in range(0, n):
	if int(ar[i]) == V:
		print(i)
		break


target = int(input())
input()
for i in enumerate([int(x) for x in input().split()]):
	if i[1] == target:
		print(i[0])


v = input()
input()
print(input().split().index(v))


V = int(input())
n = int(input())
ar = [int(v) for v in input().split()]
(l, r) = (0, len(ar) - 1)
while l < r:
	m = (l + r) // 2
	if ar[m] == V:
		(l, r) = (m, m)
	elif ar[m] > V:
		r = m - 1
	else:
		l = m + 1
print(l)


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))


v = int(input())
n = int(input())
ar = list(map(int, input().split()))
(lo, hi) = (0, n)
while lo + 1 < hi:
	i = (lo + hi) // 2
	if v < ar[i]:
		hi = i
	else:
		lo = i
print(lo)


import bisect
v = int(input())
n = int(input())
array = [int(val) for val in input().split()]
print(bisect.bisect_left(array, v))


inputs = [input() for i in range(3)]
print(inputs[2].split().index(inputs[0]))


x = int(input())
m = input()
ar = [int(i) for i in input().strip().split()]
print(ar.index(x))


value = input()
size = int(input())
array = input()
array = array.split()
print(array.index(value))


T = int(input())
N = int(input())
numlist = input().split(' ')
for x in range(0, N):
	a = numlist[x]
	a = int(a)
	if a == T:
		print(x)


V = int(input())
n = int(input())
line = input().split(' ')
ar = [int(x) for x in line]
print(ar.index(V))


import sys
lines = []
for line in sys.stdin:
	lines.append(line)
V = int(lines[0])
n = int(lines[1])
ar = list(map(int, lines[2].split()))
print(ar.index(V))


V = eval(input())
n = input()
arr = input().split()

def binsearch(arr, begin, end, V):
	if end - begin == 0:
		return -1
	mid = (begin + end) // 2
	if int(arr[mid]) == V:
		return mid
	elif int(arr[mid]) > V:
		return binsearch(arr, begin, mid, V)
	else:
		return binsearch(arr, mid, end, V)
print(binsearch(arr, 0, len(arr), V))


def getLine():
	s = str(input())
	if ' ' in s:
		return list(map(int, s.split()))
	else:
		return int(s)
V = getLine()
n = getLine()
ar = getLine()
print(ar.index(V))


import sys
key = sys.stdin.readline().strip()
leng = sys.stdin.readline().strip()
arr = sys.stdin.readline().split()
for i in range(0, int(leng)):
	if int(arr[i]) == int(key):
		print(i)


v = int(input())
n = int(input())
ar = [int(x) for x in input().split()]
print(ar.index(v))


V = int(input().strip())
N = int(input().strip())
ar = list(map(int, input().strip().split()))
print(ar.index(V))


V = int(input())
n = int(input())
ls = [int(x) for x in input().split()]
for (i, x) in enumerate(ls):
	if x == V:
		print(i)
		break
	elif x > V:
		break


V = int(input())
n = int(input())
ar = list(map(int, input().split()))
from bisect import bisect_left
print(bisect_left(ar, V))


V = int(input())
n = int(input())
ar = list(map(int, str(input()).split()))
(low, high) = (0, n - 1)
while low < high:
	mid = low + (V - ar[low]) * (high - low) // (ar[high] - ar[low])
	if V < ar[mid]:
		high = mid - 1
	elif ar[mid] < V:
		low = mid + 1
	else:
		low = high = mid
print(low)


v = input()
size = int(input())
assert 1 < size < 1000
array = input().split(' ')
if v in ''.join(array):
	print(array.index(v))


val = int(input())
s = int(input())
arr = [int(x) for x in input().split()]
print(arr.index(val))


import sys

def index(value, size, array):
	return array.index(value)
v = sys.stdin.readline().strip()
n = sys.stdin.readline().strip()
ar = sys.stdin.readline().strip().split()
print(index(v, n, ar))


v = int(input())
n = int(input())
A = list(map(int, input().split(' ')))
(i, j) = (0, n - 1)
while i <= j:
	m = i + int((j - i) / 2)
	if A[m] == v:
		print(m)
		break
	if A[m] < v:
		i = m + 1
	else:
		j = m - 1


def get_num():
	a = input()
	a = a.split(' ')
	for i in range(len(a)):
		a[i] = eval(a[i])
	return a
V = eval(input())
n = eval(input()) - 1
ar = get_num()

def binary_search(V, n, ar):
	i = 0
	j = n
	while i <= j:
		aux = (j + i) // 2
		if ar[aux] == V:
			break
		elif ar[aux] > V:
			j = aux - 1
		else:
			i = aux + 1
	print(aux)
binary_search(V, n, ar)


x = input()
y = input()
z = input().split()
print(z.index(x))