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

Codevita

The document describes a maze navigation problem where the goal is to find the shortest path from a starting block to a target block in a maze represented by a grid of integers. Blocks with a value of 1 are obstacles that cannot be passed through, while blocks with a value of 2 or 3 have additional constraints on how many times they can be passed or must be avoided. The problem provides examples of mazes with solutions or where no path exists, and requires finding the shortest path length or outputting "STUCK" if no path is possible.

Uploaded by

Naveen Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Codevita

The document describes a maze navigation problem where the goal is to find the shortest path from a starting block to a target block in a maze represented by a grid of integers. Blocks with a value of 1 are obstacles that cannot be passed through, while blocks with a value of 2 or 3 have additional constraints on how many times they can be passed or must be avoided. The problem provides examples of mazes with solutions or where no path exists, and requires finding the shortest path length or outputting "STUCK" if no path is possible.

Uploaded by

Naveen Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1 best bubble

Best Bubble
Problem Description
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in the wrong order. The problem with bubble sort is its worst case scenario. When the smallest
element is in the last position, then it takes more time to sort in ascending order, but takes less time to sort
in descending order.

An array is called beautiful if all the elements of the array are in either ascending or descending order.
Given an array of numbers, find the minimum swap operations required to make the array beautiful.

Constraints
0 < N < 1000

0 < Arr[i] < 1000

Input
First line contains of integer N denoting number of elements in the array.

Second line consist of N integers separated by space denoting the elements of the array.

Output
Single integer denoting the least numbers of swap operations required to make the array beautiful.

Time Limit (secs)


1

Examples
Example 1

Input

45321

Output

Explanation

The number of swaps required to sort the elements in ascending order is 9.

The number of swaps required to sort the elements in descending order is 1.

The best way is to sort in descending order and swaps required is 1.


Example 2

Input

45123

Output

Explanation

Ascending order:

Pass/Index a b c d e Comparison Need swap

Pass 1 4 5 1 2 3 ab No

4 5 1 2 3 bc Yes

4 1 5 2 3 cd Yes

4 1 2 5 3 de Yes

Pass 2 4 1 2 3 5 ab Yes

1 4 2 3 5 bc Yes

1 2 4 3 5 cd Yes

1 2 3 4 5 de No

Descending order:
Pass/index a b c d e Comparison Need swap

Pass 1 4 5 1 2 3 ab Yes

5 4 1 2 3 bc No

5 4 1 2 3 cd Yes

5 4 2 1 3 de Yes

Pass 2 5 4 2 3 1 ab No

5 4 2 3 1 bc No

5 4 2 3 1 cd yes

5 4 3 2 1 de No

The number of swaps required to sort the elements in ascending order is 6.

The number of swaps required to sort the elements in descending order is 4.

The best way is to sort in descending order and swaps required is 4.


Question 2

Orchard
Problem Description
Orchards are a piece of enclosed land planted with different fruit trees in an orderly manner. The owners
will manage those trees and fulfill all the needs like pesticides, water, fertilizers for a better yielding.

Ashok and Anand are friends. On a fine day they went to an orchard where lemon and mango trees are
planted in rows. The owner planted these trees in rows but in random order. Some trees have plenty of
fruits and some other plants didn't give good yield. While they are walking through the rows, both of them
selected a row of trees. The trees in the row are represented by M and L which represents mango and
lemon respectively. After selecting the rows, they both argued for sometime over the number of fruits.
Then they saw Akhil walking towards them. They asked Akhil to declare which row holds more number of
fruits. Akhil understood that guessing the row with maximum number of fruits will be quite difficult.

So he asked to follow the below rules.

 Each time one has to select three trees from the row and form a set out of them, such that
no two adjacent trees in the set should be same.

 Once a tree is selected, they cannot walk back and select another tree.

 Trees need not to be adjacent for selection.

 Who ever have the more number of possibilities will be considered as winners.

Given two strings denoting the trees in the selected rows, find who is the winner. If the string is invalid,
print "Invalid input" and if no one wins, print "Draw".

Constraints
1 <= len(str) <= 10^4

Input
First line consists of the string denoting trees in Ashok's row.

Second line consists of the string denoting trees in Anand's row.

Output
Print the name of the winner in a single line.

Time Limit (secs)


1

Examples
Example 1

Input
MMLMLLM

LMLLLMLM

Output

Anand

Explanation

Ashok's possibilities are (1,3,4), (2,3,4), (3,4,6), (4,6,7), (1,3,7), (3,4,5), (1,5,7), (2,6,7), (2,3,7), (2,5,7),
(4,6,7), (1,6,7) ie., 12 possibilities.

Anand's possibilities are (2,3,6), (1,2,3), (1,6,7), (1,2,4), (3,6,7), (2,3,8), (1,2,5), (2,4,6), (4,6,7), (1,2,7),
(2,4,8), (5,6,7), (2,5,6), (2,7,8), (2,5,8), (6,7,8) ie., 16 possibilities. Hence he wins.

