0% found this document useful (0 votes)
331 views

Algorithms

You are given a string with lowercase letters and spaces. Print the last word in the string. There will be at least one word and no consecutive spaces.

Uploaded by

candy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
331 views

Algorithms

You are given a string with lowercase letters and spaces. Print the last word in the string. There will be at least one word and no consecutive spaces.

Uploaded by

candy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 113

Binary array

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

Explanation: We need 2 swaps:


1) 1 1 0 0 1 -> 0 110 1
2) 0 1 1 0 1 -> 0 0 1 1 1

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

Explanation: (100)2 = (4)10. 4 + 1 = 5. (101)2 = (5)10


Example#2
Input
4
1001

Output

1010

Explanation: (1001)2 = (9)10. 9 + 1 = 10. (1010)2 = (10)10

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

Explanation: Binary representation of 5:


00000000000000000000000000000101.
00000000000000000000000000000101 ->
00000000000000000000000000000111 = 710

Example#2
Input
7

Output

15

Explanation: Binary representation of 7:


00000000000000000000000000000111.
00000000000000000000000000000111 ->
00000000000000000000000000001111 = 1510

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

Explanation: A -> a, b -> B, C -> c.

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

Explanation: Reverse: "abc" - "cba". Remove character from index 0: "cba" -


> "ba".

Example#2
Input
5
bbaba

Output

abb

Explanation: Reverse: "bbaba" - "ababb". Remove characters from index 2


and 4: "ababb" -> "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.

The last word


You are given a string consisting of lowercase Latin letters and white spaces. Print
the last word in this string. It is guaranteed that there are no two consecutive white
spaces in the given string and it contains at least one word.

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

Explanation: arr: {|2 - 3| = 1, |1 - 3| = 2, |3 - 3| = 0}. After sorting: 3, 2, 1.

Example#2
Input
3 0
1 2 3

Output

1 2 3

Explanation: arr: {|1 - 0| = 1, |2 - 0| = 2, |3 - 0| = 3}. After sorting: 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

Explanation: We can create the following valid expression: 27 + 1 = 28.

Example#2
Input
3 90
234

Output

NO

Explanation: We can not create a valid expression.

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

Explanation: 3 * 4 * 5 * 6 = 360. 360 % 7 = 3.

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

Explanation: (110)2 = (6)10. 21 = 2. 6 / 2 = 3(0)

Example#2
Input
3 1
111

Output

NO

Explanation: (111)2 = (7)10. 21 = 2. 7 / 2 = 3(1)

Binary strings xor


You are given a binary array, it contains only 0s and 1s. You must find the number
of ways to remove one element so that xor of that array elements becomes 0.

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

Explanation: There are two ways:


1) We can remove the first 0: {1, 0, 1}, 1 xor 0 xor 1 = 0
2) We can remove the second 0: {1, 0, 1}, 1 xor 0 xor 1 = 0

Example#2
Input
3
1 0 0

Output

Explanation: There is only one way: Remove 1: {0, 0}. 0 xor 0 = 0


Distinct characters
You are given a string consisting of lowercase Latin letters. You must count
the number of substrings which contain exactly k distinct characters.

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

Explanation: aabc, aabcc, abc, abcc

Example#2
Input
4 3
aabc

Output

Explanation: aabc, abc.


Vowels
You are given a string consisting of n lowercase Latin letters. Reverse only the
vowels ('a', 'e', 'i', 'o', 'u') 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
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.

Maximal prefix subsequence


You are given two strings str1 and str2. Both of them contain lowercase Latin
letters. You must find the maximum length of some prefix of the str1 which
occur in str2 as subsequence.

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

Explanation: abcd - ghafb. Length of "ab" is 2.


Example#2
Input
5 5
abcde
fabcd

Output

Explanation: abcde - fabcd. Length of "abcd" is 4.

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.

Last two digits


You are given an array consisting of n integers. Print the last two digits of the
product of its array values.

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

Explanation: abc - "adbcc

Example#2
Input
1 3
a
bcd

Output

NO

Explanation: "a" is not a subsequence of "bcd".

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

Explanation: 2 xor 5 = 7, 7 > 5. 3 xor 5 = 6, 6 > 5.

Example#2
Input
4

Output
3

Explanation: 1 xor 4 =5, 5 > 4. 2 xor 4 = 6. 6 > 4. 3 xor 4 = 7, 7 > 4.

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

