Algorithms
Algorithms
You are given a binary array. It contains only 0s and 1s. You must count the
minimum number of swaps required to sort this array.
Input
The first line of input contains an integer n, the number of elements in the given
array.
The second line of input contains n space separated integers, elements of the given
array.
Output
Print the minimum number of swaps required to sort this array.
Constraints
1 <= n <= 100
0 <= arr[i] <= 1. arr[i] is the i th
element of the given array.
Example#1
Input
5
1 0 1 0 1
Output
1
Explanation: If we swap the first and the fourth elements array becomes sorted: 1 0
1 0 1 -> 0 0 1 1 1
Example#2
Input
5
1 1 0 0 1
Output
Next number
You are given a binary representation of some positive integer number (n) as a string.
You must generate the binary representation of n + 1.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the binary representation of n + 1.
Constraints
1 <= n <= 100
Example#1
Input
3
100
Output
101
Output
1010
Rightmost 0
You are given a positive integer n. You must set the rightmost unset bit in
the binary representation of n.
Input
The only line of input contains an integer n.
Output
Print n in base 10 system after setting the rightmost unset bit in the binary
representation of it.
Constraints
1 <= n <= 107
Example#1
Input
5
Output
7
Example#2
Input
7
Output
15
lower-upper, upper-lower
You are given a string consisting of lowercase and uppercase Latin letters. Replace
each lowercase letter with a corresponding uppercase letter and each uppercase letter
- with a corresponding lowercase letter.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the given string after replacing characters.
Constraints
1 <= n <= 100
Example#1
Input
3
AbC
Output
aBc
Example#2
Input
2
xY
Output
Xy
Explanation: x -> X, Y - y.
Reverse string
You are given a string consisting of n lowercase Latin letters. Print reverse
of string and remove the characters from the reversed string where there are
vowels in the original string.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print reverse of string and remove the characters from the reversed string
where there are vowels in the original string.
Constraints
1 <= n <= 1000
Example#1
Input
3
abc
Output
ba
Example#2
Input
5
bbaba
Output
abb
Factorial
You are given n queries. Each of them contains a single integer. For each given
integer you must calculate the factorial of it modulo 1009.
Input
The first line of input contains an integer n, the number of queries.
The next line of input contains n space separated integers.
Output
For each given integer you must print the factorial of it modulo 1009.
Constraints
1 <= n <=100
1 <= k <= 1000, where k is any given number.
Example#1
Input
3
3 1 4
Output
6 1 24
Explanation:
3: 1 * 2 * 3 = 6 (mod 1009);
1: 1 = 1 (mod 1009);
4: 1 * 2 * 3 * 4= 24 (mod 1009);
Example#2
Input
2
2 6
Output
2 720
Explanation:
2: 1 * 2= 2 (mod 1009);
6: 1 * 2 * 3 * 4 * 5 * 6 = 720 (mod 1009);
Make Anagram
You are given two strings. Each of them contains distinct lowercase Latin letters. In
one move you can remove any character from any given string. Print the minimal
number of moves to convert the given strings into anagram of each other.
Input
The first line of input contains two space separated integers n and k, where n is the
length of the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print the minimal number of moves to convert the given strings into anagram of each
other.
Constraints
1 <= n, k <= 25
Example#1
Input
5 6
abcde
abcdfk
Output
3
Explanation: We should remove 3 characters (One from the first string and two from
the second string): abcde-> abcd, abcdff -> abcd.
Example#2
Input
3 2
qwe
ew
Output
Explanation: We should remove only one character from the firs string: qwe -> qw,
ew -> ew.
Make Anagram
You are given two strings. Each of them contains distinct lowercase Latin letters. In
one move you can remove any character from any given string. Print the minimal
number of moves to convert the given strings into anagram of each other.
Input
The first line of input contains two space separated integers n and k, where n is the
length of the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print the minimal number of moves to convert the given strings into anagram of each
other.
Constraints
1 <= n, k <= 25
Example#1
Input
5 6
abcde
abcdfk
Output
3
Explanation: We should remove 3 characters (One from the first string and two from
the second string): abcde-> abcd, abcdff -> abcd.
Example#2
Input
3 2
qwe
ew
Output
Explanation: We should remove only one character from the firs string: qwe -> qw,
ew -> ew.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the last word in this string.
Constraints
1 <= n <= 100
Example#1
Input
7
agb fgh
Output
fgh
Explanation: There are two words in this string: "agb", "fgh". The last of them is
"fgh".
Example#2
Input
17
ninjas in pyjamas
Output
pyjamas
Explanation: There are three words in the given string: "ninjas", "in", "pyjamas".
The last of them is "pyjamas"
Subsegment of ‘1’s
You are given a binary string. Print the length of the longest possible
subsegment of ‘1’s by changing at most one ‘0’.
Input
The first line of input contains two integers n and k, where n is the length of
the given string.
The second line of input contains a string.
Output
Print the length of the longest possible subsegment of ‘1’s by changing at
most one ‘0’.
Constraints
1 <= n <= 30
0 <= k <= n
Example#1
Input
7
1011001
Output
4
Explanation: In this case we can make one change, there are three ways:
Way 1: 1011001-> 1011011. The length of the longest subsegment of ‘1’s is
2 (1011011 or 1011011).
Way 2: 1011001-> 1011101. The length of the longest subsegment of ‘1’s is
3 (1011101).
Way 2: 1011001-> 1111001. The length of the longest subsegment of ‘1’s is
4 (1111001).
Thus the result is 4
Example#2
Input
3
111
Output
Explanation: We can nor make any changes, because there are no '0's. The
length of the longest subsegment of ‘1’s is 3.
Matrix sorting
You are given n x n matrix A. You must sort the given matrix in a way such
that all elements in a row are sorted in increasing order and for row ‘i’, where
1 <= i <= n-1, first element of row 'i' is greater than or equal to the last element
of row 'i-1'.
Input
The first line of input contains an integer n.
Each of next n lines contains n space separated integers.
Output
Print the given matrix after sorting.
Constraints
1 <= n <= 30
Example#1
Input
2
3 1
2 4
Output
1 2
3 4
Explanation: Both rows are sorted and the first element of row 1 is greater
than the last element of row 0.
Example#2
Input
2
1 34
33 1
Output
1 1
33 34
Explanation: Both rows are sorted and the first element of row 1 is greater
than the last element of row 0.
Array sorting
You are given an array of n distinct elements and a number k, arrange array
elements according to the absolute difference with k, i. e., element having
minimum difference comes first and so on.
If two or more elements are at equal distance arrange them in same
sequence as in the given array.
Input
The first line of input contains two integers n and k.
The second line of input contains n space separated integers, elements of
the given array.
Output
Print array after sorting. Print elements on a single line and use a white space
as separator.
Constraints
1 <= n, k <= 50
Example#1
Input
3 3
2 1 3
Output
3 2 1
Example#2
Input
3 0
1 2 3
Output
1 2 3
Valid expression
You are given a string which contains only digits form 1 to 9 (inclusive) and
an integer k. You must check whether it is possible to create an expression
which evaluates to k using "+" operator in given string of digits.
Input
The first line of input contains two integers n and k, where n is the length of
the given string and k is the target value.
The second line of input contains a string.
Output
Print "YES" (without quotes) if it is possible to create an expression which
evaluates to k using "+" operator in given string of digits, print "NO" (without
quotes) otherwise.
Constraints
1 <= n <= 7
1 <= k <= 107
Example#1
Input
3 28
271
Output
YES
Example#2
Input
3 90
234
Output
NO
Find reminder
You are given an array consisting of n integers. You must find the reminder
of ((arr[0] * arr[1] * arr[2] * ... * arr[n - 1]) / k)
Input
The first line of input contains two space separated integers n and k.
The second line of input contains n space separated integers, elements of
the given array.
Output
Print the reminder of of ((arr[0] * arr[1] * arr[2] * ... * arr[n - 1]) / k).
Constraints
1 <= n <= 100
1 <= k <= 1000
1 <= arr[i] <= 100. arr[i] is the ith element of the given array.
Example#1
Input
3 5
2 2 3
Output
2
Explanation: 2 * 2 * 3 = 12. 12 % 5 = 2.
Example#2
Input
4 7
3 4 5 6
Output
Binary string
You are given an integer number represented as a binary string and some
positive integer k. You must check whether the given binary string is evenly
divisible by 2k or not.
Input
The first line of input contains two integers n and k, where n is the length of
the given binary string.
The second line of input contains the binary string.
Output
Print "YES" (without quotes) if the given binary string is evenly divisible by 2k
, print "NO" (without quotes) otherwise.
Constraints
1 <= n <= 100
1 <= k <= 5
Example#1
Input
3 1
110
Output
YES
Example#2
Input
3 1
111
Output
NO
Input
The first line of input contains an integer n, the number of elements in the given
array.
The second line of input contains n space separated integers, elements of the given
array.
Output
Print the number of ways to remove one element so that xor of that array elements
becomes 0.
Constraints
1 <= n <= 100
Example#1
Input
4
1 0 0 1
Output
2
Example#2
Input
3
1 0 0
Output
Input
The first line of input contains two space separated integers n and k, where
n is the length of the given string.
The second line of input contains a string.
Output
Print the number of substrings which contain exactly k distinct characters.
Constraints
1 <= n <= 100
Example#1
Input
5 3
aabcc
Output
3
Example#2
Input
4 3
aabc
Output
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Reverse only the vowels of the given string and print.
Constraints
1 <= n <= 100
Example#1
Input
5
adovi
Output
idova
Explanation: 'd' and 'v' are not vowels and we must not change anything at their
positions. We must reverse 'a'-'o'-'i' and insert them in 0, 2, 4 positions.
Example#2
Input
4
olla
Output
allo
Explanation: 'l' and 'l' are not vowels and we must not change anything at their
positions. We must reverse 'o'-a' and insert them in 0, 3 positions.
Input
The first line of input contains two space separated integers n and k, where
n is the length of the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print the maximum length of some prefix of the str1 which occur in str2 as
subsequence.
Constraints
1 < n <= 100
1 < k <= 100
Example#1
Input
4 5
abcd
ghafb
Output
2
Output
Strong numbers.
You are given a positive integer n. Print "YES" (without quotes) if n is the
strong number, print "NO" (without quotes) otherwise. A number is called
Strong if sum of its digits powered with their respective positions is equal to
the number itself.
Input
The only line of input contains integer n.
Output
Print "YES" (without quotes) if n is the strong number, print "NO" (without
quotes) otherwise.
Constraints
1 <= n <= 1000000
Example#1
Input
89
Output
YES
Explanation: 81 + 92 = 89
Example#2
Input
50
Output
NO
Explanation: 51 + 02 != 50
Exactly k 1s
You are given a binary string, it contains only 1s and 0s. You must count the
number of substrings which contain exactly k 1s.
Input
The first line of input contains an integer n, the length of the given string, and
then the integer k separated by a space.
The second line of input contains a string.
Output
Print the number of substrings which contain exactly k 1s.
Constraints
1 <= n <= 100
1 <= k <= n
Example#1
Input
4 2
1011
Output
3
Explanation: There are three substrings which contain exactly two 1s: 101,
011, 11.
Example#2
Input
2 1
01
Output
Explanation: There are two substrings which contain exactly one 1: 01, 1.
Input
The first line of input contains an integer n, which is the number of elements
in the given array.
The second line of input contains n space separated integers, which are the
elements of the given array.
Output
Print the last two digits of the product of the array values.
Note that you always need to print two digits.
Constraints
1 <= n <= 100
1 <= arr[i] <= 100. arr[i] is the ith element of the given array.
Example #1
Input
2
25 10
Output
50
Explanation: 25 * 10 = 250
Example #2
Input
3
2 4 5
Output
40
Explanation: 2 * 4 * 5 = 40
Subsequence
You are given two strings str1 and str2. Both of them contain only lowercase Latin
letters. You must check whether str1 is a subsequence of str2 or not.
Input
The first line of input contains two space separated integers n and k, where n is the
length of the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print "YES" (without quotes) if str1 is a subsequence of str2, print "NO" (without
quotes) otherwise.
Constraints
1 <= n <= 100
1 <= k <= 100
Example#1
Input
3 5
abc
adbcc
Output
YES
Example#2
Input
1 3
a
bcd
Output
NO
Friends
You are given a positive integer n. You must find number of "friends" of n.
We say that k is a friend of n if:
1 <= k < n
k xor n > n
Input
The only line of input contains an integer n.
Output
Print the number of "friends" of n
Constraints
1 <= n <= 105
Example#1
Input
5
Output
2
Example#2
Input
4
Output
3
Binary anagrams
You are given two positive integers. You must check if binary representations
of these integers are anagrams of each other or not.
Input
The only line of input contains two space separated integers n and k.
Output
Print "YES" (without quotes) if the given numbers are anagrams of each
other, print "NO" (without quotes) otherwise.
Constraints
1 <= n <= 105
1 <= k <= 105
Example#1
Input
3 5
Output
YES
Example#2
Input
3 4
Output
NO
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print "YES" (without quotes) if the given string follows pattern 0k1k, print "NO"
(without quotes) otherwise.
Constraints
1 <= n <= 50
Example#1
Input
4
0011
Output
YES
Example#2
Input
6
001001
Output
NO
Leaf nodes
You are given a tree represented as undirected graph. Print the number of
leaf nodes.
Input
The first line of input contains an integer r, where r is the root of the given
tree.
The second line of input contains two integers n and k, where n is the number
of nodes and k is the number of edges.
The third line of input contains n integers - nodes.
Each of next k lines of input contains two space separated integers ai and b
i. The pair (ai; bi) means that there is an edge from node a to node b.
Output
Print the number of leaf nodes.
Constraints
1 <= n <= 50
Example#1
Input
0
6 5
0 1 2 3 4 5
0 1
0 2
1 3
1 4
1 5
Output
4
Explanation:
Level 0: 0.
Level 1: 1, 2.
Level 2: 3, 4, 5.
There are four leaf nodes: 2, 3, 4, 5.
Example#2
Input
0
3 2
0 1 2
0 1
0 2
Output
Explanation:
Level 0: 0.
Level 1: 1, 2.
There are two leaf nodes: 1, 2.
Forest
You are given n nodes of a forest (collection of trees). Print the number of
trees in the forest.
Input
The first line of input contains integer n, the number of edges.
Each of next n lines of input contains two space separated integers ai and b
i. The pair (ai; bi) means that there is an edge from node a to node b.
Output
Print the number of trees in the forest.
Constraints
1 <= n <= 25
Example#1
Input
3
0 1
0 2
3 4
Output
2
Example#2
Input
2
0 1
1 2
Output
Explanation: There is only one tree in the given forest: 0->1, 1->2.
The first repeated character
You are given a string consisting of n lowercase Latin letters. Find the first repeated
character. If there is no such character print "-1".
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the first repeated character.
Constraints
1 <= n <= 30
Example#1
Input
4
abab
Output
a
Example#2
Input
3
abc
Output
-1
Input
The first line of input contains an integer n, the number of elements in the
given list.
The second line of input contains n space separated integers, elements of
the given list.
Output
Print the first half of the given list.
Constraints
2 <= n <= 100
1 <= l <= 100. l is an element of the given list.
Example#1
Input
4
1 2 3 4
Output
1 2
Example#2
Input
4
4 3 5 66
Output
4 3
odd-even
You are given a circular singly linked list consisting of n integers. Print "YES"
(without quotes) if next node for every odd element is even element and next
node for every even element is odd element, print "NO" (without quotes)
otherwise.
Input
The first line of input contains an integer n, the number of elements in the
given list.
The second line of input contains n space separated integers, elements of
the given list.
Output
Print "YES" (without quotes) if next node for every odd element is even
element and next node for every even element is odd element, print "NO"
(without quotes) otherwise.
Constraints
1 <= n <= 100
1 <= l <= 100. l is an element of the given list.
Example#1
Input
4
1 2 3 4
Output
YES
Example#2
Input
3
1 2 3
Output
NO
Explanation: In this case next node for 3 (odd number) is 1 (odd number)
You are given n distinct integers and integer k. Construct a BST (binary
search tree) with a given sequence of numbers. After inserting the given n
integers you must insert k in the given BST and print the parent node of it.
It is guaranteed that there is no node in the given BST with a value k.
Input
The first line of input contains an integer n, the number of elements.
The second line of input contains n space separated integers, elements of
a BST.
Output
Print the parent node of a new node.
Constraints
1 <= n <= 100
1 <= b <= 100. b is an element of a BST.
Example#1
Input
4 3
4 5 1 10
Output
Example#2
Input
3 5
2 1 3
Output
3
Explanation: In this example the parent of the new element(5) is 3.
You are given a string consisting of n lowercase Latin letters. Print the string
with alternate occurrences of any character dropped.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the string with alternate occurrences of any character dropped.
Constraints
1 <= n <= 100
Example#1
Input
3
aba
Output
ab
Example#2
Input
5
ababg
Output
abg
Reverse words
You are given a string consisting of words (separated with a single white
space(' ')). Each word contains lowercase Latin letters. Reverse each word
of this string and print the result string.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Reverse each word of this string and print the result string.
Constraints
1 <= n <= 100
Example#1
Input
14
i love football
Output
i evol llabtoof
Explanation: This string contains three words("i", "love" and "football"), after
reversing each of them the result is: "i evol llabtoof"
Example#2
Input
6
hjj hu
Output
jjh uh
Explanation: This string contains two words("hjj" and "hu"), after reversing
each of them the result is: "jjh uh"
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the given string after removing numeric symbols from it.
Constraints
1 <= n <= 50
Example#1
Input
6
1qw23e
Output
qwe
Explanation: There are three numeric symbols in the given string ("1qw23e"),
after removing them we get: "qwe".
Example#2
Input
3
abc
Output
abc
Rotation of a palindrom
You are given a string consisting of n lowercase Latin letters. Print "YES"
(without quotes) if the given string is a clockwise rotation of a palindrome,
print "NO" (without quotes) otherwise.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print "YES" (without quotes) if the given string is a clockwise rotation of a
palindrome, print "NO" (without quotes) otherwise.
Constraints
1 <= n <= 100
Example#1
Input
3
xxy
Output
YES
Example#2
Input
4
abcd
Output
NO
Median
You are given an array consisting of (2 * n + 1) integers. Find the median of
the given elements.
The median is the value separating the higher half from the lower half of a
data sample.
Input
The first line of input contains an integer n, the number of elements in the
given array.
The second line of input contains (2 * n + 1) space separated integers,
elements of the given array.
Output
Print the median of the given elements.
Constraints
1 <= n <= 1000
Example#1
Input
1
1 3 2
Output
2
Example#2
Input
2
3 2 2 54 1
Output
Palindromic word
You are given a string consisting of words. Words are separated with white
spaces and each of them contains lowercase Latin letters. Print the length of
the longest palindromic word in the given string. It is guaranteed that the
given string contains at least one word. If there is no palindromic word in the
given string print -1.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the length of the longest palindromic word in the given string.
Constraints
1 <= n <= 100
Example#1
Input
4
a bb
Output
2
Explanation: In this case the length of the longest palindromic word ("bb") is
2.
Example#2
Input
11
acs ede ded
Output
Explanation: In this case the length of the longest palindromic word ("ede",
"ded") is 3.
Common substring
You are given two strings. Print "YES" (without quotes), if given strings have
common substring of length l, print "NO" (without quotes) otherwise.
Input
The first line of input contains three space separated integers l, n and k, where l is
the length of required substring,n is the length of the first string and k is the length
of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print "YES" (without quotes), if given strings have common substring of length k,
print "NO" (without quotes) otherwise.
Constraints
1 <= n, k <= 100
Given string contains only lowercase Latin letters.
Example#1
Input
2 3 3
abc
bcf
Output
YES
Example#2
Input
2 3 3
abc
bbf
Output
NO
Hamming Distance
You are given two strings of equal length, Print the Hamming Distance
between these strings.
Hamming distance between two strings of equal length is the number of
positions at which the corresponding character are different.
Input
The first line of input contains an integer n, the length of the given strings.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print the Hamming Distance between these strings.
Constraints
1 <= n <= 50
Given strings contain only lowercase Latin letters.
Example#1
Input
3
abc
abv
Output
1
Example#2
Input
4
bool
loob
Output
Input
The first line of input contains two integers n and k, where n is the length of str1 and
k is the length of str2.
The second line of input contains str1.
The third line of input contains str2.
Output
Print the minimum number of steps required to make two strings anagram without
deleting any character.
Constraints
1 <= n, k <= 50
Example#1
Input
3 3
abc
bcf
Output
1
Example#2
Input
3 4
qwe
ewqq
Output
String sorting
You are given two strings , a pattern and a string. The task is to sort string
according to the order defined by pattern. It is guaranteed that pattern has
all characters of the string and all characters in pattern appear only once.
Input
The first line of input contains two integers n and k, where n is the length of
the pattern and k is the length of the string.
The second line of input contains a pattern.
The second line of input contains a string.
Output
Print string after sorting.
Constraints
1 <= n <= 26
1 <= k <= 50
Example#1
Input
3 4
bca
bbac
Output
bbca
Explanation: According to the pattern ("bca") the highest priority has 'b', then
comes 'c' and then comes 'a'.
Example#2
Input
2 4
yx
xyxy
Output
yyxx
Explanation: According to the pattern ("yx") the highest priority has 'y' and
then comes 'x'.
Not Palindrome
You are given a string consisting of n lowercase Latin letters. You must print
the length of the largest substring which is not a palindrome.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the length of the largest substring which is not a palindrome.
Constraints
1 <= n <= 100
Example#1
Input
3
aba
Output
2
Example#2
Input
5
abbaa
Output
Swap Sort
You are given an array consisting of n integers and integer k. You must
produce the lexicographical minimal array after at most k-swaps. Only
consecutive pairs of elements can be swapped.
Input
The first line of input contains two integers n and k, where n is the number of
elements in the given array.
The second line of input contains n space separated integers, elements of
the given array.
Output
Print the lexicographical minimal array after at most k-swaps.
Constraints
1 <= n <= 100
1 <= k <= 1000
1 <= arr[i] <= 100. arr[i] is the i element of the given array.
th
Example#1
Input
3 2
6 4 2
Output
2 6 4
Example#2
Input
3 1
3 2 1
Output
2 3 1
Explanation: We can make only one swap: {3, 2, 1} -> {2, 3, 1}.
Maximal number
You are given a string consisting of n digits ('1', '2', '3', ..., '9'). Rearrange the
digits to form the maximum possible number.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the string after rearranging its digits.
Constraints
1 <= n <= 100
Example#1
Input
3
231
Output
321
Explanation: We can form the following numbers: 231, 213, 132, 123, 321,
312. The largest is 321.
Example#2
Input
2
12
Output
21
Explanation: We can form the following numbers: 12 and 21. The largest is
21.
String conversion
You are given a string consisting of n lowercase Latin letters. You must
convert the given string into a numeric string (string which contains only
numbers 0 - 9). convert 'a' into 0, 'b' - 1, 'c' - 2, ..., 'z' - 25.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print a corresponding numeric string.
Constraints
1 <= n <= 50
Example#1
Input
3
abz
Output
0125
Output
911
Smallest Number
You are given a positive integer as a string. You can make only one or zero
swaps and need to form the smallest possible number.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the smallest possible positive integer after making a single swap.
Constraints
1 <= n <= 100
Example #1
Input
3
213
Output
123
Explanation: We can swap the first two digits 213 -> 123.
Example #2
Input
2
12
Output
12
Happy or unhappy
We calculate the sum of an array by the following way: sum = arr[0] - arr[1]
+ arr[2] - arr[3] + ... If the sum is more than 0, we say that the given array is
"happy", otherwise the given array is "unhappy". You are given an array
consisting of n integers. Print "HAPPY" (without quotes) if the given array is
happy, print "UNHAPPY" (without quotes) otherwise.
Input
The first line of input contains an integer n, the number of elements in the
given array.
The second line of input contains n space separated integers, elements of
the given array.
Output
Print "HAPPY" (without quotes) if the given array is happy, print "UNHAPPY"
(without quotes) otherwise.
Constraints
1 <= n <= 100
1 <= arr[i] <= 1000. arr[i] is the ith element of the given array.
Example#1
Input
4
1 2 3 4
Output
UNHAPPY
Example#2
Input
5
5 4 6 7 1
Output
HAPPY
Remove vowels
You are given a string which may contain uppercase Latin letters, lowercase
Latin letters, white spaces, commas(',') and periods('.'). Remove the
following vowels from the given string: 'a', 'e', 'i', 'o', u', 'A', 'E', 'I', 'O', U'. It is
guaranteed that:
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the given string without vowels. If the result string contains white spaces
at the start or at the end, you must remove them.
Constraints
1 <= n <= 100
Example#1
Input
7
E Hello
Output
Hll
Explanation: There are three vowels: 'E', 'e' and 'o'. "E Hello" -> "Hll".
Example#2
Input
14
Get over here.
Output
Gt vr hr.
Explanation: There are 5 vowels: 'e', 'o', 'e', 'e', 'e'. "Get over here" - > "Gt vr
hr".
Almost palindrome
You are given a string consisting of n lowercase Latin letters. It is Almost
palindrome: There is exactly one unnecessary character. You must find and
print this unnecessary character.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print unnecessary character. If there are several results, print the
lexicographically smallest character among them.
Constraints
1 <= n <= 100
Example#1
Input
4
abca
Output
b
Example#2
Input
5
abffb
Output
Common characters
You are given two strings. Each of them contains only lowercase Latin
letters. You must print all the common characters in lexicographical order.
Input
The first line of input contains two integers n and k, where n is the length of
the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print all the common characters in lexicographical order on one line.
Separate them with a white space. If there are no common characters print
-1.
Constraints
1 <= n <= 100
1 <= k <= 100
Example#1
Input
4 4
abab
aadd
Output
a a
Example#2
Input
3 4
abc
defg
Output
-1
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the minimum number of steps to get the empty string.
Constraints
1 <= n <= 100
Example#1
Input
6
101110
Output
2
Explanation: The first step: 101110 -> 1. The second step: 1 -> ""
Example#2
Input
6
111001
Output
2
Explanation: The first step: 111001 -> 00. The second step: 00 -> ""
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print "YES" (without quotes) if it is possible to split the given string in three
nonempty distinct strings, print "NO" (without quotes) otherwise.
Constraints
1 <= n <= 100
Example#1
Input
3
abc
Output
YES
Example#2
Input
4
aabb
Output
YES
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the maximum numeric value extracted from that string. If we can not extract
numeric value from that string print -1.
Constraints
1 <= n <= 100
Example#1
Input
7
av12f14
Output
14
Explanation: We can extract the following numeric values from the given string: 1,
2, 4, 12, 14. 14 is the maximal number.
Example#2
Input
3
1c2
Output
Explanation: We can extract the following numeric values from the given string: 1,
2. 2 is the maximal number.
Rotations
You are given a string consisting of n lowercase Latin letters. You are also
given k queries. Each query contains one number ki and one character ci. ki
is a nonnegative integer and ci may has two values 'l' or 'r'. For each query
you must rotate the given string by k units left (ci = 'l') or right (ci = 'r'). Print
the result string after k rotations.
Input
The first line of input contains two integers n and k, where n is the length of
the given string and k is the number of queries.
The second line of input contains a string.
Each of next k lines contains one integer (ki) and one character (ci).
Output
Print the result string after rotations.
Constraints
1 <= n <= 100
1 <= k <= 20
Example#1
Input
3 2
abc
1 r
1 l
Output
abc
Explanation:
The first rotation: "abc" -1,r-> "cab".
The second rotation: "cab" -1,l-> "abc".
Example#2
Input
3 2
qwe
2 r
2 r
Output
eqw
Explanation:
The first rotation: "qwe" -2,r-> "weq".
The second rotation: "weq" -2,r-> "eqw".
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print concatenation of all substrings of the given string in lexicographic order.
Constraints
1 <= n <= 10
Example#1
Input
2
ab
Output
aabb
Example#2
Input
2
aa
Output
aaaa
Extra spaces
You are given a string consisting of n lowercase Latin letters and spaces.
We need to remove extra spaces.
Space is extra if its previous symbols is space.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the given string after removing extra spaces.
Constraints
1 <= n <= 1000
Example#1
Input
5
ab c
Output
ab c
Explanation: The given string contains one extra space (index 3). After
removing extra space: "ab c".
Example#2
Input
4
abcd
Output
abcd
Maximal array
You are given two arrays of integers. Given arrays have same size - n. You
must construct a new array by using the elements of the given two arrays.
For each i in range 0...(n - 1) you can pick only one element either from
the first array or second.
You must pick n elements.
Your task is to create a new array according to above rules such that sum of
its elements should be maximal.
Input
The first line of input contains an integer n, the size of given arrays.
The second line of input contains n space separated integers, elements of
the first array.
The third line of input contains n space separated integers, elements of the
second array.
Output
Print the sum of elements of a new array under modulo 10007.
Constraints
1 <= n <= 105
1 <= arr[i] <= 108. arr[i] is the ith element of the given array.
Example#1
Input
3
1 2 3
3 2 1
Output
8
Explanation: In this case we should take the first element from the second
array, second element - from any of them, the third element - from the first
array: 3 + 2 + 3 = 8. 8 % 10007 = 8
Example#2
Input
4
1 1 1 9
2 2 3 10
Output
17
Explanation: In this case we should take all elements from the second array:
2 + 2 + 3 + 10 = 17. 17 % 10007 =17
Input
The first line of input contains two space separated integers n and k, where
n is the length of the first string and k is the length of the second string.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print the indices of occurrence of the first string in the second string. Print
indices on a single line, use white space as separator.
If no such index occurs, print -1.
Constraints
1 <= n <= k <= 106
Example#1
Input
3 6
abc
abcabc
Output
0 3
Example#2
Input
4 6
abcd
abcabc
Output
-1
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the number of substrings which when convert into integer are divisible
by 3.
Constraints
1 <= n <= 100
Example#1
Input
3
123
Output
3
Explanation: There are 3 substrings which when convert into integer are
divisible by 3: 12, 123 and 3.
Example#2
Input
2
11
Output
Explanation: There are no substrings which when convert into integer are
divisible by 3.
Input
The first line of input contains an integer n, the number of elements in the
given array.
The second line of input contains n space separated Strings, elements of the
given array.
Output
Print the result string.
Constraints
1 <= n <= 20
1 <= arr[i] <= 20. arr[i] is the i element of the given array.
th
Example#1
Input
3
cg ddd a
Output
acgddd
Example#2
Input
2
b a
Output
ab
Explanation: a < b
String concatenations
You are given a string str consisting of n lowercase Latin letters. str is made
by concatenating a string str2 k times. You must find the smallest possible
length of str2.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the smallest possible length of str2.
Constraints
1 <= n <= 50
Example#1
Input
9
abcabcabc
Output
3
Example#2
Input
8
abababab
Output
String concatenations
You are given a string str consisting of n lowercase Latin letters. str is made
by concatenating a string str2 k times. You must find the smallest possible
length of str2.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the smallest possible length of str2.
Constraints
1 <= n <= 50
Example#1
Input
9
abcabcabc
Output
3
Example#2
Input
8
abababab
Output
Distinct substrings
You are given a string consisting of n lowercase Latin letters. You must print the
number of distinct nonempty substrings present in the given string.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the number of distinct nonempty substrings present in the given string.
Constraints
1 <= n <= 50
Example#1
Input
3
abc
Output
6
Explanation: There are 6 distinct nonempty substings: "a", "b", "c", "ab", "bc", "abc".
Example#2
Input
3
abb
Output
Explanation: There are 5 distinct nonempty substrings: "a", "b", "ab", "bb", "abb".
Distinct substrings
You are given a string consisting of n lowercase Latin letters. You must print the
number of distinct nonempty substrings present in the given string.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the number of distinct nonempty substrings present in the given string.
Constraints
1 <= n <= 50
Example#1
Input
3
abc
Output
6
Explanation: There are 6 distinct nonempty substings: "a", "b", "c", "ab", "bc", "abc".
Example#2
Input
3
abb
Output
5
Explanation: There are 5 distinct nonempty substrings: "a", "b", "ab", "bb", "abb".
All permutations
You are given a string consisting of n distinct lowercase Latin letters. Print
all permutations of a given string in sorted order.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print all permutations of a given string in sorted order. use a white space as
a separator.
Constraints
1 <= n <= 5
Example#1
Input
3
abc
Output
abc acb bac bca cab cba
Example#2
Input
2
ab
Output
ab ba
Minimal path
You are given a directed graph with n nodes and k edges. Print the minimum number
of edges between a given pair of nodes (start, end). If there is no path from start to
end print -1. It is guaranteed that start node and end node are not same.
Input
The first line of input contains two integers s and e, where s is start node and e
is end node.
The second line of input contains two integers n and k, where n is the number of
nodes and k is the number of edges.
The third line of input contains n integers - nodes.
Each of next k lines of input contains two space separated integers ai and bi. The pair
(ai; bi) means that there is an edge from node a to node b.
Output
Print the minimum number of edges between a given pair of nodes (s, e).
Constraints
1 <= n <= 30
1 <= k <= n * (n - 1)
Example#1
Input
1 3
3 3
1 2 3
1 2
2 3
1 3
Output
1
Explanation: In this case there are three nodes (1, 2, 3) and three edges(1->2, 2->3,
1->3). The minimal number of edges from node 1 to node 3 is 1: 1->3.
Example#2
Input
1 4
4 5
1 2 3 4
1 2
2 1
2 3
4 1
2 4
Output
Explanation: In this case there are 4 nodes (1, 2, 3, 4) and 5 edges (1->2, 2->1, 2->3,
4->1, 2-4). The minimal number of edges from node 1 to node 4 is 2: 1->2->4.
k-size subsequences
You are given a string consisting of n lowercase Latin letters. Print all k-
size distinct subsequences of the given string in sorted order.
Input
The first line of input contains two space separated integers n and k.
The second line of input contains a string.
Output
Print all k-size distinct subsequences of the given string in sorted order (on
a single line). Use white a space as a separator.
Constraints
1 <= n <= 10
Example#1
Input
3 2
abc
Output
ab ac bc
Explanation: There are 3 subsequences with the length 2: "ab", "ac", "bc".
Example#2
Input
2 2
ab
Output
ab
Character searching
You are given three strings str1, str2 and str3. Each of them contains distinct
lowercase Latin letters. Print "YES" (without quotes) if str1 contains all
characters of str2 and str3, print "NO" (without quotes) otherwise.
Input
The first line of input contains three integers n, k and m, the length of the
first, the second and the third strings respectively.
The second line of input contains the first string.
The third line of input contains the second string.
The fourth line of input contains the third string.
Output
Print "YES" (without quotes) if str1 contains all characters of str2 and str3,
print "NO" (without quotes) otherwise.
Constraints
1 <= n, k, m <= 25
Example#1
Input
5 2 3
qwert
ew
rtq
Output
YES
Explanation: The first string contains all characters of the second string
("qwert") and all characters of the third string ("qwert").
Example#2
Input
3 2 2
abc
bc
ac
Output
YES
Explanation: The first string contains all characters of the second string
("abc") and all characters of the third string ("abc").
xy-k
You are given string str consisting of lowercase Latin letters. Consider a new
string formed by repeating str exactly k (positive integer) times. Print the
number of subsequences as “xy” in the newly formed string.
Input
The first line of input contains two integers n and k.
The second line of input contains a string.
Output
Print the number of subsequences as “xy” in the newly formed string.
Constraints
1 <= n, k <= 10
Example#1
Input
3 2
xyg
Output
3
Example#2
Input
3 3
ayx
Output
Form subsequence
You are given two strings str1 and str2. Each of them contains lowercase Latin
letters. You can remove exactly 0 or 1 character from str1. Print "YES" (without
quotes) if it is possible to form a new string from str1, which is a subsequence of
str2 , print "NO" (without quotes) otherwise.
Input
The first line of input contains two space separated integers n and k, where n is the
length of str1 and k is the length of str2.
The second line of input contains the first string.
The third line of input contains the second string.
Output
Print "YES" (without quotes) if it is possible to form a new string from str1, which
is a subsequence of str2 , print "NO" (without quotes) otherwise.
Constraints
1 <= n, k <= 30
Example#1
Input
3 5
abv
dsasv
Output
YES
Example#2
Input
3 3
abc
afg
Output
NO
Substring sum
You are given a string consisting of n digits ('1', '2', ..., '9'). Print the sum of all
possible substrings of the given string.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print the sum of all possible substrings of the given string.
Constraints
1 <= n <= 6
Example#1
Input
3
123
Output
164
Example#2
Input
2
99
Output
117
Explanation: 9 + 9 + 99 = 117
Count zeros
You are given an array of n integers and number m (m <= n). You need to
choose exactly m numbers so that the number of zeros at the end of the
product of these m numbers should be the maximum possible.
Input
The first line of input contains two space separated integers - n and m.
The second line of input contains n space separated integers.
Output
Print maximal number of zeros at the end of product of the chosen subset of
length m.
Constraints
1 <= n <= 200.
1 <= m <= n.
Example #1
Input:
2 2
5 10
Output:
Example #2
Input:
4 2
2 3 10 10
Output:
10 x 10 = 100.
.
Input
The first line of input contains integer n, number of rows on the table.
The format of the next n lines of input is the following: ki a1, a2, ... aj, ..., aki. ki
is a number of boxes in ith row. aj is a number of coins in the jth.
Output
Print "John" (without quotes) If John wins.
Print "Mary" (without quotes) If Mary wins.
Print "Draw" (without quotes) in case of draw.
Constraints
1 <= n <= 100. Number of rows.
1 <= k <= 100. Number of boxes in one row.
1 <= aj <= 100. Number of coins in one box.
Example#1
Input
3
1 3
2 8 6
1 8
Output
John
Explanation: John will take the following coins: 8, 6. 8 + 6 = 14. Mary will take
the following coins: 8, 3. 8 + 3 = 11. 14 > 11, John wins.
Example#2
Input
1
6 2 4 6 7 3 2
Output
Draw
Rearrange soldiers
There are several soldiers standing on a straight line. You are given two
arrays of integers, arr1 and arr2, with the size n. For each i (0 <= i < n), there
are arr2[i] soldiers initially standing at the point arr1[i].
During each minute, each soldier chooses and performs one of the following
action:
You are also given an integer k, representing time (k minutes). The goal is
to rearrange the soldiers in such a way that each point contains at most one
soldier. Print “YES” (without quotes) if it is possible to achieve the goal in k
minutes and “NO” (without quotes) otherwise.
Input
The first line of input contains two space separated integers, n and k, the
size of the arrays and time (number of minutes) respectively.
The second line of input contains n space separated distinct integers, p1, p2,
…, pn, where pi represents some ith point.
The third line of input contains n space separated integers, s1, s2, …, sn,
where si is the number of solders on the pith point.
Output
Print “YES” (without quotes) if it is possible to achieve the goal in k minutes
and “NO” (without quotes) otherwise.
Constraints
1 <= n <= 50
-1000 <= pi <= 1000, where pi is the i element of the first array.
th
1 <= si <= 1000, where si is the ith element of the second array.
Example#1
Input
5 4
-2 0 4 3 8
3 2 3 1 4
Output
YES
Explanation: There are 13 soldiers. They can take the following positions: -
4, -3, -2, -1, 0, 2, 3, 4, 5, 7, 8, 9, 10.
Example#2
Input
3 3
0 1 2
3 3 3
Output
YES
Explanation: There are 9 soldiers. They can take the following positions: -3,
-2, -1, 0, 1, 2, 3, 4, 5.
Input
The only line of input contains two space separated integers a and b.
Output
Print the degree of friendship between a and b.
Constraints
1 <= a <= b <= 108
Example#1
Input
2 4
Output
5
Explanation: ((2 xor 3) xor 4) xor 5 = 5
Example#2
Input
4 4
Output
Input
The first line of input contains an integer n, the number of elements in the
given array.
The second line of input contains n space separated integers, elements of
the given array.
Output
Print the minimal element in it.
Constraints
1 <= n <= 105
Example#1
Input
5
3 4 5 1 2
Output
Example#2
Input
5
25 5 10 15 20
Output
Exactly k cents
You are Given a value k and infinite supply of each of c = { c1, c2, .. , cn} valued
coins. We need to collect exactly k cents from this supply.
Print minimum number of coins we have to pick.
It is guaranteed that we can collect exactly k cents.
Input
The first line of input contains an integer n, the number of types of coins in the
given supply.
The second line of input contains n space separated integers - coins.
Output
Print minimum number of coins we have to pick.
Constraints
1 <= n <= 1000
1 <= k <= 10000
1 <= c <= 1000
Example#1
Input
4 20
15 1 2 5
Output
Explanation: 20 = 15 + 5
Example#2
Input
5 10
1 2 3 4 5
Output
Explanation: 10 = 5 + 5
Friends Pairing Problem
For given N friends, found out the total number of ways in which friends can remain
single or can be paired up. Each friend can be paired only once.
Consider that friends pair {1, 2} and {2, 1} is same
Input
Single Integer N as number of friends
Output
Total number of pairing ways
Constraints
(1 <= N <= 50)
Example#1
Input
Output
Explanation:
1. {1}, {2}, {3}
2. {1,2}, {3}
3. {1,3}, {2}
4. {2,3}, {1}
Example#2
Input
Output
10
Golomb sequence
You are given an integer n. Print the first n terms of Golomb sequence.
In mathematics, the Golomb sequence is a non-decreasing integer sequence where
an is the number of times that n occurs in the sequence, starting with a1 = 1, and
with the property that for n > 1 each an is the unique integer which makes it possible
to satisfy the condition.
For example, a1 = 1 says that 1 only occurs once in the sequence, so a2 cannot be 1
too, but it can be, and therefore must be, 2. The first few values are: 1, 2, 2, 3, 3, 4,
4, 4, 5, 5, 5, 6, 6, 6, 6. (www.wikipedia.org)
Input
The only line of input contains an integer n.
Output
Print the first n terms of Golomb sequence on a single line. Use white space as
separator.
Constraints
1 <= n <= 105
Example#1
Input
Output
1 2 2
Example#2
Input
20
Output
1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8
Subset Sum
For given array and value sum S, determine if there is a subset of the given set with
sum equal to given S.
Input
The first line contains single integer N as length of array
The second line contains space separated N integers
The third line contains single integer S
Output
TRUE - If there is a subset of the given set with sum equal to given S
FALSE - Otherwise
Constraints
(1 <= N <= 1000)
Example#1
Input
10
100 2 3 4 5 6 7 8 9 200
300
Output
TRUE
Example#2
Input
5
5 1 4 2 3
100
Output
FALSE
Input
The first line contains single integer N
The second line contains space separated N integers as prices of each i'th
cake piece.
Output
Maximum value obtainable by cutting up the cake and selling the pieces.
Constraints
(1 <= N <= 105)
Example#1
Input
8
1 5 8 9 10 17 17 20
Output
24
Example#2
Input
8
3 5 8 9 10 17 17 20
Output
24
Input
The first line contains single string str1
The second line contains also single string str2
Consider that there are no " " in str1 or str2
Output
Length of longest common substring
Constraints
(1 <= str1.length() <= 5000)
(1 <= str2.length() <= 5000)
Example#1
Input
abcdef
efghi
Output
Example#2
Input
abcd_abcde
abcd+abcdef
Output
Explanation: There are two common substrings "abcd" and "abcde" longest
one is abcde with length of 5
Input
The first (and only) line contains single string S
Output
Length of LPS
Constraints
(1 <= S.length() <= 5000)
Example#1
Input
ABCDEF_X+FED_XXX
Output
Example#2
Input
ABCDEFGHI
Output
Games played
For N players playing a tournament, fin the maximum number of games the winner
can play. Consider that two players are allowed to play against each other only if
the difference between games played by them in not more than one.
Input
Single integer N
Output
Maximum number of games played by winner.
Constraints
(1 <= N <= 108)
Example#1
Input
Output
Explanation: Assume that there are (1), (2) and (3) players :
first game will be (1) - (2) and winner will play (3).
Example#2
Input
10
Output
Input
The first line contains single integer N
The second line contains space separated N integers
Output
Maximum score that can be obtained by removing all the elements.
Constraints
(1 <= N <= 104)
Example#1
Input
5
1 3 1 5 2
Output
43
Explanation:
1. Remove 1 from left side score will be 1*1 = 1
2. Then remove 2 from right side 1 + 2*2 = 5
3. Then remove 3 - score 5 + 3*3 = 14
4. Remove 1 - score 14 + 1*4 = 18
5. Remove 5 - score = 18 + 5*5 = 43
Example#2
Input
1 2
Output
Explanation:
1. 1*1 = 1
2. 1 + 2*2 = 5
Input
The first line contains single integer N
The second line contains space separated N integers
The third line contains single integer M
Output
Number of subsequences having product smaller than M
Constraints
(1 <= N <=)
(1 <= M <=)
Example#1
Input
4
1 2 3 4
10
Output
11
Example#2
Input
5
10 10 10 10 10
14
Output
Input
The first line contains single integer N
The second line contains N space separated integers as socks positions
The third line contains N space separated integers as drawers positions
Output
Minimum time to assign socks to drawers.
Constraints
(1 <= N <= 100)
Example#1
Input
3
4 -4 2
4 0 5
Output
Explanation:
1. Assign first sock at position 4 (time = 0)
2. Assign second sock at position 0 (time = 4)
3. Assign third sock at position 2 (time = 3)
So after 4 minutes socks will be their appropriate positions.
Example#2
Input
2
1 2
2 3
Output
Explanation:
1. Assign first sock at position 2 (time = 1)
2. Assign second sock at position 3 (time = 1)
So after 1 minutes socks will be their appropriate positions.
Circular Array
For a given circular array (element after an is a1), find maximum sum of the
difference between consecutive elements with rearrangement of array
element allowed i.e after rearrangement of element find |a1 – a2| + |a2 – a3| +
…… + |an – 1 – an| + |an – a1|.
Input
The first line contains single integer N
The second line contains space separated N integers
Output
Maximum sum of the differences
Constraints
(1 <= N <= 10000)
Example#1
Input
4
2 4 1 8
Output
18
Example#2
Input
3
10 12 15
Output
10
Paper Cut
Cut paper of size A x B into squares of any size. Find the minimum number of
squares that can be cut from the paper.
Input
The first line contains single integer A
The second line contains single integer B
Output
Minimum number of squares
Constraints
(1 <= A,B <= 100)
Example#1
Input
13
29
Output
Explanation: 2 ( 13 x 13 ) + 4 ( 3 x 3) + 3 ( 1 x 1 ) = 9
Example#2
Input
9
9
Output
Input
The first line contains single integer N
The second line contains space separated N integer
Output
Difference between the group with highest sum and one with lowest sum is
minimum
Constraints
(1 <= N <= 105)
Example#1
Input
4
10 9 1 10
Output
Explanation: Group (10, 9) and (1, 10), difference between their sum is 8, is
other cases result will be > 8 (For example (10 + 10) - (9 + 1))
Example#2
Input
6
3 7 11 1 4 5
Output
Explanation: Group as (1, 11), (4, 5) and (3, 7), group with highest sum is,
1 + 11 = 12, and lowest 4 + 5 = 9, and 12 - 9 = 3
Input
The first line contains single integer N
The second line contains space separated N integers as arrival times
The third line contains space separated N integers as departure times
Output
Minimum number of platforms
Constraints
(1 <= N <= 1000)
Example#1
Input
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000
Output
Explanation: There are at-most three trains at a time between 11:00 to 11:20
Example#2
Input
2
1000 1100
1200 1300
Output
Explanation: There are at-most two trains at a time between 11:00 to 12:00
Input
The first line contains single integer N
The second line contains N space separated integers
The third line contains single integer K
Output
Single integer - index of K
If there is no K in array, print -1
Constraints
(1 <= N <= 104)
Example#1
Input
7
9 3 15 29 30 31 32
15
Output
Example#2
Input
5
2 1 4 3 9
9
Output