Monday, October 9, 2023

Codeathon03_Haritha


[Question]

Monkeys in the Garden
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.


Program

package src;

import java.util.Scanner;

public class Codeathon03_Haritha {

  public static void main(String[] args)

  {

   
 Scanner sc = new Scanner(System.in);

 int n = sc.nextInt();          //  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;         // length in clock wise direction
 
   anticlockwiseLength = (j - i + n) % n;     // length in anti-clock wise direction

   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);
 
       }

       }

[Explanation of Solution]

Codeathon03_Haritha Class:

This class contains the main method that calculates the maximum travel time between two trees given their heights.
It uses the Scanner class to read input from the user.

Main Method:

It starts by reading an integer n from the user, which represents the number of trees.

It initializes an integer array heights of size n to store the heights of the trees.

It then enters a loop to read the heights of each tree from the user and stores them in the heights array.

Next, it initializes an integer variable maxTravelTime to keep track of the maximum travel time between any two trees.

It also initializes several integer variables within the nested loops: clockwiseLength, anticlockwiseLength, shorterLength, and totalLength. These variables will be used to calculate the maximum travel time.

The code then enters a nested loop to compare all possible pairs of trees. It uses two nested loops to iterate through each combination of trees.

Inside the nested loops:

clockwiseLength is calculated as the length of the path in the clockwise direction between the two trees.
anticlockwiseLength is calculated as the length of the path in the anticlockwise direction between the two trees.

'shorterLength' is set to the minimum of clockwiseLength and anticlockwiseLength.
totalLength is calculated as the sum of shorterLength, the height of the first tree (heights[i]), and the height of the second tree (heights[j]). This represents the maximum path length between the two trees.

The code checks if totalLength is greater than the current maxTravelTime. If it is, it updates maxTravelTime with the new maximum travel time.

After the nested loops complete, the code has found the maximum travel time between any two trees.

Finally, it prints the maxTravelTime to the standard output, which represents the maximum time it takes to travel between two trees with the given heights.



 Haritha .P(Intern),
 Guard Ninjas,
 Data Shield Team,
 Enterprise Minds.    



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home