Problem Solving Assignment
Problem Solving Assignment
Solution:
We wish to build a mansion beside a long road. The far side of the road is filled with n
houses, each containing a given number of people. Our mansion is as long as w houses
combined. The goal is to position the mansion so that the number of people across from it is
maximized.
Input
• The first line contains two integers, n (the number of houses) and w (the width of the
mansion).
• The next n lines contain the number of people living in each house.
Output
The output is written to a file named manout.txt, which must contain a single integer
representing the largest possible number of people living across from the mansion.
Approach
To solve this problem, we use the sliding window technique, which allows us to find the
maximum sum of w consecutive elements in the array of people counts efficiently.
Steps
Explanation
Complexity Analysis
Example
Input (manin.txt):
74
Output :
12
Conclusion
This approach efficiently finds the maximum number of people living across from a mansion
of width w by leveraging the sliding window technique, ensuring a linear runtime suitable for
large values of n up to 100,000.
Question 2:
We are tasked with organizing a dinner for an international conference. Multiple countries are
represented, each with a certain number of delegates, and there are several restaurants in the
area, each with a different number of available seats. The goal is to seat as many delegates as
possible while ensuring no two delegates from the same country are seated at the same
restaurant.
Input
Output
The output should be written to a file named restout.txt, which must contain a single
integer representing the smallest number of delegates who cannot be seated.
Approach
1. Sort the delegates and restaurant capacities in descending order to maximize the
number of seated delegates.
2. Iteratively seat the delegates while ensuring no two delegates from the same country
sit in the same restaurant.
Steps
Explanation
• Sorting: Sorting delegates and restaurant seats in descending order ensures that the
largest groups are seated in the restaurants with the most capacity.
• Seating Logic: A priority queue (max-heap) is used to seat delegates while ensuring
no two delegates from the same country sit together in one restaurant.
• Complexity:
o Time Complexity: O((C+R)log(C+R))O((C + R) \log (C +
R))O((C+R)log(C+R)), where CCC is the number of countries and RRR is the
number of restaurants.
o Space Complexity: O(C+R)O(C + R)O(C+R), for storing the number of
delegates and seats.
Example
Input (restin.txt):
34333523
Output : 2
Conclusion
This approach efficiently arranges delegates to maximize seating while complying with the
rule that no two delegates from the same country are seated at the same restaurant. The
method is suitable for up to 5000 countries and restaurants.
Question : 3
You are navigating through a desert using a map represented as an (x, y) coordinate plane.
Starting at the origin (0, 0) and facing north, you follow a sequence of directional instructions
composed of:
However, one R (right turn) is missing in the sequence. Given the coordinates of the
destination (an oasis), you must determine where to insert the missing R to correctly reach the
oasis.
Input:
Output:
Approach
To solve this problem, we simulate the movement described by the sequence of directions:
Python Implementation
Explanation
Complexity Analysis
Example
Given:
• Directions: RFFLFLFFRFFL
• Oasis coordinates: (3, 3)
Output:
Conclusion
This approach effectively determines the correct position to insert a missing R in the direction
sequence to reach the specified destination on a coordinate plane
Question 4:
We have a maze where a token, represented by 'X,' is positioned on a white square within a
grid of shaded (gray) and unshaded (white) squares. The token can move either horizontally
or vertically for any number of white squares but is restricted by shaded squares, as it cannot
pass over or stop on them.
The objective is to find the minimum number of moves needed to ensure the token can
reach any other white square.
Steps to Solve:
Solution Outcome:
After going through possible paths and move calculations, the minimum number of moves
required for the token to be able to reach any white square is 10 moves.
Answer:
1. Rule (i): If three consecutive digits are palindromic (first digit = third digit), we can
remove these three digits.
2. Rule (ii): Any digit, except 9, can be increased by 1. This operation can help us form
palindromic triplets for removal according to Rule (i).
The goal is to determine the least number of times Rule (ii) must be applied to remove all
digits from the sequence.
Step-by-Step Solution
Answer:
Question 6:
Step-by-Step Solution
Conclusion
Question 7:
The puzzle involves a game where two players eat squares from a block of chocolate in
alternate turns. To take a bite, a player must choose a square and eat every square above it
and/or to the right (including the chosen square). The goal is to eventually force the opponent
to eat a square that both players dislike (in this case, square 1).
Problem Breakdown
In the given 3×3 chocolate block scenario, the players take turns. The goal is to make
strategic moves to ensure the friend ends up eating square 1. You are provided five options to
start with:
• (A) Square 5
• (B) Square 6
• (C) Square 9
• (D) Square 10
• (E) Square 11
The task is to select the square that allows you to ultimately force the opponent to eat square
1.
Strategy Explanation
To solve this, let's analyze each option based on how it impacts the possible moves for both
players, focusing on the goal of forcing the friend to eat square 1.
1. Square 5:
If you choose square 5, all squares to the right of it (squares 6, 9, and 10) will be
consumed. This leaves squares 7, 8, and 11 for subsequent moves, which gives your
friend significant freedom in choosing subsequent moves. This option does not
guarantee you can force your friend to end up eating square 1.
2. Square 6:
Choosing square 6 means consuming square 6 and all squares above and to the right
of it (squares 9 and 10). Your friend is left with squares 5, 7, 8, and 11. This move
similarly leaves options open for your friend, making it challenging to force them to
eat square 1.
3. Square 9:
Selecting square 9 will eat up square 9 and squares to the right of it (including square
10). This leaves your friend with the possibility to choose among squares 5, 6, 7, and
8, which can still lead to different outcomes and does not guarantee forcing the friend
to eat square 1.
4. Square 10:
By choosing square 10, you consume square 10 and all the squares to its right (square
11). This move leaves the 2x2 grid comprising squares 5, 6, 7, and 8. Depending on
your friend's move, this configuration allows you to steer the game towards a win
since you can potentially guide their moves to end at square 1.
5. Square 11:
Choosing square 11 results in eating square 11 and squares to the right of it (none in
this case). This option leaves a larger portion of the grid for your friend to work with,
offering minimal control over the game's outcome.
Optimal Move
The correct choice is (D) Square 10. This move effectively reduces the chocolate block to a
manageable size, giving you control over your friend's subsequent moves and allowing you to
force your friend to eat square 1 eventually.
Conclusion
The optimal strategy to ensure that your friend ends up eating square 1 is to choose square
10. This move strategically reduces the available options and provides you with the ability to
control the game's progression.
Question 8:
A pizza delivery boy is tasked with delivering pizzas to 11 houses lined up in a row, one
pizza per house. He follows a series of instructions to navigate between the houses:
• "Go to Mel's house, then go F3, B1, F6, B3, F1, B5, F1, F5, F1, B3,"
o F denotes moving forward a specified number of houses.
o B denotes moving backward a specified number of houses.
One of these instructions has been written incorrectly. The task is to identify where the
mistake lies.
• The goal is to deliver pizzas to all 11 houses, but by following these instructions, we
can see that:
o The instructions cause repeated visits to certain positions (e.g., positions 5 and
8).
o Not all positions from 1 to 11 are visited.
• The issue appears to arise around the middle of the instructions, where movements
cause revisiting previously visited houses rather than covering new ones.
• Critical Instructions to Check:
o 5th and 6th instructions: The move sequence F1 (moving to position 6)
followed by B5 (moving back to position 1) seems suspect. This backward
move covers a large distance, and subsequent instructions do not adequately
cover all the houses.
Conclusion
The mistake is likely in (C) the 5th or 6th instruction. This part of the sequence creates an
inefficiency and leads to repeated house visits instead of ensuring a systematic coverage of all
11 houses.
Question 9:
The problem involves classifying spiders as boys or girls based on their sizes. The objective
is to find a threshold value xxx (an odd number) such that all spiders with sizes larger than
xxx are considered girls, and all spiders smaller than xxx are considered boys. The aim is to
minimize the total number of misclassified spiders (i.e., girl spiders below or equal to xxx
and boy spiders above xxx).
Scenario 1
1. Possible values for xxx (odd numbers): 15, 17, 19, 21, 23, 25, 27, 29, 31
2. Evaluation:
o x=15x = 15x=15: Boy spiders misclassified = 0; Girl spiders misclassified = 1
(16). Total errors = 1.
o x=17x = 17x=17: Boy spiders misclassified = 1 (18); Girl spiders
misclassified = 1 (16). Total errors = 2.
o x=19x = 19x=19: Boy spiders misclassified = 2 (18, 22); Girl spiders
misclassified = 2 (16, 20). Total errors = 4.
o x=21x = 21x=21 and above: Misclassification count increases further.
1. Possible values for xxx (odd numbers): 15, 17, 19, 21, 23, 25, 27
2. Evaluation:
o x=15x = 15x=15: Boy spiders misclassified = 0; Girl spiders misclassified = 1
(16). Total errors = 1.
o x=17x = 17x=17: Boy spiders misclassified = 1 (18); Girl spiders
misclassified = 1 (16). Total errors = 2.
o x=19x = 19x=19: Boy spiders misclassified = 2 (18, 22); Girl spiders
misclassified = 3 (16, 20, 20). Total errors = 5.
Scenario 3
• Sizes of boy spiders: 16, 18, 20, 22, 26, 28, 30, 32, 34, 40, 42
• Sizes of girl spiders: 24, 26, 30, 36, 38, 42, 44, 46, 48, 50
1. Possible values for xxx (odd numbers): 23, 25, 27, 29, 31, 33, 35, 37, 39, 41
2. Evaluation:
o x=23x = 23x=23: Boy spiders misclassified = 0; Girl spiders misclassified = 1
(24). Total errors = 1.
o x=25x = 25x=25: Boy spiders misclassified = 1 (26); Girl spiders
misclassified = 1 (24). Total errors = 2.
o x=27x = 27x=27: Boy spiders misclassified = 2 (26, 28); Girl spiders
misclassified = 2 (24, 26). Total errors = 4.
Conclusion
4. Sequence 560560560
Conclusion
• Number of times Rule (ii) was applied: 2 times (incrementing 222 to 333 and
incrementing 555 to 666).
Final Answer
(B) 2