DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Two-Pass Huffman in Blocks of 2 Symbols: Golang Implementation

Trending

  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  1. DZone
  2. Data Engineering
  3. Data
  4. SKP's Algorithms and Data Structures #9: Java Problem: Monkeys in the Garden

SKP's Algorithms and Data Structures #9: Java Problem: Monkeys in the Garden

This Article Series Focuses on Algorithms, Data Structures, or Applying them to Problem Solving. In this Article, We Discuss the Solution to [Monkeys in the Garden] Problem from Techgig.

By 
Sumith Puri user avatar
Sumith Puri
·
Feb. 28, 21 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
4.3K Views

Join the DZone community and get the full member experience.

Join For Free
[Question/Problem Statement is the Property of Techgig] 

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. Let's 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.

Input Format
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

Output Format
You must Print an Integer which will be the Maximum Possible Travel Time.



[Explanation of the Solution]
Surprisingly, this Problem is under the Object-Oriented Programming (OOP) Section of the Problems! Initially, I was on the Lookout for a 'Mathematically Superior' Solution. But Couldn't Reach Anywhere — In the End, I have a Simple Solution at O(n²). Iterate through Each 'Combination of Trees' and then Find the Clockwise and Anti-Clockwise Distance — At Each Iteration, the Minimum of the Two is Added to the Length of Each Tree. If this Value is Greater than the Maximum Path Length (Previous Iterations) — Replace the Maximum Path Length.
 

[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]
Java
 




x
38


 
1
/*   
2
  * Techgig Core Java Basics Problem - Monkeys in the Garden 
3
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri
4
  */  
5
   
6
 import java.io.*;  
7
 import java.util.*;  
8
 import java.lang.Math;  
9
   
10
 public class CandidateCode {  
11
   
12
   public static void main(String args[] ) throws Exception {  
13
   
14
     Scanner scanner = new Scanner (System.in);  
15
       
16
     int n = scanner.nextInt();  
17
     int h[] = new int[n], max=0;  
18
     int cwLen=0,acLen=0,hiLen=0,toLen=0;  
19
   
20
     for(int i=0;i<n;i++) {  
21
       h[i] = scanner.nextInt();        
22
     }  
23
     max=h[0];  
24
   
25
     for(int i=0;i<n;i++) {  
26
   
27
       for(int j=i+1;j<n;j++) {  
28
         cwLen=Math.abs(((n-j)+i)); // clockwise  
29
         acLen=Math.abs((j-i));     // anti-clockwise  
30
         hiLen=(cwLen<=acLen)?(cwLen):(acLen);  
31
         toLen=hiLen+h[i]+h[j];     // path length    
32
         if(toLen>max) max=toLen;   // maximum path length  
33
       }  
34
     }  
35
          
36
     System.out.println (max);   
37
   }  
38
 }  



Happy Problem Solving using Java!
Java (programming language) Tree (data structure) Data (computing)

Published at DZone with permission of Sumith Puri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Two-Pass Huffman in Blocks of 2 Symbols: Golang Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!