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

Challenge

This document contains details about 5 coding questions: 1. A question about determining if two permutations of length n satisfy given prefix and suffix length conditions. 2. A question about arranging numbers to maximize the minimum absolute difference between consecutive numbers. 3. A question about finding the maximum number of operations of erasing elements from a cyclic sequence before it becomes empty. 4. A question about finding the number of leaves remaining uneaten after caterpillars of different jump lengths have traveled from leaf 1 to leaf N. 5. A question about determining the last color (red or blue) used to paint stripes on an 8x8 grid.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Challenge

This document contains details about 5 coding questions: 1. A question about determining if two permutations of length n satisfy given prefix and suffix length conditions. 2. A question about arranging numbers to maximize the minimum absolute difference between consecutive numbers. 3. A question about finding the maximum number of operations of erasing elements from a cyclic sequence before it becomes empty. 4. A question about finding the number of leaves remaining uneaten after caterpillars of different jump lengths have traveled from leaf 1 to leaf N. 5. A question about determining the last color (red or blue) used to paint stripes on an 8x8 grid.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Coding Round for CSE04 Coders

Source : codeforces.com
Author : Unknown

Question 1 : Two Permutations

You are given three integers n , a , and b. Determine if there exist two permutations p and q of
length n, for which the following conditions hold:

 The length of the longest common prefix of p and q is a.


 The length of the longest common suffix of p and q is b.
A permutation of length n is an array containing each integer from 1 to n exactly once. For
example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the
array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).

Example :
Input Format : n , a , b
Output Format : Yes/No
Input :
1 1 1
2 1 2
3 1 1
4 1 1

Output :
Yes
No
No
Yes

Note
In the first test case, [1] and [1] form a valid pair.

In the second test case and the third case, we can show that such a pair of permutations doesn't
exist.
In the fourth test case, [1,2,3,4]and [1,3,2,4] form a valid pair.

Question 2 : Kevin and Permutaion

For his birthday, Kevin received the set of pairwise distinct numbers 1,2,3,…,n as a gift.
He is going to arrange these numbers in a way such that the minimum absolute difference
between two consecutive numbers be maximum possible. More formally, if he arranges numbers
in order p1,p2,…,pn he wants to maximize the value
min|pi+1−pi| for all I from 1 to n-1
where |x| denotes the absolute value of x.

Help Kevin to do that.

Example :

Input Format : n

Output Format : Output a permutation of length n as answer

Input :

2
4
3

Output :

2 4 1 3
1 2 3

Note
In the first test case the minimum absolute difference of consecutive elements equals min{|
4−2|,|1−4|,|3−1|}=min{2,3,2}=2. It's easy to prove that this answer is optimal.
In the second test case each permutation of numbers 1,2,3 is an optimal answer. The minimum
absolute difference of consecutive elements equals to 11.

Question 3 : Elimination of a ring

Define a cyclic sequence of size n as an array s of length n, in which sn is adjacent to s1.

Muxii has a ring represented by a cyclic sequence a of size n.

However, the ring itself hates equal adjacent elements. So if two adjacent elements in the
sequence are equal at any time, one of them will be erased immediately. The sequence
doesn't contain equal adjacent elements initially.
Muxii can perform the following operation until the sequence becomes empty:
 Choose an element in a and erase it.
For example, if ring is [1,2,4,2,3,2], and Muxii erases element 4, then ring would erase one of
the elements equal to 2, and the ring will become [1,2,3,2].

Muxii wants to find the maximum number of operations he could perform.


Note that in a ring of size 1, its only element isn't considered adjacent to itself (so it's not
immediately erased).

Input :

Input Format : First line contains an integer n – the size of array

The next line contains n integers denoting the elements of array

Output Format : Output an integer x – the answer to the problem

4
1 2 3 2
4
1 2 1 2
1
1

Output :

4
3

Note
In the first test case, you can erase the second element first, then erase the remaining elements
one by one in any order. In total, you can perform the operation 4 times. Note that if you erase
the first element first, then the sequence will be turned into [2,3,2] and then immediately
become [2,3].
In the second test case, you can erase the first element first, then the sequence becomes [2,1].
Then you can erase all remaining elements one by one in any order.

Question 4 : Jumping Caterpillars

Given N leaves numbered from 1 to N . A caterpillar at leaf 1, jumps from leaf to


leaf in multiples of Aj (Aj, 2Aj, 3Aj).
j is specific to the caterpillar. Whenever a caterpillar reaches a leaf, it eats it a little
bit.. You have to find out how many leaves, from 1 to N, are left uneaten after
all K caterpillars have reached the end. Each caterpillar has its own jump factor
denoted by Aj, and each caterpillar starts at leaf number 1.

Input:
N=10 K=3
arr[] = {2, 3, 5}
Output: 2
Explanation: The leaves eaten by the first
caterpillar are (2, 4, 6, 8, 10). The leaves
eaten by the second caterpilllar are (3, 6, 9).
The leaves eaten by the third caterpilllar
are (5, 10). Ultimately, the uneaten leaves are
1, 7 and their number is 2.

Question 5 : Stripes

On an 8×8 grid, some horizontal rows have been painted red, and some vertical columns have
been painted blue, in some order. The stripes are drawn sequentially, one after the other. When
the stripe is drawn, it repaints all the cells through which it passes.

Determine which color was used last.

Input :

Input Format : 8 x 8 cross grid

Output Format : Output ‘R’ or ‘B’ – the answer to the problem

4
....B...
....B...
....B...
RRRRRRRR
....B...
....B...
....B...
....B...

RRRRRRRB
B......B
B......B
B......B
B......B
B......B
B......B
RRRRRRRB

RRRRRRBB
.B.B..BB
RRRRRRBB
.B.B..BB
.B.B..BB
RRRRRRBB
.B.B..BB
.B.B..BB

........
........
........
RRRRRRRR
........
........
........
........

Output :

R
B
B
R

Note
The first test case is pictured in the statement.

In the second test case, the first blue column is painted first, then the first and last red rows, and
finally the last blue column. Since a blue stripe is painted last, the answer is B.

You might also like