Programming challenges and algorithm implementations in Python 3.
Practice implementing various algorithms in Python and gain experience designing algorithms.
Find the maximum product of two distinct numbers in a sequence of non-negative integers.
Input: A sequence of non-negative integers (2 β€ n β€ 2*10E5).
Output: The maximum value that can be obtained by multiplying two different elements from the sequence
(0 β€ a1,...,an β€ 2*10E5).
Solution
Given an integer n, find the nth Fibonacci number πΉπ.
Input: Single integer n (0 β€ n β€ 45).
Output: nth Fibonacci number.
Solution
Given an integer n, find the last digit of the nth Fibonacci number πΉπ.
Input: Single integer n (0 β€ n β€ 10E7).
Output: Last digit of Fn (where digit is Fn mod 10).
Solution
Given two integers a and b, find their greatest common divisor.
Input: Two integers a and b seperated by a space (1 β€ a, b β€ 2*10E9).
Output: Greatest common divisor of a and b.
Solution
Given two integers a and b, find their least common multiple.
Input: Two integers a and b seperated by a space (1 β€ a, b β€ 2*10E7).
Output: Least common multiple of a and b.
Solution
Given two integers π and π, output πΉπ mod m.
Input: Two integers π and π separated by a space. (1 β€ π β€ 10E14; 2 β€ π β€ 10E3).
Output: πΉπ mod π.
Solution
Given an integer π, find the last digit of the sum πΉ0 + πΉ1 +Β·Β·Β·+ πΉπ.
Input: Single integer π (0 β€ π β€ 10E14).
Output: The last digit of πΉ0 + πΉ1 +Β·Β·Β·+ πΉπ.
Solution
Given two non-negative integers π and π, where π β€ π, find the last digit of the sum πΉπ + πΉπ+1 +Β·Β·Β·+ πΉπ.
Input: Two non-negative integers π and π separated by a space (0 β€ π β€ π β€ 10E14).
Output: The last digit of πΉπ + πΉπ+1 +Β·Β·Β·+ πΉπ.
Solution
Compute the last digit of πΉ0^2 + πΉ1^2 +Β·Β·Β·+ πΉπ^2.
Input: Integer π (0 β€ π β€ 10E14).
Output: The last digit of πΉ0^2 + πΉ1^2 +Β·Β·Β·+ πΉπ^2.
Solution
Goal is to find the minimum number of coins needed to change the input value into coins with denominations 1, 5, and 10.
Input: Single integer π (1 β€ π β€ 10E3).
Output: The minimum number of coins with denominations 1, 5, 10 that changes π.
Solution
Implement an algorithm for the fractional knapsack problem.
Input: First line contains the number π of items and the capacity W of a knapsack. Following π lines define the
values and the weights of the items. The i-th line contains integers π£π and π€π, the value and weight of the i-th item,
respectively (1 β€ π β€ 10E3, 0 β€ π β€ 2Β·10E6; 0 β€ π£π β€ 2Β·10E6, 0 < π€π β€2Β·10E6 for all 1 β€ π β€ π).
Output: The maximal value of fractions of items that fit into the knapsack. The output has to have at least four
digits after the decimal point.
Solution
What is the minimum number of refills needed to travel to another city located π miles away. The car starts with a full
tank and can travel π miles on a full tank. Along the journey there are gas stations at distances stop1, stop2,..., stopπ.
Input: Firt line contains an integer π. Second line contains an integer π. The third line specifies an integer π.
The last line contains integers stop1, stop2,..., stopπ (1 β€ π β€ 10E5; 1 β€ π β€ 400; 1 β€ π β€ 300; 0 < stop1 < stop2
<Β·Β·Β·< stopπ < π).
Output: The minimum number of refills needed, assuming the car starts with a full tank. If it is not possible to
reach the destination, output -1.
Solution
Given two sequences π1,π2,...,ππ (ππ is the profit per click of the π-th ad) and π1,π2,...,ππ (ππ is the average number of
clicks per day of the π-th slot), we need to partition them into π pairs (ππ,ππ) such that the sum of their products is
maximized.
Input: The first line contains an integer π, the second one contains a sequence of integers π1,π2,...,ππ, the third
one contains a sequence of integers π1,π2,...,ππ (1 β€ π β€ 10E3; β10E5 β€ ππ,ππ β€ 10E5 for all 1 β€ π β€ π).
Output: The maximum value of βοΈ ππππ, where π1,π2,...,ππ is a permutation of π1,π2,...,ππ.
Solution
Given a set of π segments {(π0,π0),(π1,π1),...,(ππβ1,ππβ1)} with integer coordinates on a line, find the minimum number π
of points such that each segment contains at least one point. That is, find a set of integers π of the minimum size such
that for any segment (ππ,ππ) there is a point π₯ β π such that ππ β€ π₯ β€ ππ.
Input: The first line of the input contains the number π of segments. Each of the following π lines contains two
integers ππ and ππ (separated by a space) defining the coordinates of endpoints of the π-th segment (1 β€ π β€ 100; 0 β€ ππ
β€ ππ β€ 10E9 for all 0 β€ π < π).
Output: The minimum number π of points on the first line and the integer coordinates of π points (separated by
spaces) on the second line.
Solution
The goal of this problem is to represent a given positive integer π as a sum of as many pairwise distinct positive
integers as possible. That is, to find the maximum π such that π can be written as π1+π2+Β·Β·Β·+ππ where π1,...,ππ are
positive integers and ππ != ππ for all 1 β€ π < π β€ π.
Input: A single integer n (1 β€ π β€ 10E9).
Output: In the first line, output the maximum number π such that π can be represented as a sum of π pairwise distinct
positive integers. In the second line, output π pairwise distinct positive integers that sum up to π (if they exist).
Solution
The goal is to composes the largest number out of a set of integers.
Input: First line contains an integer n. Second line contains integers separated by a space (1 β€ π β€ 100; 1 β€ ππ β€
10E3 for all 1 β€ π β€ π).
Output: The largest number that can be composed out of a1,a2,...,an.
Solution
The goal is to implement the binary search algorithm.
Input: The first line of the input contains an integer π and a sequence π0 < π1 < . . . < ππβ1 of π pairwise distinct
positive integers in increasing order. The next line contains an integer π and π positive integers π0, π1, . . . , ππβ1
(1 β€ π β€ 10E5; 1 β€ π β€ 3Β·10E4; 1 β€ ππ β€10E9 for all 0 β€ π < π; 1 β€ ππ β€ 10E9 for all 0 β€ π < π).
Output: For all π from 0 to πβ1, output an index 0 β€ π β€ πβ1 such that ππ = ππ or β1 if there is no such index.
Solution
Check whether an input sequence contains a majority element.
Input: The first line contains an integer π, the next one contains a sequence of π non-negative integers π0,π1,...,ππβ1
(1 β€ π β€ 10E5; 0 β€ ππ β€ 10E9 for all 0 β€ π < π).
Output: 1 if the sequence contains an element that appears strictly more than π/2 times, and 0 otherwise.
Solution
Implement the quicksort algorithm to efficiently process a sequences with few unique elements, by implementing a 3-way
partition of the array into three parts: < π₯ part, = π₯ part, and > π₯ part.
Input: The first line of the input contains an integer π. The next line contains a sequence of π integers π0,π1,...,ππβ1
(1 β€ π β€ 10E5; 1 β€ ππ β€ 10E9 for all 0 β€ π < π).
Output: The sorted sequence in non-decreasing order.
Solution
Count the number of inversions of a given sequence.
Input: The first line contains an integer π, the next one contains a sequence of integers π0,π1,...,ππβ1
(1 β€ π β€ 10E5, 1 β€ ππ β€ 10E9 for all 0 β€ π < π).
Output: Integer of the inversions in the sequence.
Solution
Given a set of points on a line and a set of segments on a line. The goal is to compute, for each point, the number of
segments that contain this point.
Input: The first line contains two non-negative integers π and π defining the number of segments and the number of
points on a line, respectively. The next π lines contain two integers ππ,ππ defining the π-th segment (ππ, ππ). The next
line contains π integers defining points π₯1,π₯2,...,π₯π (1 β€ π ,π β€ 50000;β10E8 β€ ππ β€ ππ β€ 10E8 for all 0 β€ π < π ;
β10E8 β€ π₯π β€ 10E8 for all 0 β€ π < π).
Output: π non-negative integers π0,π1,...,ππβ1 where ππ is the number of segments which contain π₯π.
Solution
Given π points on a plane, find the smallest distance between a pair of two (different) points.
Input: The first line contains the number π of points. Each of the following π lines defines a point (π₯π, π¦π)
(2 β€ π β€ 10E5 ; β10E9 β€ π₯π,π¦π β€ 10E9 are integers).
Output: The minimum distance.
Solution
Python 3.8
This project is licensed under the terms of the MIT license. See the LICENSE file for details.