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

Practice Sheet 2 - Circular Array

Uploaded by

al.faridul.karim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Practice Sheet 2 - Circular Array

Uploaded by

al.faridul.karim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Circular Array

Question 1

Given a circular array A of N elements(the next element of the last element is the first element of the
array), print the Next Greater Number for every element. The Next Greater Number of a number x is
the first greater number to its traversing-order next in the array, which means you could search
circularly to find its next greater number. If it doesn't exist, output -1 for this number.

Input

The first line contains N, the following line contains N space separated integers.

Output

Print the next greater number of each element of the array (space separated).

Sample Input
3
1 2 1

Sample Output
2 -1 2

Explanation
The first 1's next greater number is 2; The number 2 can't find next greater number; The second 1's
next greater number needs to search circularly, which is also 2.

Question 2
Ninja has a circular array ‘Nums’ containing ‘N’ positive integers. An array is called circular if we
consider the first element as next of the last element.

Ninja wants you to find the first greater number to the right of each element in the array, if there
is no greater element to the right of an element, then output -1 for this element.

Example :
If N = 5 and the array is: [ 1, 6, 4, 3, 5 ]

We will return [ 6, -1, 5, 5, 6 ]


because 6 is the first element to the right of 1 that is greater than 1,
no element exists that is greater than 6,
5 is the first element to the right of 4 that is greater than 4,
5 is the first element to the right of 3 that is greater than 3,
6 is the first element to the circular-right of 5 that is greater than 5.
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case
follows:

The first line of each test case contains a single integer ‘N’ denoting the size of the array.

The second line of each test case contains N positive integers denoting the array elements
‘Nums[i]’.
Output Format:
For each test case, print the next greater elements for each element in the circular array.

Output for each test case will be printed in a separate line.

Question 3

Given an array of n elements. Consider array as circular array i.e element after an is a1. The
task is to 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|.

Examples:

Input : arr = [4, 2, 1, 8]


Output : 18
Rearrange given array as : [1, 8, 2, 4]
Sum of difference between consecutive element
= |1 - 8| + |8 - 2| + |2 - 4| + |4 - 1|
=7+6+2+3
= 18.

Input : arr = [10, 12, 15]


Output : 10

Question 4

Given a circular array arr[] of length N, the task is to find the minimum absolute difference
between any adjacent pair. If there are many optimum solutions, output any of them.

Examples:

Input: arr = [10, 12, 13, 15, 10]


Output: 0
Explanation: |10 – 10| = 0 is the minimum possible difference.
Input: arr = [10, 20, 30, 40]
Output: 10
Explanation: |10 – 20| = 10 is the minimum, 20 30 or 30 40 could be the answer too.

Question 5

You are playing another computer game, and now you have to slay n monsters. These monsters
are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has ai health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases
the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of
some monster i becomes 0 or less than 0, it dies and explodes, dealing bi damage to the next
monster (monster i+1, if i<n, or monster 1, if i=n). If the next monster is already dead, then
nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster
after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the
circle.

Input
The first line contains one integer T (1≤T≤150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n
(2≤n≤300000) — the number of monsters. Then n lines follow, each containing two integers ai
and bi (1≤ai, bi≤1012) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all
of the monsters.

Example
input
1
3
7 15
2 14
53
output
6
Question 6
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are n players
arranged in a circle, so that for all j such that 2≤j≤n, player j−1 is to the left of the player j, and
player j is to the right of player j−1. Additionally, player n is to the left of player 1, and player 1 is
to the right of player n.

Currently, each player is attacking either the player to their left or the player to their right. This
means that each player is currently being attacked by either 0, 1, or 2 other players. A key
element of Bed Wars strategy is that if a player is being attacked by exactly 1 other player, then
they should logically attack that player in response. If instead a player is being attacked by 0 or
2 other players, then Bed Wars strategy says that the player can logically attack either of the
adjacent players.

Unfortunately, it might be that some players in this game are not following Bed Wars strategy
correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any
amount of the n players in the game to make them instead attack another player — i. e. if they
are currently attacking the player to their left, Omkar can convince them to instead attack the
player to their right; if they are currently attacking the player to their right, Omkar can convince
them to instead attack the player to their left.

Omkar would like all players to be acting logically. Calculate the minimum amount of players that
Omkar needs to talk to so that after all players he talked to (if any) have changed which player
they are attacking, all players are acting logically according to Bed Wars strategy.

Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1≤t≤104). The descriptions of the test cases follows.

