Wednesday, October 11, 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.


Code Explaination:

  1. The program, "Question03_Venkata," calculates the maximum travel time for two monkeys in a circular garden with varying tree heights.
  2. Users are prompted to input the number of trees and their respective heights.
  3. The calculateMaximumTravelTime method computes the maximum travel time by iterating through each tree as a potential starting point for one of the monkeys.
  4. For each pair of trees, the method calculates clockwise and anticlockwise distances, choosing the shortest path and adding tree heights to determine the total time.
  5. The program employs a Scanner to collect user input and displays the maximum travel time once calculated.
  6. It offers a straightforward tool for analyzing travel times between trees in a circular garden with variable tree heights.
GutHub Repo link : https://rb.gy/bf1d5

Source Code(Java):

import java.util.Scanner;

public class Question03_Venkata {

   /**
    * Calculates the maximum travel time for two monkeys in a circular garden.
    */
  
public static int calculateMaximumTravelTime(int[] treeHeights) {
      int maxTravelTime = 0;
      int n = treeHeights.length;

      for (int i = 0; i < n; i++) {
         int maxTime = 0;

         for (int j = i + 1; j < n; j++) {
            int clockwiseDistance = (n - j + i) % n;
            int anticlockwiseDistance = (j - i) % n;
            int shortestDistance = Math.min(clockwiseDistance, anticlockwiseDistance);

            int totalTime = shortestDistance + treeHeights[i] + treeHeights[j];
            maxTime = Math.max(maxTime, totalTime);
         }

         maxTravelTime = Math.max(maxTravelTime, maxTime);
      }

      return maxTravelTime;
   }

   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      System.out.print("Enter the number of trees: ");
      int n = scanner.nextInt();

      int[] treeHeights = new int[n];

      System.out.println("Enter the heights of trees (one on each line):");
      for (int i = 0; i < n; i++) {
         treeHeights[i] = scanner.nextInt();
      }

      int maxTravelTime = calculateMaximumTravelTime(treeHeights);
      System.out.println("Maximum Traveling Time: " + maxTravelTime);

      scanner.close();
   }
}

Thank you

Venkata kishore T(Intern)

Shield Warriors,

Data Shield Team,

Enterprise Minds.

 

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...