Example 2

Input

MLLM

LMLL

Output

Draw

Explanation

Ashok's possibilities are (1,3,4), (1,2,4) ie., 2 possibilities.

Anand's possibilities are (1,2,4), (1,2,3) ie., 2 possibilities.

So no one wins and we print draw.

Question 3.
MazeRunner
Problem Description
You are a brave adventurer who finds yourself in the middle of a mysterious maze. The maze is
represented by a set of integers viz. 0, 1, 2, and 3. Each element in the maze represents a block. You are
given the coordinates of the starting block and the target block in the maze, and your task is to reach the
target block such that you travel least distance in doing so.

As you explore the maze, you encounter several obstacles that block your path. Obstacles are represented
by block with a value of 1, and you must avoid these blocks at all costs. Additionally, the maze contains
blocks with a value of 2. In your travelled path from source to destination there cannot be more than two
blocks with value 2.

Furthermore, as you make your way through the maze, you notice that some blocks are marked with the
value 3. These blocks are extremely dangerous and must be avoided at all costs unless it is the only
possible way to reach the target block and you should cross such blocks as less as possible even if it leads
to a longer path. You cannot move diagonally or visit any blocks twice. Your starting point can be any
block.

Your task is to use your wits and navigate through the maze such that you travel the shortest distance from
the starting block to the target block without violating any of the rules mentioned above. If no such path
exists, you must print STUCK. Can you find the way out of the maze and reach the target block safely?

Constraints
2 <= R,C <= 15

2*2 <= RxC <= 15 *15 (assuming it has considerate amount of 1 2 and 3).

Left Top represents 0 0 and Right Bottom represents R C.

Input
The first line contains the number of rows (R) and columns (C) separated by spaces.

Next next R lines, each containing C space seperated integers represent the maze.

The next line contains the coordinates of the starting block.

The last line contains coordinates of the target block.

Output
Print an integer representing the length of the shortest path traveled between the starting and the target
block. If no shortest path found print STUCK.

Time Limit (secs)


1

Examples
Example 1
Input

33

030

002

100

00

02

Output

Explaination

Consider the following diagram:

You start from the block (0,0). Now, you have two options: either you can take the path having block (0,1)
or the block (1,0). Since, an alternate path is available you cannot take path involving block containing
number 3. Therefore, you take the path with block with number 0. Then, similarly, you take the block (1,1)
with number 0 and the block (1,2) with number 2. You can take at most 2 blocks of such number. Finally,
you reach the target (0,3). So, the total distance covered is 4.

Therefore, the shortest path would be (0,0) -> (1,0) -> (1,1) -> (1,2) -> (0,2) as shown below

Example2

Input

33

010

032

120
00

22

Output

Explanation

Consider the following diagram:

You start from block (0, 0). Then, you move to (1, 0) as that's the only possible route from the start point.
From (1, 0), (2, 0) is blocked as it has a value of 1. Therefore, the only option is to choose (1, 1) with value
3. Now, from (1, 1), you can take the route of (1, 2) or (2, 1), but as both of them will give the same
shortest distance, you can choose either of them. So, the total distance covered is 4.

Therefore, the shortest path would be (0,0) -> (1,0) -> (1,2) -> (2,1) -> (2,2) as shown below:

Example 3

Input

33

010

031

010

00

22

Output

STUCK

Explaination
Consider the following diagram:

If you take account of the previous example its the same but in block (1,
2) or (2, 1) you have obstacle with number 1 and there is no other way you can reach the target therefore
you STUCK.

So there will be no path from start to target as shown below:

Example 4

Input

34

0100

0330

0300

00

03

Output

Explanation

Consider the following diagram:


You start from block (0,0) then (0,1) is 1 so it's blocked then block (1,0) is the only possible path then from
(1,0) you take both the block (1,1) and (2,0). So the possible route will be (0,0) -> (1,0) -> (1,1) -> (1,2) -
>(1,3) -> (0,3) or (0,0) -> (1,0) -> (1,1) -> (1,2) -> (0,2) -> (0,3) but here you will notice that we have
crossed blocks with 3 two times but according to the rules we must avoid 3 at all cost unless necessary. So
the only possible route is (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2) -> (2,3) -> (1,3) -> (0,3) where we cross
only one block with 3. Therefore, the total distance is 7.
The path would be as shown below:

Question 4

Perfect Budget
Problem Description
Shasank is a government civil engineer, who constructs the infrastructure like roads, bridges, water
resources etc. Once he receive the appropriate budget and the list of constructions(or projects), he will start
them one after the other.

These projects are based on different regions and he has to move from one site to another site along with
the resources. These sites have their own rules and by the end day of the project, the higher officials in that
site will give/ take an amount towards reward / penalty for early / late completion.

