Thursday, 5 October 2023

EM-Tirupati Codeathon Series #03

[Question]
Monkeys in the Garden [www.techgig.com]
In a garden, trees are arranged in a circular fashion with an equal distance between two adjacent trees. The height of trees may vary. Two monkeys live in that garden and they were very close to each other. One day they quarreled due to some misunderstanding. None of them were ready to leave the garden. But each one of them wants that if the other wants to meet him, it should take maximum possible time to reach him, given that they both live in the same garden.
The conditions are that a monkey cannot directly jump from one tree to another. There are 30 trees in the garden. If the height of a tree is H, a monkey can live at any height from 0 to H. Lets say he lives at the height of K then it would take him K unit of time to climb down to the ground level. Similarly, if a monkey wants to climb up to K height it would again take K unit of time. The time to travel between two adjacent trees is 1 unit. A monkey can only travel in a circular fashion in the garden because there is a pond at the center of the garden.
So the question is where should two monkeys live such that the traveling time between them is maximum while choosing the shortest path between them in any direction clockwise or anti-clockwise. You have to answer only the maximum traveling time.
 
[Sample Input]
The First Line consists of Total Number of Trees (N). Each of the Following N Lines contains the Height of Trees in a Clockwise Fashion.
Constraints
1 <= Total Trees <= 30
1 <= Height Of Trees(H) <= 10000

[Sample Output]
You must Print an Integer which will be the Maximum Possible Travel Time.
 
[Explanation of Solution]
The program takes the heights of the trees as input and calculates the maximum possible travel time between the monkeys. It does so by iterating through all possible pairs of trees, determining the shortest path between them in both clockwise and anti-clockwise directions, and calculating the total travel time. It keeps track of the maximum travel time encountered during this process.
 
Github Repo for Code:  https://tinyurl.com/23wf74v3
 
[Solution (Java Code)]
/*
 * Core Java Problem Solving - Codeathon.
 * A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
 *
 */
package codeathon;
import java.util.Scanner;
public class Codeathon03_Kishore
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();          // Total Number of Trees
        int[] heights = new int[n];    // Heights of the Trees
        for (int i = 0; i < n; i++)
        {
            heights[i] = sc.nextInt();
        }
        int maxTravelTime = 0;
        int clockwiseLength, anticlockwiseLength, shorterLength, totalLength;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                clockwiseLength = (n - j + i) % n;         // clockwise length
                anticlockwiseLength = (j - i + n) % n;     // anti-clockwise  length
                shorterLength = Math.min(clockwiseLength, anticlockwiseLength);
                totalLength = shorterLength + heights[i] + heights[j];  // maximum path length
                if (totalLength > maxTravelTime)
                {
                    maxTravelTime = totalLength;      // maximum travel time
                }
            }
        }
        System.out.println(maxTravelTime);
    }
}

No comments:

Post a Comment