Problem 1: The Cake’s Walk Pre-requisite: Simple Math
Link to the problem:
https://www.hackerearth.com/prayatnaopc/algorithm/the-cakes-walk-3/
Problem statement in a nut shell:
Choose two numbers <= 100 such that pow(a,b) + pow(b,a) = x and a-b is maximum and x<=100
My Solution:
The solution to this problem is based on the simple observation that “1” raised to any power is “1” itself and any number raised to “1” is the number itself. So we can always choose the number ‘a’ as ‘x-1’ and the number ‘b’ as 1 such that the above equation will be satisfied. The only corner case to this exception is x=1. When x=1, the answer is a=1 and b=0.
Problem 2:
Pre-requisite: Simple Math
Link to the problem:
https://www.hackerearth.com/prayatnaopc/algorithm/handsome-boy-and-samosas-11/
Problem statement in a nut shell:
Given a rectangle of length ‘l’ and breadth ‘b’ such that abs(l-b)<=2, find the maximum area of the square of which this rectangle can be broken into without any wastage of area.
My solution:
It has been clearly mentioned in the problem that abs(l-b) <=2. The above statement simplifies a lot of stuff for us. One important point to note here is, the answer to this problem can only be 1 or 2 or l or b. Let us break this problem into various cases:
Case 1: Both ‘l’ and ‘r’ are even:
1. If l == r, then obviously it is a square by itself and the answer is ‘l’.
2. If l != r, then abs(l-r) must be equal to ‘2’ and the answer will be ‘2’.
Case 2: Both ‘l’ and ‘r’ are odd:
1. If l == r, then as in the above case the answer is ‘l’.
2. If l != r, tnen abs(l-r) must be equal to ‘2’ as in the above case but the answer will not be equal to ‘2’ since both ‘l’ and ‘r’ are odd numbers. Hence the answer will be equal to ‘1’.
Case 3: ‘l’ is odd and ‘r’ is even or vice versa:
This is a very simple case. Since the parity of ‘l’ and ‘r’ are different the answer to this case has to be ‘1’. No other sized squares can be fit into such a rectangle without wasting area.
Happy Coding!!!
Link to the problem:
https://www.hackerearth.com/prayatnaopc/algorithm/the-cakes-walk-3/
Problem statement in a nut shell:
Choose two numbers <= 100 such that pow(a,b) + pow(b,a) = x and a-b is maximum and x<=100
My Solution:
The solution to this problem is based on the simple observation that “1” raised to any power is “1” itself and any number raised to “1” is the number itself. So we can always choose the number ‘a’ as ‘x-1’ and the number ‘b’ as 1 such that the above equation will be satisfied. The only corner case to this exception is x=1. When x=1, the answer is a=1 and b=0.
Problem 2:
Pre-requisite: Simple Math
Link to the problem:
https://www.hackerearth.com/prayatnaopc/algorithm/handsome-boy-and-samosas-11/
Problem statement in a nut shell:
Given a rectangle of length ‘l’ and breadth ‘b’ such that abs(l-b)<=2, find the maximum area of the square of which this rectangle can be broken into without any wastage of area.
My solution:
It has been clearly mentioned in the problem that abs(l-b) <=2. The above statement simplifies a lot of stuff for us. One important point to note here is, the answer to this problem can only be 1 or 2 or l or b. Let us break this problem into various cases:
Case 1: Both ‘l’ and ‘r’ are even:
1. If l == r, then obviously it is a square by itself and the answer is ‘l’.
2. If l != r, then abs(l-r) must be equal to ‘2’ and the answer will be ‘2’.
Case 2: Both ‘l’ and ‘r’ are odd:
1. If l == r, then as in the above case the answer is ‘l’.
2. If l != r, tnen abs(l-r) must be equal to ‘2’ as in the above case but the answer will not be equal to ‘2’ since both ‘l’ and ‘r’ are odd numbers. Hence the answer will be equal to ‘1’.
Case 3: ‘l’ is odd and ‘r’ is even or vice versa:
This is a very simple case. Since the parity of ‘l’ and ‘r’ are different the answer to this case has to be ‘1’. No other sized squares can be fit into such a rectangle without wasting area.
Happy Coding!!!