Saturday, January 30, 2021

SKP's Java Problem Solving Series : Simple Java Inheritance (OOPs)

[Question/Problem Statement is the Property of Techgig]
 
Java Inheritance / Simple OOPs [www.techgig.com] 
Create Two Classes:

BaseClass
The Rectangle class should have two data fields-width and height of int types. The class should have display()method, to print the width and height of the rectangle separated by space.

DerivedClass 
The RectangleArea class is Derived from Rectangle class, i.e., it is the Sub-Class of Rectangle class. The class should have read_input() method, to Read the Values of width and height of the Rectangle. The RectangleArea class should also Overload  the display() Method to Print the Area (width*height) of the Rectangle.

Input Format
The First and Only Line of Input contains two space separated Integers denoting the width and height of the Rectangle.

Constraints
1 <= width,height <= 10^3

Output Format
The Output Should Consist of Exactly Two Lines.
In the First Line, Print the Width and Height of the Rectangle Separated by Space. 
In the Second Line, Print the Area of the Rectangle.

______________ 
 
 
[Explanation of the Solution]
This is the Simplest of all OOPs Questions! Demonstration of Inheritance and Overriding (Very Loosely, Liskov Substitution of SOLID). 
 
________________  
 

[Source Code, Sumith Puri (c) 2021 - Free to Use & Distribute]
 /*    
  * Techgig Core Java Basics Problem - Get Simple OOPs Right!  
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri;  
  */   
     
  import java.io.*;   
  import java.util.*;   
   
   
  class Rectangle {  
   
    private int width;  
    private int height;  
   
    public void display() {  
   
      System.out.println(width + " " + height);  
    }  
   
    public int getWidth() {  
   
      return width;  
    }  
   
    public void setWidth(int width) {  
   
      this.width=width;  
    }  
   
    public int getHeight() {  
   
      return height;  
    }  
   
    public void setHeight(int height) {  
   
      this.height=height;  
    }  
  }  
   
  class RectangleArea extends Rectangle {  
   
    public void read_input() {  
   
     Scanner scanner = new Scanner (System.in);   
       
     setWidth(scanner.nextInt());   
     setHeight(scanner.nextInt());  
    }  
   
    public void display() {  
   
      super.display();  
      System.out.println(getWidth()*getHeight());  
    }  
  }  
     
  public class CandidateCode {   
     
   public static void main(String args[] ) throws Exception {   
     
     RectangleArea rectangleArea = new RectangleArea();  
     rectangleArea.read_input();  
   
     rectangleArea.display();  
   }   
 } 

Happy Problem Solving using Java!

Friday, January 29, 2021

SKP's Java Problem Solving Series : Monkeys in the Garden

[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. 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.

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 & Distribute]
 /*   
  * Techgig Core Java Basics Problem - Monkeys in the Garden 
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri
  */  
   
 import java.io.*;  
 import java.util.*;  
 import java.lang.Math;  
   
 public class CandidateCode {  
   
   public static void main(String args[] ) throws Exception {  
   
     Scanner scanner = new Scanner (System.in);  
       
     int n = scanner.nextInt();  
     int h[] = new int[n], max=0;  
     int cwLen=0,acLen=0,hiLen=0,toLen=0;  
   
     for(int i=0;i<n;i++) {  
       h[i] = scanner.nextInt();        
     }  
     max=h[0];  
   
     for(int i=0;i<n;i++) {  
   
       for(int j=i+1;j<n;j++) {  
         cwLen=Math.abs(((n-j)+i)); // clockwise  
         acLen=Math.abs((j-i));     // anti-clockwise  
         hiLen=(cwLen<=acLen)?(cwLen):(acLen);  
         toLen=hiLen+h[i]+h[j];     // path length    
         if(toLen>max) max=toLen;   // maximum path length  
       }  
     }  
          
     System.out.println (max);   
   }  
 }  
 
Happy Problem Solving using Java!