Explanation: Binary representation of 3:


00000000000000000000000000000011. Binary representation of 5:
00000000000000000000000000000101. As we can see they are anagrams
of each other.

Example#2
Input
3 4

Output

NO

Explanation: Binary representation of 3:


00000000000000000000000000000011. Binary representation of 4:
00000000000000000000000000000100. As we can see they are not
anagrams of each other.
0k1k
You are given a binary string, Print "YES" (without quotes) if the given string
follows pattern 0k1k (it has '0's followed by '1's such that the number of '0's and '1's
are same), 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 follows pattern 0k1k, print "NO"
(without quotes) otherwise.

Constraints
1 <= n <= 50

Example#1
Input
4
0011

Output
YES

Explanation: The given string follows pattern 0k1k. In this case k is 2

Example#2
Input
6
001001
Output

NO

Explanation: The given string does not follow pattern 0 k1k.

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

Explanation: There are two trees in the given forest:


The first tree: 0->1, 0->2
The second tree: 3->4

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

Explanation: The first repeated character is 'a', index: 2.

Example#2
Input
3
abc

Output

-1

Explanation: There is no repeated character.


The first half of circular linked list
You are given a circular singly linked list consisting of even number of
elements. Implement the method which takes the head of the given list as an
argument and prints the first half of it.

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

Explanation: The first half of the given list: 1, 2.

Example#2
Input
4
4 3 5 66
Output

4 3

Explanation: The first half of the given list: 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

Explanation: 1->2, 2->3, 3->4, 4->1

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

Explanation: In this example the parent of the new element (3) is 1.

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

Explanation: We ignore the second occurrence of 'a' (position: 2).

Example#2
Input

5
ababg
Output

abg

Explanation: We ignore the second occurrence of 'a' (position: 2) and the


second occurrence of 'b' (position: 3).

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"

Remove numeric symbols


You are given a string consisting of n lowercase Latin letters and numeric
symbols ('0', '1', '2', ..., '9'). Print the given string after removing numeric
symbols from it.

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

Explanation: There are no numeric symbols in the given string.

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

Explanation: The given string is a rotation of a palindrome: "xyx" ->(clockwise


rotation)->( "xxy")

Example#2
Input
4
abcd

Output

NO

Explanation: The given string is not a clockwise rotation of a palindrome.

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

Explanation: The median of the given elements is 2: 1, 2, 3.

Example#2
Input
2
3 2 2 54 1

Output

Explanation: The median of the given elements is 2: 1, 2, 2, 3, 54.

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

Explanation: Given strings have common substring of length 2 - "ab".

Example#2
Input
2 3 3
abc
bbf

Output
NO

Explanation: There is no common substring of length 2.

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

Explanation: There is one different character: abc - abv

Example#2
Input
4
bool
loob

Output

Explanation: There are two different characters: bool - loob

Minimum steps to make anagram


You are given two strings str1 and str2, we need to find the minimum number of
steps required to make two strings anagram without deleting any character. In one
step you can replace any character with other character or add any character in any
string.

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

Explanation: We can convert 'f' to 'a' in str2: "bcf" -> "bca".

Example#2
Input
3 4
qwe
ewqq

Output

Explanation: We can add 'q' in str1: "qwe" -> "qweq".

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

Explanation: The length of the longest non-palindromic substring is 2, we can


take the following substrings: "ab" or "ba".

Example#2
Input
5
abbaa

Output

Explanation: The length of the longest non-palindromic substring is 5, we can


take the following substring: "abbaa"

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

Explanation: We can make two swaps.