For every project, given

Expenditure - the expenditure for the given project

Completion bonus - the amount received as gratuity after the project is completed.

Reward / Penalty - this is an amount that is either given/taken to/from Shasank according to the site
norms. The '+' sign indicates reward, else it is considered as penalty for being late.

A budget is called perfect budget if within that amount, he can complete all the projects in every possible
order. Help Shasank in calculating what is the minimum perfect budget for the given list of projects.

Note: The site officials will not charge penalty which is greater than the bonus for that project ie., bonus -
penalty >= 0 for all projects.

Constraints
1 <= n <= 1000

0 <= expenditure, bonus <= 10^4

1 <= reward <= 10^4

-10^4 <= penalty <= -1

Input
First line contains n denoting the number of projects.

Next n lines will contain expenditure, completion bonus, reward / penalty separated by space.

Output
Print the minimum budget needed to complete all the projects in every possible order.

Time Limit (secs)


1

Examples
Example 1

Input

3 9 +4
0 4 -2

7 10 -1

4 2 +1

Output

Explanation

Initial Budget An order which is not possible within the given initial budget

2 P1, P2, P3, P4

3 P4, P3, P2, P1

5 P3, P4, P2, P1

7 P4, P3, P2, P1

8 All orders are possible

Thus we can complete the given projects in every possible way with a budget of 8, hence print 8.

Example 2

Input

13 9 +6

7 3 -3

9 6 +11

Output

20

Explanation
Initial Budget An order which is not possible within the given initial budget

4 P3, P2, P1

9 P1, P2, P3

12 P1, P2, P3

15 P2, P1, P3

17 P2, P1, P3

20 All orders are possible

Thus we can complete the given projects in every possible way with a budget of 20, hence print 20.

Question 5

Vinni - The Craftsman


Problem Description
Vinni is a craftsman. He makes different types of toys using different materials and sells them in the
market. One day he ran out of raw materials, so he went to the market with N rupees.
There he found M types of materials with which he can make toys. Each type of material has different
costs and thus the selling price of toys of each type will also vary from one another.

Given,

N - total amount of money with Vinni (in rupees)

M - total types of materials available in the market.

quantity_available - an array denoting the available quantity of each type where i th integer denotes the
available quantity of ith type material.

quantity_needed - an array denoting the quantity needed for making the toy of that type where i th integer
denotes the quantity of ith material needed to make toy.

cost_of_one_unit - an array denoting the cost of one unit of material where i th integer denotes cost of one
unit of ith type material.

selling_price - an array denoting the selling price of toys where i th integer denotes the price of toy made up
of ith material.

Help Vinni in choosing the raw materials to avail maximum amount!

Note: Every toy he is going to make will be guaranteed to be sold in the market.

Constraints
1<= N <= 10^4

1<= M <= 10^3

1 <= quantity_available[i] <= 10^3

1 <= quantity_needed[i] <= 10^3

1 <= cost_of_one_unit [i] <= 10^3

1 <= selling_price [i] <= 10^3

It is not guaranteed that initially there will be enough raw material in the market to make a toy of a given
type.

Input
First line consists of N and M separated by space.

Next four lines consists of four array viz. quantity_available, quantity_needed, cost_of_one_unit,
selling_price.

Output
Print the maximum amount Vinni can earn.

Time Limit (secs)


1
Examples
Example 1

Input

21 3

11 12 13

345

212

20 15 12

Output

70

Explanation

Quantit Cost of
Quantity Selling price Total
y needed one unit
available of quantity
to make of raw Cost of selected raw materials
in the manufactured selected
Type one toy material
shop toy (in rupees)
(in
(in (in
(in units) (in rupees) units)
units) rupees)

Type
11 3 2 20 6 2*6=12
1

Type
12 4 1 15 8 1*8=8
2

Type
13 5 2 12 0 0
3

From the above table, it is clear that Vinni can earn a maximum of 70 rupees. There is no other
combination which can earn more than 70 rupees.

Example 2

Input
82

10 6

12

21

25 30

Output

115

Explanation

Quantit Cost of Cost of


Quantity Selling price Total
y needed one unit selected
available of quantity
to make of raw raw Amount earned by Vinni selling toys
in the manufactured selected
Type toy material materials
shop toy (in rupees)
(in
(in (in (in
(in units) (in rupees) units)
units) rupees) rupees)

Type
10 1 2 25 1 2*1=2 1*25=25
1

Type
6 2 1 30 6 1*6=6 3*30=90
2

From the above table, it is clear that Vinni can earn a maximum of 115 rupees. There is no other
combination which can earn more than 115 rupees.

Wuestion 6

Bouncing Balls
Problem Description
James is playing a shooting game called "Bouncing Balls in the Box." In this game, he needs to shoot all
the balls that are moving inside a big rectangular box to win.