The first line of each test case contains one integer n (3≤n≤2⋅105) — the amount of players
(and therefore beds) in this game of Bed Wars.

The second line of each test case contains a string s of length n. The j-th character of s is equal
to L if the j-th player is attacking the player to their left, and R if the j-th player is attacking the
player to their right.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output one integer: the minimum number of players Omkar needs to talk to
to make it so that all players are acting logically according to Bed Wars strategy.

It can be proven that it is always possible for Omkar to achieve this under the given constraints.

Example
input
5
4
RLRL
6
LRRRRL
8
RLLRRRLL
12
LLLLRRLRRRLL
5
RRRRR
output
0
1
1
3
2
Note
In the first test case, players 1 and 2 are attacking each other, and players 3 and 4 are attacking
each other. Each player is being attacked by exactly 1 other player, and each player is attacking
the player that is attacking them, so all players are already being logical according to Bed Wars
strategy and Omkar does not need to talk to any of them, making the answer 0.

In the second test case, not every player acts logically: for example, player 3 is attacked only by
player 2, but doesn't attack him in response. Omkar can talk to player 3 to convert the attack
arrangement to LRLRRL, in which you can see that all players are being logical according to
Bed Wars strategy, making the answer 1.

Question 7

The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order
of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each
figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger
and so on). The type of animal of the i-th figure equals ti.

The example of the carousel for n=9 and t=[5,5,1,15,1,5,5,1,1].


You want to color each figure in one of the colors. You think that it's boring if the carousel
contains two different figures (with the distinct types of animals) going one right after another
and colored in the same color.

Your task is to color the figures in such a way that the number of distinct colors used is the
minimum possible and there are no figures of the different types going one right after another
and colored in the same color. If you use exactly k distinct colors, then the colors of figures
should be denoted with integers from 1 to k.

Input
The input contains one or more test cases.

The first line contains one integer q (1≤q≤104) — the number of test cases in the test. Then q
test cases follow. One test case is given on two lines.

The first line of the test case contains one integer n (3≤n≤2⋅105) — the number of figures in the
carousel. Figures are numbered from 1 to n in order of carousel moving. Assume that after the
n-th figure the figure 1 goes.

The second line of the test case contains n integers t1,t2,…,tn (1≤ti≤2⋅105), where ti is the type
of the animal of the i-th figure.

The sum of n over all test cases does not exceed 2⋅105.

Output
Print q answers, for each test case print two lines.

In the first line print one integer k — the minimum possible number of distinct colors of figures.

In the second line print n integers c1,c2,…,cn (1≤ci≤k), where ci is the color of the i-th figure. If
there are several answers, you can print any.

Example
input
4
5
12122
6
122122
5
12123
3
10 10 10
output
2
12122
2
212121
3
23231
1
111

Reference:
1. https://www.geeksforgeeks.org/top-50-array-coding-problems-for-interviews/
2. https://www.csinfo360.com/p/array-practice-problems.html
3. https://www.hackerearth.com/problem/algorithm/circular-nge/
4. https://www.codingninjas.com/codestudio/problem-details/ninja-s-circular-array_2221409
5. https://leetcode.com/problems/circular-array-loop/ (Special problem. Interesting but can
not provide it due to graph knowledge limitation)
6. https://codeforces.com/contest/1334/problem/C
7. https://codeforces.com/contest/1392/problem/D
8. https://codeforces.com/contest/1328/problem/D
9. https://leetcode.com/problems/median-of-two-sorted-arrays/

You might also like