First move: {6, 4, 2} -> {6, 2, 4}
Second move: {6, 2, 4} -> {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

Explanation: 'a' -> 0, 'b' -> 1, 'z' -> 25


Example#2
Input
3
jbb

Output

911

Explanation: 'j' -> 9, 'b' -> 1.

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

Explanation: We do not need to make any swaps.

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

Explanation: 1 - 2 + 3 - 4 = -3. -2 <= 0. Thus the given array is unhappy.

Example#2
Input
5
5 4 6 7 1

Output

HAPPY

Explanation: 5 - 4 + 6 - 7 + 1 = 1. 1 > 0. Thus the given array is 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:

 There are no two consecutive white spaces in the given strings


 There are no white spaces at the start and at the end of the given string.
 You will not get an empty string as a result.

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

Explanation: If we remove 'b' or 'c' we get a palindrome ("aca" or "aba"). 'b'


< 'c'. Thus 'b' is the result.

Example#2
Input
5
abffb

Output

Explanation: 'a' is unnecessary character: "bffb"

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

Explanation: There are two common characters: abab - aadd

Example#2
Input
3 4
abc
defg

Output

-1

Explanation: There are no common characters.


Remove palindromic subsequences
You are given a binary string, it contains only 0s and 1s. In one step you can remove
any palindromic subsequence from the given string. You must count the minimum
number of steps to get the empty 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 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 -> ""

Three distinct strings


You are given a string consisting of n lowercase Latin letters. You must check
whether it is possible to split the given string in three nonempty distinct
strings or not.

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

Explanation: "abc" -> "a", "b", "c".

Example#2
Input
4
aabb

Output

YES

Explanation: "aabb" -> "a", "ab", "b".

Maximal numeric value


You are given a string consisting of lowercase Latin letters and number digits (0, 1,
2,.., 9). Extract maximum numeric value from that string. If we can not extract
numeric value from that 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 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".

Substrings in lexicographic order


You are given a string consisting of n lowercase Latin letters. Print
concatenation of all substrings of the given string in lexicographic 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 concatenation of all substrings of the given string in lexicographic order.

Constraints
1 <= n <= 10

Example#1
Input
2
ab

Output
aabb

Explanation: There three nonempty substrings: "a", "ab", "b" (lexicographic


order).

Example#2
Input
2
aa

Output

aaaa

Explanation: There three nonempty substrings: "a", "a", "aa" (lexicographic


order).

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

Explanation: The given string does not contain extra space.

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

First string in the second string


You are given two strings. Print the indices of occurrence of the first string in
the second string. If no such index occurs, print -1.
We use 0-based indexing.

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

Explanation: abcabc - index 0, abcabc - index 3.

Example#2
Input
4 6
abcd
abcabc

Output

-1

Explanation: The first string is not a substring of the second string.

Number of substrings divisible by 3


You are given a positive integer number as a string. You must count the
number of substrings which when convert into integer are divisible by 3.

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.

Lexicographically smallest string


You are given an array consisting of n strings. You must concatenate them
in an order that produces the lexicographically smallest possible string.

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

Explanation: a < cg < ddd

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

Explanation: The smallest length of str2 is 3, str2: "abc".

Example#2
Input
8
abababab

Output

Explanation: The smallest length of str2 is 2, str2: "ab".

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

Explanation: The smallest length of str2 is 3, str2: "abc".

Example#2
Input
8
abababab

Output

Explanation: The smallest length of str2 is 2, str2: "ab".

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

Explanation: There are 6 permutations of string "abc": "abc", "acb", "bac",


"bca", "cab", "cba".

Example#2
Input
2
ab
Output

ab ba

Explanation: There are two permutations of string "ab": "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

Explanation: There is only one subsequence with the length 2: "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

Explanation: At first we form the following string: "xygxyg". The number of


subsequences as “xy” in the newly formed string is 3: "xygxyg", "xygxyg",
"xygxyg".

Example#2
Input
3 3
ayx
Output

Explanation: At first we form the following string: "ayxayxayx". The number


of subsequences as “xy” in the newly formed string is 3: "ayxayxayx",
"ayxayxayx", "ayxayxayx".

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

Explanation: If we remove the second character from str1 it becomes the


subsequence of str2: "abv" -> "av". "av" - "dsasv"

Example#2
Input
3 3
abc
afg

Output

NO

Explanation: We can not form the subsequence of str2 from str1.

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

Explanation: 1 + 2 + 3 + 12 + 23 + 123 = 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:

There is only one way - 5 x 10 = 50.

Example #2
Input:

4 2
2 3 10 10

Output:

10 x 10 = 100.
.

Who is the winner


John and Mary are playing the game. There are n rows of boxes on a table.
Each box contains positive number of coins. John can take only one box
from the left end of any nonempty row in one move. Mary can take only one
box from the right end of any nonempty row in one move. Each player wants
to take the maximal number of coins and the winner is a player who collects
more coins, if they collect equal amount of coins it is draw. The game ends
when all rows are empty. Players make moves one after the other.
We know that John makes the first move and both players are playing
optimally. Determine the winner. If John wins, print "John" (without quotes),
if Mary wins, print "Mary" (without quotes), print "Draw" (without quotes) in
case of draw.

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

Explanation: John will take the following coins: 6, 2, 4. 6 + 2 + 4 = 12. Mary


will take the following coins: 2, 3, 7. 2 + 3 = 7 = 12. 12 = 12, 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:

 Stay in his place.


 Move one unit to the left (from x to x-1).
 Move one unit to the right (from x to x+1).

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.

The degree of friendship


John says that if we are given two integers, a and b, the degree of friendship
between a and b is the XOR of all numbers between a and b (inclusive). John
asked you to write a program which takes integer a and integer b as
arguments and prints the degree of friendship between them.

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

Explanation: xor of a single number is the number itself.

Sorted and rotated, minimal element


You are given a sorted and rotated array consisting of n integers. Print the
minimal element in it.

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

Explanation: The smallest element is 1.

Example#2
Input

5
25 5 10 15 20

Output

Explanation: The smallest element is 5.

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

Explanation: Same way as above example

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

Explanation: The first 3 terms of Golomb sequence: 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

Explanation: The first 20 terms of Golomb sequence: 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

Explanation: There is 100, 200 as subsequence and their sum is 300

Example#2
Input

5
5 1 4 2 3
100

Output

FALSE

Explanation: There is no subsequence such that their sum equals to 100


Cake Cutting
Given a cake with length N, each piece of cake has different price, determine
the maximum value obtainable by cutting up the cake and selling the pieces.

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

Explanation: We can cut it in two pieces of lengths 2 and 6 (5 + 17 = 22)

Example#2
Input

8
3 5 8 9 10 17 17 20

Output

24

Explanation: We can cut it in eight pieces of length 1

Length of LCS ( Longest Common Substring )


For given strings str1 and str2 find length of LCS ( Longest Common
Substring )

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

Explanation: "ef" is longest common substring, accordingly result will be 2


(length of "ef")

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

Length of LPS ( Longest Palindromic Subsequence )


For a given string S find the length of LPS ( Longest Palindromic
Subsequence ) in it.

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

Explanation: There is DEF_FED as subsequence of S which is palindromic


string with length of 7

Example#2
Input

ABCDEFGHI

Output

Explanation: There is no length of > 1 subsequence in S but each char itself


is palindrome so result in this case will be 1

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

Explanation: Same technique as above

Maximize the Sum of Products


For given N positive integers, you are able to remove element from either of the
two ends, each time you remove an element, score is increasing by value of
element * (number of elements removed + 1). You must find the maximum score
that can be obtained by removing all the elements.

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

umber of all subsequences having product less than M


For given array find the number of subsequences having product smaller
than M

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

Explanation: There are :


{1}, {2}, {3}, {4}
{1,2}, {1,3}, {1,4}
{2, 3}, {2, 4},
{1, 2, 3}, {1, 2, 4} subsequences
each of this have a product less than M

Example#2
Input

5
10 10 10 10 10
14

Output

Explanation: Same technique as above

Assign Socks to Drawers


We have N sock and N drawers placed in a straight line. Each drawer can
accommodate 1 sock. We can leave sock at its position, move one step right from i
to i + 1, or move one step left from i to i - 1. Each move consumes 1 minute. Assign
socks to drawers so that the time when last sock gets its position is minimized.

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

Explanation: First rearrange given array as 1, 8, 2, 4


sum of neighbor elements will be :
|1 - 8| + |8 - 2| + |2 - 4| + |4 - 1| = 18

Example#2
Input

3
10 12 15

Output

10

Explanation: |10 - 12| + |12 - 15| + |15 - 5| = 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

Explanation: Single 9 x 9 square

Groups of Size Two in Array


Given an array with length of N (where N is even), group 2 - 2 elements from
array such that difference between the group with highest sum and one with
lowest sum is minimum

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

Bus Station Problem


Given two array, arrival and departure times of all trains that reach a railway
station
where for example 940 is 09:40, 1120 is 11:20 ...
Find the minimum number of platforms required for the railway station so that
no train waits.

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

Nearly Sorted Array


For a given nearly sorted array, which means that some elements are moved to
either of the adjacent positions after sorting, write a function to search an element
K.

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

Explanation: Index of 15 in array is 2

Example#2
Input

5
2 1 4 3 9
9

Output

Explanation: 9 is last member of array with index 4

You might also like