James has successfully shot all but two balls, and he has only one bullet left. Luckily, these two remaining
balls overlap each other at some point in their movement trajectory. If he aims at the right moment during
their overlap, he can eliminate both balls with a single shot.
The game is set in a big rectangular box with dimensions MxN, which is divided into MxN cells. Inside
this box, two balls are the targets.

The balls move from one cell to their neighboring cell one step at a time. Each ball maintains its respective
direction until it hits the wall of the big box, at which point it changes direction due to the impact with the
wall. James needs to time his shot to hit both balls while they overlap for the perfect shot.

These directions describe how the target box moves within the rectangular box, starting from the initial
position [3, 3] and progressing through subsequent cells based on the specified direction. When the target
boxes are moving in their corresponding directions, they will eventually hit the wall of the rectangular box
and bounce back according to the laws of reflection. Here's how the the path and the bouncing behavior
would look for each direction from the position [3,3]:

1. Direction: (-1, -1)

 Initially: [3, 3] -> [2, 2] -> [1, 1]

 Bounce (due to hitting the wall): [1, 1] -> [2, 2] -> [3, 3] -> ...

2. Direction: (0, -1)

 Initially: [3, 3] -> [3, 2] -> [3, 1]


 Bounce (due to hitting the wall): [3, 1] -> [3, 2] -> [3, 3] -> ...

3. Direction: (1, -1)

 Initially: [3, 3] -> [4, 2] -> [5, 1]

 Bounce (due to hitting the wall): [5, 1] -> [4, 2] -> [3, 3] -> ...

4. Direction: (-1, 0)

 Initially: [3, 3] -> [2, 3] -> [1, 3]

 Bounce (due to hitting the wall): [1, 3] -> [2, 3] -> [3, 3] -> ...

5. Direction: (0, 0)

 Initially: [3, 3] -> [3, 3] -> [3, 3] (No change in position as the direction is [0, 0])

6. Direction: (1, 0)

 Initially: [3, 3] -> [4, 3] -> [5, 3]

 Bounce (due to hitting the wall): [5, 3] -> [4, 3] -> [3, 3] -> ...

7. Direction: (-1, 1)

 Initially: [3, 3] -> [2, 4] -> [1, 5]

 Bounce (due to hitting the wall): [1, 5] -> [2, 4] -> [3, 3] -> ...

8. Direction: (0, 1)

 Initially: [3, 3] -> [3, 4] -> [3, 5]

 Bounce (due to hitting the wall): [3, 5] -> [3, 4] -> [3, 3] -> ...

9. Direction: (1, 1)

 Initially: [3, 3] -> [4, 4] -> [5, 5]

 Bounce (due to hitting the wall): [5, 5] -> [4, 4] -> [3, 3] -> ...

Suppose if the ball is in the position [4,3] in the above grid and is moving in the direction (1,1) then the
initial path will be [4,3] -> [5,4] and then it had hit the wall, so the reflection will be along the cells, [4,5] -
> [3,4] -> [2,3] ...

Now that you know how the balls moves inside the rectangular box, predict when the two target balls will
overlap or will be in the same cell, and help James in shooting.

Note:

 James is a good shooter and will never miss the hitting, all he needs is to have both the
balls in the same cell.

 Balls will never stop moving until they are shot.

Constraints
0 < M, N < 1000

Input
The first line consists of two space-separated integers representing the total number of rows and columns,
MxN of the box.

The second line consists of two space-separated integers representing the position of the first target ball.

The third line consists of two space-separated integers representing the direction in which the first target
ball is moving. Each integer can be either -1, 0, or 1.

The fourth line consists of two space-separated integers representing the position of the second target ball.

The fifth line consists of two space-separated integers representing the direction in which the second target
ball is moving. Each integer can be either -1, 0, or 1.

Output
A Single integer that represents the minimum number of steps after which both the balls overlap. Print
"Never" if they will never overlap.

Time Limit (secs)


1

Examples
Example 1

Input

55

42

1 -1

44

11

Output

Explanation

Target ball 1 is in [4,2] and the path traced was [5,1],[4,2],[3,3]

Target ball 2 is in [4,4] and the path traced was [5,5],[4,4],[3,3]

At the third step both balls will overlap i.e. will be in the same cell. Hence, print 3.

Example 2

Input
10 10

22

11

66

11

Output

Explanation

Target ball 1 :[ 2, 2],[ 3, 3],[ 4, 4],[ 5, 5],[ 6, 6],[ 7, 7],[ 8, 8]

Target ball 2 :[ 6, 6],[ 7, 7],[ 8, 8],[ 9, 9],[10,10],[ 9, 9],[ 8, 8]

At the sixth step both balls will overlap i.e. will be in the same cell. Hence, print 6.

You might also like